WarPie90

Internet server Simba 1.4

Dec 8th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.03 KB | None | 0 0
  1. program new;
  2.  
  3. type
  4.   PInternetServer = ^TInternetServer;
  5.   TInternetServer = record
  6.     ID: Int32;
  7.     IP, Port: string;
  8.   end;
  9.  
  10.   TInternetClient = record
  11.     ID: Int32;
  12.     IP, Port: string;
  13.   end;
  14.  
  15.  
  16. procedure TInternetClient.WriteString(s: string);
  17. begin
  18.   SendSocket(Self.ID, s);
  19. end;
  20.  
  21. function TInternetClient.Read(bytes: Int32): string;
  22. begin
  23.   Result := RecvSocketEx(Self.ID, bytes);
  24. end;
  25.  
  26. procedure TInternetClient.SetTimeout(Time: Int32);
  27. begin
  28.   SetSocketTimeout(Self.ID, Time);
  29. end;
  30.  
  31. procedure TInternetClient.CloseAndFree();
  32. begin
  33.   CloseSocket(Self.ID);
  34.   FreeSocket(Self.ID);
  35. end;
  36.  
  37.  
  38. function TInternetServer.Create(IP: string; Port: string): TInternetServer; static;
  39. begin
  40.   Result.ID   := CreateSocket();
  41.   Result.IP   := IP;
  42.   Result.Port := Port;
  43.   BindSocket(Result.ID, Result.IP, Result.Port);
  44.   ListenSocket(Result.ID);
  45. end;
  46.  
  47. function TInternetServer.AcceptClient(): TInternetClient;
  48. begin
  49.   Result.ID := AcceptSocket(Self.ID);
  50.   SocketInfo(Result.ID, Result.IP, Result.Port);
  51. end;
  52.  
  53. procedure TInternetServer.CloseAndFree();
  54. begin
  55.   CloseSocket(Self.ID);
  56.   FreeSocket(Self.ID);
  57. end;
  58.  
  59. procedure TInternetServer.HandleClient(Client: TInternetClient);
  60. var
  61.   Data,IP, port: string;
  62. begin
  63.   Client.SetTimeout(1000*60*5); // 5 minutes
  64.  
  65.   Writeln('Connection from: ', Client.IP, ':', Client.port);
  66.   while True do
  67.   begin
  68.     try
  69.       Data += Client.Read(1);
  70.  
  71.       if Pos('GET', Data) <> 0  then // Some expected string
  72.       begin
  73.         WriteLn('<<< ', Data);
  74.         Client.WriteString('TAKE THAT BACK!');
  75.         Data := '';
  76.       end;
  77.     except
  78.       Exit; //socket auto closed and free'd on timeout
  79.     end;
  80.   end;
  81.  
  82.   Client.CloseAndFree();
  83. end;
  84.  
  85. procedure Main;
  86. var
  87.   Server: TInternetServer;
  88.   Client: TInternetClient;
  89. begin
  90.   Server := TInternetServer.Create('127.0.0.1', '27015'); // Example IP and Port
  91.   while True do
  92.   begin
  93.     Client := Server.AcceptClient();
  94.     Server.HandleClient(Client);
  95.   end;
  96.   Server.CloseAndFree();
  97. end;
  98.  
  99. begin
  100.   Main;
  101. end.
Add Comment
Please, Sign In to add comment