P
ply2002
Unregistered / Unconfirmed
GUEST, unregistred user!
各位老大,利用clientsocket和seversocket做数据传输,client端采用阻塞,server端stThreadBlocking 一对一传输是正常的,但是我一打开多个client同时向sever端发送数据时,就出错了,望各位老大,指点一下!!以下是我源代码:
private
thr:tserverclientwinsocket;
//serversockitconnection : TCustomWinSocket;
var
myt:TClientThread;
procedure Tfrmsever.ServerSocketGetThread(Sender: TObject; ClientSocket: TServerClientWinSocket; var SocketThread: TServerClientThread);
var
i :integer;
begin
myt:= TClientThread.Create(false, ClientSocket);
myt.thr:=clientsocket;
socketthread:=myt;
//i:=serversocket.Socket.ActiveThreads;
//myt.serversockitconnection:=serversocket.Socket.Connections[i-1];
end;
Procedure TClientThread.ClientExecute;
Var
ReceiveBuffer : Array[0..299] Of Char;
SocketStream : TWinSocketStream;
BytesRead ,j: Integer;
Begin
While Not Terminated And ClientSocket.Connected Do
Begin
try
SocketStream := TWinSocketStream.Create(ClientSocket, 60000);
try
FillChar(ReceiveBuffer, 10, 0);
If SocketStream.WaitForData(5000) Then
begin
Repeat
BytesRead := SocketStream.Read(ReceiveBuffer,SizeOf(ReceiveBuffer));
if BytesRead = 0 then
ClientSocket.Close
Else
begin
mystream.WriteBuffer(ReceiveBuffer, BytesRead);
end;
Until Not SocketStream.WaitForData(2000);
end;
finally
SocketStream.Free;
ClientSocket.Close;
mystream.clear;
end;
except
HandleException;
end;
End;
End;
private
thr:tserverclientwinsocket;
//serversockitconnection : TCustomWinSocket;
var
myt:TClientThread;
procedure Tfrmsever.ServerSocketGetThread(Sender: TObject; ClientSocket: TServerClientWinSocket; var SocketThread: TServerClientThread);
var
i :integer;
begin
myt:= TClientThread.Create(false, ClientSocket);
myt.thr:=clientsocket;
socketthread:=myt;
//i:=serversocket.Socket.ActiveThreads;
//myt.serversockitconnection:=serversocket.Socket.Connections[i-1];
end;
Procedure TClientThread.ClientExecute;
Var
ReceiveBuffer : Array[0..299] Of Char;
SocketStream : TWinSocketStream;
BytesRead ,j: Integer;
Begin
While Not Terminated And ClientSocket.Connected Do
Begin
try
SocketStream := TWinSocketStream.Create(ClientSocket, 60000);
try
FillChar(ReceiveBuffer, 10, 0);
If SocketStream.WaitForData(5000) Then
begin
Repeat
BytesRead := SocketStream.Read(ReceiveBuffer,SizeOf(ReceiveBuffer));
if BytesRead = 0 then
ClientSocket.Close
Else
begin
mystream.WriteBuffer(ReceiveBuffer, BytesRead);
end;
Until Not SocketStream.WaitForData(2000);
end;
finally
SocketStream.Free;
ClientSocket.Close;
mystream.clear;
end;
except
HandleException;
end;
End;
End;