没
没理头号
Unregistered / Unconfirmed
GUEST, unregistred user!
SOCKET控件当服务端用stThreadBlocking方式时,客户端是不是也要相应改变;
我做一测试程序,服务端用stThreadBlocking方式,客户端仍用ctNonBlocking方式,
在单独连一台机器时很正常,但当我连的机器多时,服务器方好象总是处理一个线程
好象要等很长时间才会响应其它线程,不知为什么?
服务段代码如下:
procedure TMainFrm.sSocketGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread := TSSServerThread.Create(False, ClientSocket);
end;
procedure TSSServerThread.ClientExecute;
var
Data: array[0..2047] of char;
RecText: string;
SocketStream: TWinSocketStream;
cpos: integer;
begin
while not Terminated and ClientSocket.Connected do
try
if (CList.IndexOf(ClientSocket.RemoteAddress) < 0) then
begin
ClientSocket.SendText('1001');
CList.Add(ClientSocket.RemoteAddress);
end;
//超过5000指定时间读不到数据就退出?
SocketStream := TWinSocketStream.Create(ClientSocket, 5000);
try
FillChar(Data, SizeOf(Data), '8');
//每次读取指定长度的数据,如果只有一半数据呢?继续等待?
if SocketStream.Read(Data, SizeOf(Data)) = 0 then
begin
// If we didn't get any data after xx seconds then close the connection
ClientSocket.SendText('Timeout on Server' + #13#10);
//Wait a little time to allow sending of text before disconnect
sleep(1);
cpos := CList.IndexOf(ClientSocket.RemoteAddress);
if (cpos >= 0) then
CList.Delete(cpos);
ClientSocket.Close;
Terminate;
end;
RecText := Data;
if ClientSocket.Connected and (SSHQVal <> preSSHQVal) then
begin
ClientSocket.SendText(RecText);
preSSHQVal := SSHQVal;
LogStr := 'Send to' + ClientSocket.RemoteAddress;
Synchronize(WLog);
end;
finally
SocketStream.Free;
end;
sleep(10);
except
HandleException;
end;
end;
客户端:
procedure TMainFrm.CSocketRead(Sender: TObject; Socket: TCustomWinSocket);
var
cstr: string;
begin
cstr := Socket.ReceiveText;
if (cstr = '1001') then IsOnLine := true;
self.Memo1.Lines.Add(cstr);
inc(rcount);
self.Label2.Caption :=inttostr(rcount);
end;
我做一测试程序,服务端用stThreadBlocking方式,客户端仍用ctNonBlocking方式,
在单独连一台机器时很正常,但当我连的机器多时,服务器方好象总是处理一个线程
好象要等很长时间才会响应其它线程,不知为什么?
服务段代码如下:
procedure TMainFrm.sSocketGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
SocketThread := TSSServerThread.Create(False, ClientSocket);
end;
procedure TSSServerThread.ClientExecute;
var
Data: array[0..2047] of char;
RecText: string;
SocketStream: TWinSocketStream;
cpos: integer;
begin
while not Terminated and ClientSocket.Connected do
try
if (CList.IndexOf(ClientSocket.RemoteAddress) < 0) then
begin
ClientSocket.SendText('1001');
CList.Add(ClientSocket.RemoteAddress);
end;
//超过5000指定时间读不到数据就退出?
SocketStream := TWinSocketStream.Create(ClientSocket, 5000);
try
FillChar(Data, SizeOf(Data), '8');
//每次读取指定长度的数据,如果只有一半数据呢?继续等待?
if SocketStream.Read(Data, SizeOf(Data)) = 0 then
begin
// If we didn't get any data after xx seconds then close the connection
ClientSocket.SendText('Timeout on Server' + #13#10);
//Wait a little time to allow sending of text before disconnect
sleep(1);
cpos := CList.IndexOf(ClientSocket.RemoteAddress);
if (cpos >= 0) then
CList.Delete(cpos);
ClientSocket.Close;
Terminate;
end;
RecText := Data;
if ClientSocket.Connected and (SSHQVal <> preSSHQVal) then
begin
ClientSocket.SendText(RecText);
preSSHQVal := SSHQVal;
LogStr := 'Send to' + ClientSocket.RemoteAddress;
Synchronize(WLog);
end;
finally
SocketStream.Free;
end;
sleep(10);
except
HandleException;
end;
end;
客户端:
procedure TMainFrm.CSocketRead(Sender: TObject; Socket: TCustomWinSocket);
var
cstr: string;
begin
cstr := Socket.ReceiveText;
if (cstr = '1001') then IsOnLine := true;
self.Memo1.Lines.Add(cstr);
inc(rcount);
self.Label2.Caption :=inttostr(rcount);
end;