关于TIndyTCPClient/TIndyTCPServer在C/S架构中的数据传输问题(高手请进),在线等待 ( 积分: 50 )

  • 主题发起人 主题发起人 loadme
  • 开始时间 开始时间
L

loadme

Unregistered / Unconfirmed
GUEST, unregistred user!
服务器端的TIndyTCPServer的OnExecute事件
pausestr:=athread.Connection.CurrentReadBuffer;
if pausestr='' then exit;
edtlen.Text:='需要接收到'+trim(copy(pausestr,2,6))+'字节xml数据';
pausestr的copy(pausestr,2,6)代表需要接收的字节数,
但是为什么数据量超过8k的时候,总是不能正确的接收数据,CurrentReadBuffer是什么意思,OpenWriteBuffer()是什么意思,writebuffer(msendbuf,msendlen,true)这里的true是什么意思。在线等待
 
服务器端的TIndyTCPServer的OnExecute事件
pausestr:=athread.Connection.CurrentReadBuffer;
if pausestr='' then exit;
edtlen.Text:='需要接收到'+trim(copy(pausestr,2,6))+'字节xml数据';
pausestr的copy(pausestr,2,6)代表需要接收的字节数,
但是为什么数据量超过8k的时候,总是不能正确的接收数据,CurrentReadBuffer是什么意思,OpenWriteBuffer()是什么意思,writebuffer(msendbuf,msendlen,true)这里的true是什么意思。在线等待
 
writebuffer(msendbuf,msendlen,true)这里的true是开始写的意思
具体的格式:procedure writebuffer(const ABuffer;ABytecount:longint;const AWriteNow:boolean=false);
 
to sdjxmikill:
如果这里的true时开始写的意思,那么false是什么意思呢
 
换个事件试试看

ServerSocketClientRead
 
to cangwu_lee:
没有你说的那个事件呀,你说的不是indy控件吧
 
是的,FastNet 里面的。
我觉得, Server端使用 ServerSocket、客户端使用TIdTcpClient 配合不错
 
CurrentReadBuffer 读取缓冲区目前所有数据
OpenWriteBuffer 强制发送缓冲区目前所有数据,并清空缓冲队列
writebuffer true和false同样是指是否立即发送缓冲区数据。

//对writebuffer我也不太理解,代码贴上来大家瞧瞧
procedure TIdTCPConnection.WriteBuffer(const ABuffer; AByteCount: Integer;
const AWriteNow: boolean = false);
var
LBuffer: TIdSimpleBuffer;
nPos, nByteCount: Integer;
begin
if (AByteCount > 0) and (@ABuffer <> nil) then begin
// Check if disconnected
CheckForDisconnect(True, True);
if connected then begin
if (FWriteBuffer = nil) or AWriteNow then begin
LBuffer := TIdSimpleBuffer.Create; try
LBuffer.WriteBuffer(ABuffer, AByteCount);
if Assigned(Intercept) then begin
LBuffer.Position := 0;
Intercept.Send(LBuffer);
AByteCount := LBuffer.Size;
end;
nPos := 1;
repeat
nByteCount := IOHandler.Send(PChar(LBuffer.Memory)[nPos - 1], LBuffer.Size - nPos + 1);
// Write always does someting - never retuns 0
// TODO - Have a AntiFreeze param which allows the send to be split up so that process
// can be called more. Maybe a prop of the connection, MaxSendSize?
TIdAntiFreezeBase.DoProcess(False);
FClosedGracefully := nByteCount = 0;

// Check if other side disconnected
CheckForDisconnect;
// Check to see if the error signifies disconnection
if GStack.CheckForSocketError(nByteCount
, [ID_WSAESHUTDOWN, Id_WSAECONNABORTED, Id_WSAECONNRESET]) then begin
DisconnectSocket;
GStack.RaiseSocketError(GStack.WSGetLastError);
end;
DoWork(wmWrite, nByteCount);
nPos := nPos + nByteCount;
until nPos > AByteCount;
finally FreeAndNil(LBuffer); end;
// Write Buffering is enabled
end else begin
FWriteBuffer.WriteBuffer(ABuffer, AByteCount);
if (FWriteBuffer.Size >= FWriteBufferThreshhold) and (FWriteBufferThreshhold > 0) then begin
// TODO: Maybe? instead of flushing - Write until buffer is smaller than Threshold.
// That is do at least one physical send.
FlushWriteBuffer(FWriteBufferThreshhold);
end;
end;
end
else
begin
Raise EIdNotConnected.Create(RSNotConnected);
end;
end;
end;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
937
SUNSTONE的Delphi笔记
S
后退
顶部