dsocksvr.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. Program server;
  2. {
  3. $Id$
  4. This file is part of the Free Component Library (FCL)
  5. Copyright (c) 1999-2000 by the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. Dual server program. This will listen on port 4100 till
  14. a client connects if '-i' is on the command-line.
  15. Otherwise it will open a unix socket. You can connect by
  16. running the 'sockcli' or 'dsockcli' programs in another
  17. terminal.
  18. specifying -b on the command-line will disable blocking.
  19. }
  20. {$mode objfpc} {$H+}
  21. uses sysutils,ssockets,getopts;
  22. const
  23. ThePort=4100;
  24. TheSocket = 'ServerSoc';
  25. Var
  26. DoInet,NonBlocking : boolean;
  27. Type
  28. TServerApp = Class(TObject)
  29. Private
  30. FCalls : longint;
  31. FServer : TSocketServer;
  32. Public
  33. Constructor Create(Port : longint);
  34. Constructor Create(Socket : String);
  35. Destructor Destroy;override;
  36. Procedure OnConnect (Sender : TObject; Data : TSocketStream);
  37. Procedure OnIdle(Sender : TObject);
  38. Procedure Run;
  39. end;
  40. Constructor TServerApp.Create(Port : longint);
  41. begin
  42. FServer:=TINetServer.Create(Port);
  43. FServer.OnConnect:=@OnConnect;
  44. end;
  45. Constructor TServerApp.Create(Socket : String);
  46. begin
  47. FServer:=TUnixServer.Create(Socket);
  48. FServer.OnConnect:=@OnConnect;
  49. end;
  50. Destructor TServerApp.Destroy;
  51. begin
  52. FServer.Free;
  53. end;
  54. Procedure TServerApp.OnConnect (Sender : TObject; Data : TSocketStream);
  55. Var Buf : ShortString;
  56. Count : longint;
  57. begin
  58. Repeat
  59. Count:=Data.Read(Buf[1],255);
  60. SetLength(Buf,Count);
  61. Write('Server got : ',Buf);
  62. Until (Count=0) or (Pos('QUIT',Buf)<>0);
  63. Data.Free;
  64. FServer.StopAccepting;
  65. end;
  66. Procedure TServerApp.OnIdle(Sender : TObject);
  67. begin
  68. Inc(FCalls);
  69. Write('.');
  70. end;
  71. Procedure TServerApp.Run;
  72. begin
  73. Write ('Listening on ');
  74. if FServer is TUnixServer Then
  75. Writeln ('socket : ',(FServer as TUnixServer).Filename)
  76. else If FServer is TINetServer Then
  77. Writeln ('port : ',(FServer as TInetServer).port);
  78. If NonBlocking then
  79. begin
  80. FServer.SetNonBlocking;
  81. FServer.OnIdle:=@OnIdle;
  82. end;
  83. FServer.StartAccepting;
  84. end;
  85. Var
  86. Application : TServerApp;
  87. c : char;
  88. begin
  89. DoInet:=False;
  90. NonBlocking:=False;
  91. repeat
  92. c:=getopt('ib');
  93. case c of
  94. 'b' : NonBlocking:=True;
  95. 'i' : DoInet:=True;
  96. end;
  97. until c=EndOfOptions;
  98. If DoInet then
  99. Application:=TServerApp.Create(ThePort)
  100. else
  101. Application:=TServerApp.Create(TheSocket);
  102. Application.Run;
  103. Application.Free;
  104. end.
  105. {
  106. $Log$
  107. Revision 1.5 2005-03-16 13:35:59 marco
  108. * some fixes for objfpc mode
  109. Revision 1.4 2005/02/14 17:13:18 peter
  110. * truncate log
  111. }