有多连接的Tserversocket如何正确接收数据?(100分)

W

weiky

Unregistered / Unconfirmed
GUEST, unregistred user!
我的服务端有20几个连接。假设发送的消息包为
type TMsg=record
iSize:interger; //这个包大小。
strBigString:array[0..300000]; //很大的一个字符串数组
end;
我在TServersocket的OnClientRead()函数里的程序为:
先Socket.ReceiveBuf(a,4)一个4字节的TMsg.iSize;
再用一个while循环将剩下的TMsg.strBigString用Socket.ReceiveBuf(a,TMsg.iSize-4)接收下来。
但这样,若有多个客户端同时发送这个包时,在循环里会收到不同客户端的数据。
如何,将一个客户端的一个包完整地接收下来呢?
我的TServersocket用的是stNonBlocking方式。
谢谢!!!
 
在TServersocket的OnClientRead()函数里:
TMsg:= Socket.Data ;
具体代码。
Socket.Data := TMsg;
 
honestman大侠能否具体解释下,看得不大懂也。
 
就是把TMsg作为每个线程的局部变量。
这样需要的时候就从线程的内存中取出来,用完就放回去保存起来,下次再用。
 
关键是我每次接收的包不一定一样的结构,每种消息都对应一种结构。这样的话,我不知道该用
哪个结构来=socket.data。另外,线程是指对应的socket?
 
看:
TCon = record
FileStream : TFileStream;
curSize : Integer;
end;
PCON = ^TCON;

procedure TFormmain.SS_mainClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
cmd :String;
C : PCON;
buf :array[0..(bufsize-1)] of byte;
bufcount,i :integer;
begin
C:= Socket.Data ;
GetFilec(cmd,c);//我自己的。
Socket.Data := c;
end;

/////////////////////////////////////////////////////////////
Points to application-specific data associated with the socket.

property Data: Pointer;

Description

Use Data to associate information specific to the socket connection with the
Windows socket object. For example, Data can be used to store access and
authentication information used by a server socket to evaluate client
connection requests.

Data can store information in a thread-safe manner that would otherwise require
the use of global variables. This is useful given the multi-threaded nature of
many client-server applications.
 
var
Len: Integer;
RecBuf:array of char;
begin
Len:=Socket.ReceiveLength;
SetLength(RecBuf, Len);
Socket.ReceiveBuf(RecBuf, Len);
{上一句出错,如果在定义时用: RecBuf:array[1..2048] of char;(静态数组,
并隐藏:SetLength(RecBuf, Len);)则可以,请问这是为什么??}
...
end;
 
可为什么我在客户端收这个DATA时,是NIL呢?

到底它应该怎么用?
 
顶部