type
myServerSocket = class(TServerSocket)
public
constructor Create;
procedure ClientRead(Sender: TObject; Socket: TCustomWinSocket);
end;
procedure myServerSocket.ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
s : string;
begin
s := Socket.ReceiveText;
WriteLn('Server recieved: ' + s);
end;
[...]
// Auszug aus Main-Prozedur der Konsole
// Server
if ActAsServer then
begin
try
begin
ServerSocket := TServerSocket.Create(nil);
mSS := myServerSocket.Create;
ServerSocket.Port := PortNumber;
ServerSocket.OnClientRead := mSS.ClientRead;
ServerSocket.Active := true;
// Wait for connection
while true do
begin
WriteLn(Format('Waiting for client to connect <Active %d>', [ord(ServerSocket.Active)]));
if not ServerSocket.Active then
ServerSocket.Active := true;
Sleep(500);
end;
end;
except on e:
exception do
WriteLn(Format('Server raised
exception <%s>', [e.Message]));
end;
// Close connections
if ServerSocket <> nil then
ServerSocket.Active := false;
FreeAndNil(ServerSocket);
// Client
if ActAsClient then
begin
try
begin
ClientSocket := TClientSocket.Create(nil);
ClientSocket.Port := PortNumber;
ClientSocket.Host := ServerIP;
ClientSocket.Active := true;
// Send data
while not Eof(txtFile) do
begin
ReadLn(txtFile, s);
ClientSocket.Socket.SendText(s);
end;
end;
except on e:
Exception do
WriteLn(Format('Client raised
exception <%s>', [e.Message]));
end;
// Close connections
if ClientSocket <> nil then
ClientSocket.Active := false;
FreeAndNil(ClientSocket);
end;