INdy(IdTCPServer1,IdTCPClient1)服务器Server端向Client端返回数据出错,帮帮忙,急!(100分)

  • 主题发起人 主题发起人 200103067
  • 开始时间 开始时间
2

200103067

Unregistered / Unconfirmed
GUEST, unregistred user!
Server端代码:
procedure TFrmServer.IdTCPServer1Execute(AThread: TIdPeerThread);
var
RecvStr: string;
RecvStream,SendStream : TMemoryStream;
begin
RecvStream := TMemoryStream.Create;
AThread.Connection.ReadStream(RecvStream, -1, true);
RecvStream.Position := 0;
SetLength(RecvStr, RecvStream.size);
LbLog.Lines.Add('收到的数据:'+ RecvStr);
RecvStream.Free;

SendStream := TMemoryStream.Create;
SendStream.LoadFromFile(ExtractFilePath(paramstr(0)) +ServerToClientStream.dat);
SendStream.Position := 0;
AThread.Connection.WriteStream(SendStream);
SendStream.Free;

AThread.Connection.DisConnect;

end;

Client端代码:
procedure TClientHandleThread.HandleShow;
var
RecvStream: TMemoryStream;
RecvStr:string;
begin
RecvStream := TMemoryStream.Create;
FrmClient.IdTCPClient1.ReadStream(RecvStream, -1, true);
RecvStream.Position := 0;
SetLength(RecvStr, RecvStream.size);
FrmClient.Memo1.Text :='收到数据'+RecvStr;
RecvStream.Free;
end;

procedure TClientHandleThread.Execute;
begin
while not Terminated do
begin
if FrmClient.IdTCPClient1.Connected then
begin
Synchronize(HandleShow);
Sleep(100);
end;
end;
end;

procedure TFrmClient.SendToServerClick(Sender: TObject);
var
MyStream: TMemoryStream;
begin
ClientHandleThread := TClientHandleThread.Create(False);
MyStream := TMemoryStream.Create;
MyStream.LoadFromFile(ExtractFilePath(paramstr(0))+ClientToServerStream.dat);
MyStream.Position := 0;
IdTCPClient1.Connect;
IdTCPClient1.writestream(MyStream);
IdTCPClient1.Disconnect;
MyStream.Free;
end;
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Server端AThread.Connection.WriteStream(SendStream);出错,好象是说未连接,如果去掉Client端的IdTCPClient1.Disconnect; Server器端无法接收数据,谢谢赐教!!!
 
有些看不明白你的做法了。你这样试试服务端。客户端连接以后别马上端掉。
另外,你下载一个indy的demo包研究研究。
procedure TFrmServer.IdTCPServer1Execute(AThread: TIdPeerThread);
var
RecvStr: string;
RecvStream,SendStream : TMemoryStream;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
RecvStream := TMemoryStream.Create;
AThread.Connection.ReadStream(RecvStream, -1, true);
RecvStream.Position := 0;
SetLength(RecvStr, RecvStream.size);
LbLog.Lines.Add('收到的数据:'+ RecvStr);
RecvStream.Free;

SendStream := TMemoryStream.Create;
SendStream.LoadFromFile(ExtractFilePath(paramstr(0)) +ServerToClientStream.dat);
SendStream.Position := 0;
AThread.Connection.WriteStream(SendStream);
SendStream.Free;
end;
// AThread.Connection.DisConnect;

end;
 
后退
顶部