当SendBuf到ServerSocket时ClientSocket断开连接

时间:2018-09-22 03:06:45

标签: sockets delphi serversocket tclientsocket

晚安,我正在尝试解决问题。 如果我尝试将buf发送到serverocket,则从开始发送几秒钟后我就断开了连接。在服务器套接字上,我看到收到一点,然后从服务器套接字断开连接。

我正在使用客户端/服务器套接字作为ctNonBlocking并通过互联网(数字)ip。<​​/ p>

我的客户端发送功能是:

var
  Png     : TPngImage;
  ms      : TMemoryStream;
  Desktop : HDC;
  Tam     : Int64;
begin
  Result  := TBitmap.Create;
  Desktop := GetDC(0);

  try
    try
      Result.PixelFormat  := pf4bit;
      Result.Width        := Screen.Width;
      Result.Height       := Screen.Height;
      BitBlt(Result.Canvas.Handle, 0, 0, Result.Width, Result.Height, Desktop, 0, 0, SRCCOPY);
      Result.Modified := True;
    finally
      ReleaseDC(0, Desktop);
    end;
  except
    Result.Free;
    Result := nil;
  end;

  ms := TMemoryStream.Create;
  Result.SaveToStream(ms);
  ms.Position := 0;

      Tam := ms.Size;   
//send size to server
Form1.Cliente.Socket.SendText(Format('size@%d#', [Tam]));  
Sleep(1000);
//send buf    
Form1.Cliente.Socket.SendBuf(ms.Memory^, ms.Size);
ms.Free;

在服务器端,我正在使用以下代码来接收屏幕截图:

var
  s           : String;
  ss          : UnicodeString;
  rr          : TArray<string>;

  Info        : TShellExecuteInfo;
  MyFilename  : string;

  ClientData  : ^TClientData;

  Buffer      : array [0..9999] of Char;
  RecievedLen : integer;
begin
  ClientData := Socket.Data;

  if ClientData.ScreenShot then begin
    if ClientData.Arquivo = nil then begin
      ClientData.Arquivo      := TMemoryStream.Create;
      ClientData.NomeArquivo  := Format('%sScreenShots\ScreenShot_%d_%s.png', [ExtractFilePath(ParamStr(0)), GetTickCount, ClientData.HWID]);
    end;

    RecievedLen := socket.ReceiveBuf(Buffer, Sizeof(Buffer));
    ClientData.Arquivo.Write(Buffer, RecievedLen);

    if ClientData.Arquivo.Size >= ClientData.TamanhoArquivo then ClientData.Arquivo.SaveToFile(ClientData.NomeArquivo);
  end;

请帮助。

0 个答案:

没有答案