P
powersite
Unregistered / Unconfirmed
GUEST, unregistred user!
遇到一个问题:
Socket发送数据,用NonBlock方式,发送数据,
代码如下:
procedure TBase_Trans.SendData(AStr: String); //发送数据
var
BlockSize: Integer;
P: Integer;
TotalSize: Integer;
SendSize: Integer;
Head: string;
ms: TMemoryStream;
begin
TotalSize := Length(AStr);
Head := format('$$$$$%0.10d$$$$$',[TotalSize]); //先发一个前缀,告诉对方需接收多少字节
SendSize := FSocket.SendBuf(Head[1],20);
ms:=TMemoryStream.Create;
ms.Write(AStr[1],TotalSize);
try
ms.Seek(0,soFromBeginning);
P := 0;
repeat //分块发送数据包
if TotalSize-P>BufferSize then
BlockSize := BufferSize
else
BlockSize := TotalSize-P;
ms.Read(SendBuffer,BlockSize);
repeat
SendSize := FSocket.SendBuf(SendBuffer,BlockSize); //缓冲区内的数据有可能不能一次性递交,须判断
Sleep(50);
until SendSize<>-1; //一直到发送完成才发送下面一部分
P := P+SendSize;
until BlockSize<BufferSize;
finally
ms.Free;
end;
end;
发现发送到一定程度后,经常会在调用SendBuf时,返回值为-1,说明数据队列已满,不能再被递交,好久也不行,此时Socket是仍旧正常连接的,不知是什么原因?
感激涕零!
不好意思,只有40分了,望大侠帮忙啊,
Socket发送数据,用NonBlock方式,发送数据,
代码如下:
procedure TBase_Trans.SendData(AStr: String); //发送数据
var
BlockSize: Integer;
P: Integer;
TotalSize: Integer;
SendSize: Integer;
Head: string;
ms: TMemoryStream;
begin
TotalSize := Length(AStr);
Head := format('$$$$$%0.10d$$$$$',[TotalSize]); //先发一个前缀,告诉对方需接收多少字节
SendSize := FSocket.SendBuf(Head[1],20);
ms:=TMemoryStream.Create;
ms.Write(AStr[1],TotalSize);
try
ms.Seek(0,soFromBeginning);
P := 0;
repeat //分块发送数据包
if TotalSize-P>BufferSize then
BlockSize := BufferSize
else
BlockSize := TotalSize-P;
ms.Read(SendBuffer,BlockSize);
repeat
SendSize := FSocket.SendBuf(SendBuffer,BlockSize); //缓冲区内的数据有可能不能一次性递交,须判断
Sleep(50);
until SendSize<>-1; //一直到发送完成才发送下面一部分
P := P+SendSize;
until BlockSize<BufferSize;
finally
ms.Free;
end;
end;
发现发送到一定程度后,经常会在调用SendBuf时,返回值为-1,说明数据队列已满,不能再被递交,好久也不行,此时Socket是仍旧正常连接的,不知是什么原因?
感激涕零!
不好意思,只有40分了,望大侠帮忙啊,