//thread record
type TSockData =
record
Sock: TSocket;
SockEvent: WSAEvent;
end;
//connect routine
procedure TTCPSocket.ConnectSocket(
IP:
string; PORT: Integer);
var SockAddr: TSockAddrIn;
AddrLen: Integer;
Data: WSAData;
begin
WSAStartup( MakeWord(2,0), Data);
Sock := CreateSocket;
AddrLen := SizeOf( SockAddr );
SockAddr.sin_family := AF_Inet;
SockAddr.sin_port := htons( PORT );
SockAddr.sin_addr.S_addr := inet_addr( PChar(
IP) );
if Connect(Sock, SockAddr, AddrLen) = Socket_Error
then
HandleError;
SockEvent:=WSACreateEvent;
//--->>>> Sock und SockEvent sind global
WSAEventSelect( Sock, SockEvent, FD_Read);
end;
//Thread
threadvar
SockPtr : ^TSockData;
procedure ReadSocket(Parameter: Pointer);
var buffer:
array[0..511]
of Char;
data:
string;
recvLen, WSAResult: Integer;
SockEvent: WSAEvent;
begin
SockPtr := Parameter;
//Pointeradresse wurde richtig übergeben
data := '
';
while true
do begin
WSAResult := WSAWaitForMultipleEvents( 1, @SockPtr.SockEvent, false, infinite, false);
case WSAResult
of //-------->>>> WSAResult gibt -1
WSA_WAIT_EVENT_0:
begin
fillchar(buffer, 512, #0);
recvLen := recv(SockPtr.Sock, buffer, sizeOf(buffer), 0);
showmessage(buffer);
{if recvLen = INVALID_SOCKET then
HandleERROR;}
data := data + buffer;
end;
else
break;
end;
WSAresetEvent(SockEvent);
end;
// OnReceive( data );
WSACloseEvent(SockEvent);
WSACleanUp;
// EndThread(0);
end;
//--->>> die schleife einfach übersprungen
// start thread
procedure TTCPSocket.StartReading;
var id: LongWord;
DataBuf: TSockData;
begin
DataBuf.Sock:=Sock;
DataBuf.SockEvent:=SockEvent;
ReadingThread := BeginThread(
nil, 0, Addr(ReadSocket), Addr(DataBuf), 0, id);
end;