0

这是我服务器的编码

procedure TForm1.IdTCPServerExecute(AThread: TIdPeerThread);
var
  InputString: string;
  ACommand: string[1];
  AFileName: string;
  ATempFileName: string;
  AFileStream: TFileStream;
begin
  InputString := UpperCase(AThread.Connection.ReadLn);
  ACommand := Copy(InputString, 1, 1);
  AFileName := FPicFilePath + Copy(InputString, 2, 5) + '.jpg';

  if ACommand = 'R' then begin
    AFileStream := TFileStream.Create(AFileName, fmOpenRead + fmShareDenyNone);

    try
      AThread.Connection.WriteStream(AFileStream, true, true);
    finally
      AFileStream.Free;
    end;
  end else if ACommand = 'S' then begin
    ATempFileName := FPicFilePath + 'TEMP.jpg';

    if FileExists(ATempFileName) then
      DeleteFile(ATempFileName);

    AFileStream := TFileStream.Create(ATempFileName, fmCreate);

    try
      AThread.Connection.ReadStream(AFileStream, -1, false);
      //RenameFile(ATempFileName, AFileName);
    finally
      AFileStream.Free;
    end;
  end;

  AThread.Connection.Disconnect;
end;

这是我客户的编码

procedure TForm1.SendImageToServer(ASendCmd: string);
var
  AFileStream: TFileStream;
begin
  MessageDlg('Sending ' + ASendCmd + ' :' + FSendFileName, mtInformation, [mbOK], 0);
  Screen.Cursor := crHourGlass;

  with IdTCPClient do begin
    if Connected then Disconnect;
    Host := '127.0.0.1';
    Port := 2108;

    AFileStream := TFileStream.Create(FSendFileName, fmOpenRead);

    try
      try
        Connect;
        try
          WriteLn(ASendCmd);
          WriteStream(AFileStream, true, false);
        finally
          Disconnect;
        end;
      finally
        AFileStream.Free;
      end;
    except
    end;
  end;

  Screen.Cursor := crDefault;
end;

我可以成功地从服务器获取图像,但是当我必须将新图像发送回服务器时,我只有一个空的 TEMP.jpg。

请帮忙。谢谢。

德尔福 5,印地 9

4

1 回答 1

0

当从客户端向服务器发送文件时,客户端并没有告诉WriteStream()发送流大小,但服务器告诉ReadStream()期望流大小到达,所以你有一个不匹配。

当从服务器向客户端发送文件时,服务器告诉WriteStream()发送流大小,客户端告诉ReadStream()期望流大小到达,所以没有不匹配。

于 2013-06-28T20:53:51.057 回答