2
0

isocksvr.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. Program server;
  2. {
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. TInetServer server program. This will listen on port 4100 till
  13. a client connects. You can connect by running the 'isockcli' or
  14. 'dsockcli -i' programs in another terminal.
  15. }
  16. {$mode objfpc}{$H+}
  17. uses
  18. {$IFDEF UNIX}cthreads,{$ENDIF}
  19. classes, sockets, ssockets;
  20. const
  21. ThePort=4100;
  22. Type
  23. { TAcceptThread }
  24. TAcceptThread = Class(TThread)
  25. FSocket : TInetServer;
  26. Constructor Create(aSocket : TInetServer);
  27. Procedure Execute; override;
  28. end;
  29. TINetServerApp = Class(TObject)
  30. Private
  31. FServer : TInetServer;
  32. Public
  33. Constructor Create(Port : longint);
  34. Destructor Destroy;override;
  35. Procedure OnConnect (Sender : TObject; Data : TSocketStream);
  36. Procedure OnDisConnect (Sender : TObject; Data : TSocketStream);
  37. Procedure Run;
  38. end;
  39. { TAcceptThread }
  40. constructor TAcceptThread.Create(aSocket: TInetServer);
  41. begin
  42. FSocket:=aSocket;
  43. FreeOnTerminate:=True;
  44. Inherited Create(False);
  45. end;
  46. procedure TAcceptThread.Execute;
  47. begin
  48. FSocket.StartAccepting;
  49. end;
  50. { TInetServerApp }
  51. Constructor TInetServerApp.Create(Port : longint);
  52. begin
  53. FServer:=TINetServer.Create(Port);
  54. FServer.OnConnect:=@OnConnect;
  55. FServer.OnDisConnect:=@OnDisConnect;
  56. end;
  57. Destructor TInetServerApp.Destroy;
  58. begin
  59. FServer.Free;
  60. end;
  61. Function SocketAddrToString(ASocketAddr: TSockAddr): AnsiString;
  62. Var
  63. S : ShortString;
  64. begin
  65. Result:='';
  66. if ASocketAddr.sa_family = AF_INET then
  67. begin
  68. S := NetAddrToStr(ASocketAddr.sin_addr);
  69. Result:=S;
  70. end;
  71. end;
  72. Procedure TInetServerApp.OnDisConnect (Sender : TObject; Data : TSocketStream);
  73. var
  74. PeerHost : String;
  75. begin
  76. PeerHost:=SocketAddrToString(Data.RemoteAddress);
  77. Writeln('Disconnecting from ',PeerHost);
  78. end;
  79. Procedure TInetServerApp.OnConnect (Sender : TObject; Data : TSocketStream);
  80. Var Buf : ShortString;
  81. Count : longint;
  82. begin
  83. Writeln('Connection from ',SocketAddrToString(Data.RemoteAddress));
  84. Repeat
  85. Count:=Data.Read(Buf[1],255);
  86. SetLength(Buf,Count);
  87. Write('Server got : ',Buf);
  88. Until (Count=0) or (Pos('QUIT',Buf)<>0);
  89. if Pos('QUIT',Buf)<>0 then
  90. FServer.StopAccepting;
  91. Data.Free;
  92. end;
  93. Procedure TInetServerApp.Run;
  94. begin
  95. TAcceptThread.Create(FServer).WaitFor;
  96. end;
  97. Var
  98. Application : TInetServerApp;
  99. begin
  100. Application:=TInetServerApp.Create(ThePort);
  101. Application.Run;
  102. Application.Free;
  103. end.