Socket问题请教!(50分)

  • 主题发起人 主题发起人 没理头号
  • 开始时间 开始时间

没理头号

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方式时,是不是每有一个连接服务端就开一个线程还是永远只有一个线程(但是独立于主线程)来处理客户请求!
 
把你的代码发给我,让我看看。

yjliu74@sohu.com

 
真不知是服务端的问题还是客户端的问题,
我头都大了,没人吗?
 
Sorry! It is my fault!
Problem is solve!
 
服务端用stThreadBlocking方式,客户端仍用ctNonBlocking方式

这就叫驴唇不对马嘴!!!

服务端都用stThreadBlocking方式了,客户端为什么不用ctBlocking方式
 
后退
顶部