home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vptcp110.zip / FINGERD.PAS next >
Pascal/Delphi Source File  |  1996-06-11  |  2KB  |  85 lines

  1. Program FingerD;
  2. (* Simple Finger Daemon
  3.  * Copyright 1996 Antony T Curtis
  4.  *)
  5. Uses Os2Def,Use32,Sockets,Strings;
  6.  
  7. {$PMTYPE VIO}
  8.  
  9. Const
  10.   MyMessage    :PChar    = 'Simple Finger Daemon'#10#13+
  11.               'Demo Message...'#10#13;
  12.  
  13. Procedure Main;
  14. var
  15.   Sock,NewSock    :TSocket;
  16.   Server,Client :TSockAddr_in;
  17.   Buffer    :Array[1..512] of Byte;
  18.   i        :Integer;
  19.   Info        :THostEnt;
  20.   Serv        :TServEnt;
  21. begin
  22.   Sock_Init;                    (* Initilise Socket Library *)
  23.   if SockError<>0 then begin
  24.     Writeln('Unable to initilise socket library');
  25.     halt(1);
  26.   end;
  27.  
  28.   if not GetServiceByName(Serv,'finger','tcp') then begin     (* Inquire about a service *)
  29.     Writeln('Requested service unavailable');
  30.     halt;
  31.   end;
  32.   Writeln(' Using service ',Serv.s_name,'/',Serv.s_proto,' on port ',Serv.s_port);
  33.  
  34.   Sock:=Sock_New(AF_INET,SOCK_STREAM,0);    (* Open a new socket *)
  35.  
  36.   if Sock_Error then halt(1);            (* Check for errors *)
  37.  
  38.   Server.sin_family     := AF_INET;
  39.   Server.sin_port     := Serv.s_Port;
  40.   Server.sin_addr.s_addr := INADDR_ANY;
  41.  
  42.   Sock_Bind(Sock,TSockAddr(Server));        (* Bind the socket to the port *)
  43.   if Sock_Error then begin
  44.     Sock_Close(Sock);
  45.     halt(2);
  46.   end;
  47.  
  48.   while Sock_Listen(Sock,1) do begin        (* Listen for anything interesting *)
  49.  
  50.                         (* Accept it *)
  51.     NewSock:=Sock_Accept(Sock,TSockAddr(Client));
  52.  
  53.     if Sock_Error then continue;
  54.  
  55.  
  56.     with Client.sin_addr do
  57.     Write('Finger request from ',inet_ntoa(Client.sin_addr));
  58.  
  59.                            (* Get information about the host *)
  60.     if GetHostByAddr(Info,Client.sin_addr,AF_INET) then
  61.       Write(' [',Info.h_name,']');
  62.     Writeln;
  63.  
  64.     Sock_Read(NewSock,Buffer,SizeOf(Buffer),i); (* Receive the finger data *)
  65.     if not Sock_Error then Writeln('':2,i,' bytes received');
  66.     if Sock_Error then begin
  67.       Sock_Close(NewSock);
  68.       continue;
  69.     end;
  70.                         (* Send our message back *)
  71.     Sock_Write(NewSock,MyMessage^,StrLen(MyMessage),i);
  72.     if not Sock_Error then WriteLn('':2,i,' bytes sent');
  73.  
  74.     Sock_Close(NewSock);            (* Close the socket *)
  75.   end;
  76.   Sock_Error;
  77.   Sock_Close(Sock);
  78. end;
  79.  
  80. begin
  81.   Writeln('Simple Finger Daemon');
  82.   Writeln('Copyright 1996 Antony T Curtis');
  83.   Main;
  84. end.
  85.