Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program new;
- type
- PInternetServer = ^TInternetServer;
- TInternetServer = record
- ID: Int32;
- IP, Port: string;
- end;
- TInternetClient = record
- ID: Int32;
- IP, Port: string;
- end;
- procedure TInternetClient.WriteString(s: string);
- begin
- SendSocket(Self.ID, s);
- end;
- function TInternetClient.Read(bytes: Int32): string;
- begin
- Result := RecvSocketEx(Self.ID, bytes);
- end;
- procedure TInternetClient.SetTimeout(Time: Int32);
- begin
- SetSocketTimeout(Self.ID, Time);
- end;
- procedure TInternetClient.CloseAndFree();
- begin
- CloseSocket(Self.ID);
- FreeSocket(Self.ID);
- end;
- function TInternetServer.Create(IP: string; Port: string): TInternetServer; static;
- begin
- Result.ID := CreateSocket();
- Result.IP := IP;
- Result.Port := Port;
- BindSocket(Result.ID, Result.IP, Result.Port);
- ListenSocket(Result.ID);
- end;
- function TInternetServer.AcceptClient(): TInternetClient;
- begin
- Result.ID := AcceptSocket(Self.ID);
- SocketInfo(Result.ID, Result.IP, Result.Port);
- end;
- procedure TInternetServer.CloseAndFree();
- begin
- CloseSocket(Self.ID);
- FreeSocket(Self.ID);
- end;
- procedure TInternetServer.HandleClient(Client: TInternetClient);
- var
- Data,IP, port: string;
- begin
- Client.SetTimeout(1000*60*5); // 5 minutes
- Writeln('Connection from: ', Client.IP, ':', Client.port);
- while True do
- begin
- try
- Data += Client.Read(1);
- if Pos('GET', Data) <> 0 then // Some expected string
- begin
- WriteLn('<<< ', Data);
- Client.WriteString('TAKE THAT BACK!');
- Data := '';
- end;
- except
- Exit; //socket auto closed and free'd on timeout
- end;
- end;
- Client.CloseAndFree();
- end;
- procedure Main;
- var
- Server: TInternetServer;
- Client: TInternetClient;
- begin
- Server := TInternetServer.Create('127.0.0.1', '27015'); // Example IP and Port
- while True do
- begin
- Client := Server.AcceptClient();
- Server.HandleClient(Client);
- end;
- Server.CloseAndFree();
- end;
- begin
- Main;
- end.
Add Comment
Please, Sign In to add comment