TClientSocket und Threads

Nur für mich, ums irgendwann wieder zu finden 🙂

Quellcode, der funktioniert:

procedure TLS_Logopak.Datensenden(sString: String); var Sockenstrom: TWinSocketStream;
DrClient : TClientSocket;
Buffer: Array [0..2000] of Char;
sResult : String;
bConnected : Boolean;
bSent      : Boolean;
begin
Sockenstrom := nil;
DrClient := nil;
bSent := false;
bAk := false;
bNAK := false;
bTimeOut := true;
bConnected := false;
try
DrClient := TClientSocket.Create (nil);
bTimeout := true;

with Drucker_Data.Drucker[_iDrucker_idx] do
begin
DrClient.Address    := sDrucker_IP ;
DrClient.Port       := iServer_Port;
DrClient.ClientType := ctBlocking;
DrClient.Active := true;
bConnected := true;
end;
except
on e: Exception do
begin
Trace (1,TRC_Logopak,'(Datensenden) Exception bei Connect %s‘,[e.Message]);
end;
end;

if bConnected then
begin
try
Sockenstrom := TWinSocketStream.Create (DrClient.Socket,2000);

Sockenstrom.Write (PChar(sString)^, Length(sString));
bSent := true;
except
on e: Exception do
begin
Trace (1,TRC_Logopak,'(Datensenden) Exception bei Write %s‘,[e.Message]);
end;
end;
if bSent then
begin
FillChar (Buffer, SizeOf (Buffer),0);
sResult := “;
if Sockenstrom.WaitForData (1000) then
begin
Sockenstrom.Read(Buffer,SizeOf(Buffer));
// Was damit machen
end;
end;
end;
if Assigned (Sockenstrom) then
begin
FreeAndNil(Sockenstrom);
end;
if Assigned (DrClient) then
begin
if DrClient.Active then DrClient.close;
FreeAndNil (DrClient);
end;
end;

Leave a Reply

You must be logged in to post a comment.