Hallo Berlinermauer, in deinem geposteten Code, ist einiges nicht ganz optimal implementiert.
Für eine Asynchrone Übertragung, kann folgendes Template verwendet werden.
Protokoll Stack, Errorbehandlung müssen noch implementiert werden.
Als konzeptioneller Ansatz, sollte das Teil aber brauchbar sein.
Delphi-Quellcode:
program ConsoleClient;
{$APPTYPE CONSOLE}
uses
Windows,
Messages,
SysUtils,
Winsock,
ScktComp;
type
TClientSocketSample = class
private
FClientSocket: TClientSocket;
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ClientSocketLookup(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocketWrite(Sender: TObject;
Socket: TCustomWinSocket);
public
constructor create;
destructor destroy; override;
end;
var
ClientSocketSample: TClientSocketSample;
MsgRec : TMsg;
{ TClientSocketSample }
procedure TClientSocketSample.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
writeln(
'Ein Client hat sich Server verbunden. Socket: ' +
inttostr(Socket.SocketHandle)
);
end;
procedure TClientSocketSample.ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
begin
writeln(
'Ein Client is connecting to Server Socket: ' +
inttostr(Socket.SocketHandle)
);
end;
procedure TClientSocketSample.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
writeln(
'Ein Client disconnect from Server. Socket: ' +
inttostr(Socket.SocketHandle)
);
end;
procedure TClientSocketSample.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
writeln(
'Ein Clientverbindung mit folgendem eindeutigen Socket: ' +
inttostr(Socket.SocketHandle) +
' hat einen Fehler verursacht, der socket Fehlercode:' +
inttostr(ErrorCode)
);
end;
procedure TClientSocketSample.ClientSocketLookup(Sender: TObject;
Socket: TCustomWinSocket);
begin
writeln(
'Ein Client Lookup for Server. Socket: ' +
inttostr(Socket.SocketHandle)
);
end;
procedure TClientSocketSample.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
var
cbRcv: integer;
RcvBuf: array [0..32767] of Byte;
s: string;
begin
cbRcv := Socket.ReceiveBuf(RcvBuf[0], Length(RcvBuf));
if (cbRcv > 0) and (Socket.SocketHandle > 0) then begin
SetLength(s, cbRcv);
move(RcvBuf[0], s[1], cbRcv);
writeln('Vom Server mit folgendem eindeutigen Socket: ' +
inttostr(Socket.SocketHandle) + ' wurden ' + IntToStr(cbRcv) + ' Bytes ' +
'mit folgenden Daten [ ' + s + ' ] empfangen'
);
end;
end;
procedure TClientSocketSample.ClientSocketWrite(Sender: TObject;
Socket: TCustomWinSocket);
var
s: string;
begin
s := 'Hello World, this is from Client';
Socket.SendBuf(s[1], Length(s));
end;
constructor TClientSocketSample.create;
begin
Self.FClientSocket := TClientSocket.create(nil);
with Self.FClientSocket, Self do begin
Port := 8088;
Address := '127.0.0.1';
OnConnect := ClientSocketConnect;
OnDisconnect := ClientSocketDisconnect;
OnError := ClientSocketError;
OnRead := ClientSocketRead;
OnConnecting := ClientSocketConnecting;
OnLookUp := ClientSocketLookUp;
OnWrite := ClientSocketWrite;
Active := true;
end;
end;
destructor TClientSocketSample.destroy;
begin
shutdown(Self.FClientSocket.Socket.SocketHandle, SD_BOTH);
CloseSocket(Self.FClientSocket.Socket.SocketHandle);
inherited;
end;
begin
ClientSocketSample := TClientSocketSample.Create;
try
repeat
if PeekMessage(MsgRec, 0, 0, 0, PM_REMOVE) then
DispatchMessage(MsgRec) else WaitMessage;
until (MsgRec.Message = WM_QUIT) or (MsgRec.Message = WM_CLOSE);
finally
ClientSocketSample.Free;
ClientSocketSample := nil;
end;
end.
lg. Astat