Delphi---TServerSocket和TClientSocket发送和接收大数据包

https://www.cnblogs.com/zhangzhifeng/p/6065244.html

TServerSocket和TClientSocket用非阻塞模式发送和接收比较大的数据时,可能一次会接收不完,此时需要通过循环接收。

//接收

procedure TMAINFORM.TCPServer1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
iLength,I,iStart, iRevCount: Integer;
iReceived: Integer;
buff: array of byte;
S:STRING;
begin
S:='';
iStart := 0;
iLength:= Socket.ReceiveLength;
iRevCount := iLength; //这里加入的
SetLength(buff, iLength);
MEMO1.Lines.Add(INTTOSTR(ILENGTH));
while iLength>0 do
begin
iReceived := Socket.ReceiveBuf(buff[iStart], iLength); //这里修改了注意
iStart := iStart + iReceived ; //这里加入的
iLength := iLength - iStart;
end;
//下面你的iLength 使用 iRevCount 替换了
FOR I:=0 TO iRevCount -1 DO
BEGIN
S:=S+INTTOSTR(BUFF[I]);
END;
MEMO1.Lines.Add(S);
buff:= nil;
end;

  

发送:Socket.SendBuf

上一篇:hdu 1241 Oil Deposits(DFS求连通块)


下一篇:Linux设置定时任务