The following example shows a typical ClientExecute method for a custom descendant of TServerClientThread. It reads from the socket connection specified by ClientSocket using a thread-local instance of TWinSocketStream.procedure TMyServerThread.ClientExecute;var Stream : TWinSocketStream; Buffer : array[0 .. 9] of Char; begin { make sure connection is active } while (not Terminated) and ClientSocket.Connected do begin try Stream := TWinSocketStream.Create(ClientSocket, 60000); try FillChar(Buffer, 10, 0); { initialize the buffer } { give the client 60 seconds to start writing } if Stream.WaitForData(60000) then begin if Stream.Read(Buffer, 10) = 0 then { if can抰 read in 60 seconds } ClientSocket.Close; { close the connection } { now process the request } ... end else ClientSocket.Close; { if client doesn抰 start, close } finally Stream.Free; end; except HandleException; end; end; end;
Stream : TWinSocketStream;
Buffer : array[0 .. 9] of Char;
begin
{ make sure connection is active }
while (not Terminated) and ClientSocket.Connected do
begin
try
Stream := TWinSocketStream.Create(ClientSocket, 60000);
try
FillChar(Buffer, 10, 0); { initialize the buffer }
{ give the client 60 seconds to start writing }
if Stream.WaitForData(60000) then begin
if Stream.Read(Buffer, 10) = 0 then { if can抰 read in 60 seconds }
ClientSocket.Close; { close the connection }
{ now process the request }
...
end
else
ClientSocket.Close; { if client doesn抰 start, close }
finally
Stream.Free;
end;
except
HandleException; end;
end;
end;
这个接受数据,另外一个发送数据不就行了.
FillChar( CApp_Head, SizeOf(VApp_Head), 0 );
with CApp_Head do
begin
len:= htonl( 12 ); //longword; //消息的总长度(字节)
CommandId := htonl(APP_ACTIVE); //longword; //命令ID
seqno := htonl( CMPPLSH ); //longword; //序列号,循环使用,步长为1,范围1-0x7fffffff
end;
form1.clientsocket.socket.SendBuf(CApp_Head, SizeOf(VApp_Head));