serverex.pp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. Program Socket_Comms_Test;
  2. {***************************************************************************
  3. TCP/IP Streaming Socket Server Example program.
  4. NumberofConnections is the number of consecutive active connections I will
  5. allow. I have not hit this limit yet.
  6. This defaults to port 5000
  7. The MOST Important thing to look at when doing socket calls of any kind
  8. is the byte order in the structure. Got caught big time with this in reference
  9. to the port number.
  10. This program runs as-is, just telnet localhost 5000 to connect to it.
  11. No warranty at all, I will not be responsible if this sets fire to your dog!
  12. This is exactly as I use it, I have just put the references to my db unit
  13. in curly brackets. It just echoes back what you type on a line by line basis
  14. Run it in X or on a seperate virtual console to the one you are telneting from
  15. as it prints a LOT of info to the console about what it is doing and such.
  16. I'm not a pretty coder at all, so please, no complaints about the lack of
  17. comments or coding style, unless they are very contructive ;p)
  18. type 'quit', minus the quotes and in lower case on the console to exit the
  19. program. The only problem I can see with this, is if you exit it, it does
  20. not shut down the connections to the telnet sessions cleanly, and therefore
  21. it leaves port 5000 in a TIME_WAIT state for a couple of minutes. This prevents
  22. you re-running the program immediately as it will not bind to the port.
  23. (Bind Error 98).
  24. If you know how to fix this, please let me know and I'll update the code.
  25. If you exit all your telnet sessions before shutting the server down, it
  26. works fine.
  27. Hope some of you find this usefull. I wrote it, purely because there is a
  28. big lack of examples of linux port use in FPC. And I know NO C, therefore
  29. the examples on the net meant nothing to me.
  30. All I ask is :-
  31. If you like it, use it or want to change it, please drop me an E-mail.
  32. Regards Brad Campbell
  33. [email protected]
  34. ***************************************************************************}
  35. Uses Linux, Sockets, Sysutils{, dbu};
  36. Const
  37. NumberofConnections = 5;
  38. Type ConnectionType = Record
  39. IP : Cardinal;
  40. Port : Word;
  41. Handle : Integer;
  42. Connected : Boolean;
  43. IdleTimer : Integer;
  44. End;
  45. Var
  46. Connection : Array[1..NumberofConnections] of ConnectionType;
  47. FDS : FDSet;
  48. S : LongInt;
  49. PortNumber : Word;
  50. GreatestHandle : Integer;
  51. Quit : Boolean;
  52. Command : String;
  53. Procedure ZeroConnection;
  54. Var Loop : Integer;
  55. Begin
  56. For Loop := 1 to NumberOfConnections do
  57. Connection[Loop].Connected := False;
  58. End;
  59. Function FreeConnections : Integer;
  60. Var Loop : Integer;
  61. Result : Integer;
  62. Begin
  63. Result := 0;
  64. For Loop := 1 to NumberOfConnections do
  65. If Not Connection[Loop].Connected then Result := Result + 1;
  66. FreeConnections := Result;
  67. End;
  68. Function GetFreeConnection : Integer;
  69. Var Loop : Integer;
  70. Result : Integer;
  71. Found : Boolean;
  72. Begin
  73. Result := 0;
  74. Loop := 1;
  75. Found := False;
  76. While (Loop < NumberOfConnections + 1) and (Not Found) do
  77. Begin
  78. If Not Connection[Loop].Connected then
  79. Begin
  80. Found := True;
  81. Result := Loop;
  82. End;
  83. Loop := Loop + 1;
  84. GetFreeConnection := Result;
  85. End;
  86. End;
  87. Procedure PError(S : String);
  88. Begin
  89. Writeln(S,SocketError);
  90. Halt(100);
  91. End;
  92. Procedure PDebug(S : String);
  93. Begin
  94. Writeln(S);
  95. End;
  96. Procedure PDebugNOLF(S: String);
  97. Begin
  98. Write(S);
  99. End;
  100. Function SockAddrtoString(InAddr : LongWord) : String;
  101. Var
  102. P1,P2,P3,P4 : Byte;
  103. S1,S2,S3,S4 : String;
  104. Begin
  105. P1 := (InAddr And $ff000000) Shr 24;
  106. P2 := (InAddr And $ff0000) Shr 16;
  107. P3 := (InAddr And $ff00) Shr 8;
  108. P4 := InAddr And $FF;
  109. Str(P1,S1);
  110. Str(P2,S2);
  111. Str(P3,S3);
  112. Str(P4,S4);
  113. SockAddrtoString := S4+'.'+S3+'.'+S2+'.'+S1;
  114. End;
  115. Procedure WelcomeHandle(Handle, ConnNum : Integer);
  116. Var Buffer : String;
  117. Sent : Integer;
  118. Begin
  119. Buffer := 'Welcome to Brads Server 1.0'+#10+#13+'You Are Connection '+
  120. InttoStr(ConnNum)+' Of '+InttoStr(NumberofConnections)+
  121. ', With '+InttoStr(FreeConnections)+' Connections Free'#13+#10;
  122. Sent := Send(Handle,Buffer[1],Length(Buffer),0);
  123. If Sent <> Length(Buffer) then PDebug('Wanted to Send : '
  124. +InttoStr(Length(Buffer))+' Sent Only : '+InttoStr(Sent)+
  125. ' to Connection : '+InttoStr(ConnNum));
  126. End;
  127. Procedure AcceptNewConnection;
  128. Var ConnectionNumber : Integer;
  129. Handle : LongInt;
  130. FromAddrSize : LongInt;
  131. FromAddr : TInetSockAddr;
  132. Begin
  133. FromAddrSize := Sizeof(FromAddr);
  134. If FreeConnections > 0 then Begin
  135. ConnectionNumber := GetFreeConnection;
  136. PDebug('Accepting New Connection Number : '+InttoStr(ConnectionNumber));
  137. Handle := Accept(S,FromAddr,FromAddrSize);
  138. If Handle < 0 then PError('Accept Error!!');
  139. PDebug('Accepted From : '+SockAddrtoString(FromAddr.Addr)+' Port : '
  140. +Inttostr(Swap(FromAddr.Port)));
  141. Connection[ConnectionNumber].Handle := Handle;
  142. Connection[ConnectionNumber].IP := FromAddr.Addr;
  143. Connection[ConnectionNumber].Port := FromAddr.Port;
  144. Connection[ConnectionNumber].Connected := True;
  145. Connection[ConnectionNumber].IdleTimer := 0;
  146. WelcomeHandle(Handle,ConnectionNumber);
  147. End;
  148. End;
  149. Procedure SetUpSocket;
  150. Var
  151. SockAddr : TInetSockAddr;
  152. Begin
  153. SockAddr.Family := AF_INET;
  154. SockAddr.Port := Swap(PortNumber);
  155. SockAddr.Addr := 0;
  156. S := Socket(AF_INET,SOCK_STREAM,0);
  157. If SocketError <> 0 then PError('Socket Error : ');
  158. If not Bind(S,SockAddr,SizeOf(SockAddr)) then PError('Bind Error : ');
  159. If not Listen(S,5) then PError('Listen Error : ');
  160. End;
  161. Procedure LoadConnectedFDS;
  162. Var Loop : Integer;
  163. Begin
  164. For Loop := 1 to NumberOfConnections Do
  165. If Connection[Loop].Connected then
  166. Begin
  167. FD_SET(Connection[Loop].Handle,FDS);
  168. If Connection[Loop].Handle > GreatestHandle then
  169. GreatestHandle := Connection[Loop].Handle;
  170. End;
  171. End;
  172. Procedure ServiceHandle(Handle, ConnectionNum : Integer);
  173. Var Buffer : String;
  174. Sent, BufferLength : Integer;
  175. Begin
  176. Writeln('Service Handle : ',Handle);
  177. BufferLength := Recv(Handle,Buffer[1],200,0);
  178. Setlength(Buffer,BufferLength);
  179. If SocketError <> 0 then PDebug('Reciceved Socket Error : '
  180. +InttoStr(SocketError)+' OnHandle '+InttoStr(Handle));
  181. If BufferLength = 0 then {It's EOF, Socket has been closed}
  182. Begin
  183. PDebug('Socket Handle '+InttoStr(Handle)+' Closed');
  184. Connection[ConnectionNum].Connected := False;
  185. Shutdown(Handle,2);
  186. fdClose(Handle);
  187. End
  188. Else Begin
  189. PDebug(InttoStr(BufferLength)+' Bytes Recieved');
  190. {Buffer := Db_Query(Buffer);}
  191. Sent := Send(Handle,Buffer[1],Length(Buffer),0);
  192. If Sent <> Bufferlength then
  193. PDebug('Wanted to Send : '+InttoStr(Length(Buffer))+' Only Sent : '+InttoStr(Sent));
  194. End;
  195. End;
  196. Procedure ServiceSockets;
  197. Var Loop : Integer;
  198. Begin
  199. For Loop := 1 to NumberOfConnections do
  200. If Connection[Loop].Connected then
  201. If FD_ISSET(Connection[Loop].Handle,FDS) Then
  202. ServiceHandle(Connection[Loop].Handle,Loop);
  203. IF FD_ISSET(S,FDS) then AcceptNewConnection;
  204. End;
  205. Procedure CloseAllOpen;
  206. Var Loop : Integer;
  207. Begin
  208. For Loop := 1 to NumberOfConnections do
  209. Begin
  210. If Connection[Loop].Connected = True then
  211. Begin
  212. Shutdown(Connection[Loop].Handle,1);
  213. { fdClose(Connection[Loop].Handle);}
  214. {Connection[Loop].Connected := False;}
  215. End;
  216. End;
  217. End;
  218. begin
  219. ZeroConnection; {Clear Connected Array}
  220. Quit := False;
  221. PortNumber := 5000;
  222. SetupSocket;
  223. Repeat
  224. FD_ZERO(FDS);
  225. FD_SET(S,FDS); { Socket Looking for new connections }
  226. FD_SET(1,FDS); { Terminal }
  227. GreatestHandle := S;
  228. LoadConnectedFDS;
  229. If Select(GreatestHandle+1,@FDS,nil,nil,1000) > 0 then
  230. Begin
  231. ServiceSockets;
  232. If FD_ISSET(1,FDS) then
  233. Begin
  234. PDebug('Reading Console');
  235. Readln(Command);
  236. If Command='quit' then quit := True;
  237. { Else Writeln(DB_Query(Command));}
  238. Command := '';
  239. End;
  240. End;
  241. {DB_Tic;} {Updates Database Internals, Needs at Least 1 run per second}
  242. Until Quit = True;
  243. CloseAllOpen;
  244. End.