【问题标题】:Lazarus/FreePascal, Synapse send file to TCPBlockSocketLazarus/FreePascal, Synapse 发送文件到 TCPBlockSocket
【发布时间】:2015-08-27 00:36:33
【问题描述】:

我尝试制作一个模拟 nc (netcat) 工具。 打开文件并将其发送到 TCP 套接字。

客户端(发送者)端

procedure TForm1.SpeedButton3Click(Sender: TObject);
var Client:TTCPBlockSocket;
    FS: TFileStream;
begin
  Client:=TTCPBlockSocket.Create;
  Client.RaiseExcept:=True;
  Client.Connect('192.168.1.95','9999');
  FS:=TFileStream.Create('/tmp/test_print.pdf',fmOpenRead);
  FS.Position:=0;
  Client.SendStream(FS);
  FS.Free;
  Client.CloseSocket;
  Client.Free;
end;        

服务器(接收方)端

nc -l 192.168.1.95 9999 > test.pdf

运行后我得到 test.pdf 但 md5sum 无效

fe2e5d86acd267ca39a9c15c456e1be0  /tmp/test_print.pdf
34d755aa81d72f525a5e691e98bed283  test.pdf

大小也不一样

-rw-rw---- 1 user1 DomainUsers 46444 Июн  8 17:11 /tmp/test_print.pdf
-rw-r--r-- 1 user1 DomainUsers 46448 Июн 11 15:40 test.pdf

我做错了什么?我的错在哪里?

附言test.pdf 开头多出 4 个字节 (6c,b5,00,00)

[user1@pandora6 /tmp]$ hexdump -C test.pdf | head -5
00000000  6c b5 00 00 25 50 44 46  2d 31 2e 34 0a 25 c7 ec  |l...%PDF-1.4.%..|
00000010  8f a2 0a 34 20 30 20 6f  62 6a 0a 3c 3c 2f 4c 69  |...4 0 obj.<</Li|
00000020  6e 65 61 72 69 7a 65 64  20 31 2f 4c 20 34 36 34  |nearized 1/L 464|
00000030  34 34 2f 48 5b 20 34 33  39 36 38 20 31 34 32 5d  |44/H[ 43968 142]|
00000040  2f 4f 20 36 2f 45 20 34  33 39 36 38 2f 4e 20 31  |/O 6/E 43968/N 1|

[user1@pandora6 /tmp]$ hexdump -C /tmp/test_print.pdf | head -5
00000000  25 50 44 46 2d 31 2e 34  0a 25 c7 ec 8f a2 0a 34  |%PDF-1.4.%.....4|
00000010  20 30 20 6f 62 6a 0a 3c  3c 2f 4c 69 6e 65 61 72  | 0 obj.<</Linear|
00000020  69 7a 65 64 20 31 2f 4c  20 34 36 34 34 34 2f 48  |ized 1/L 46444/H|
00000030  5b 20 34 33 39 36 38 20  31 34 32 5d 2f 4f 20 36  |[ 43968 142]/O 6|
00000040  2f 45 20 34 33 39 36 38  2f 4e 20 31 2f 54 20 34  |/E 43968/N 1/T 4|

【问题讨论】:

    标签: delphi freepascal lazarus netcat synapse


    【解决方案1】:

    嗯... Hex 00 00 B5 6C(你的额外字节反向)等于 46444。所以我的猜测是 TFileStream 或 SendStream 在实际数据之前发送它想要发送的字节数。

    看Synapse的代码有功能:

    procedure TBlockSocket.InternalSendStream(const Stream: TStream; WithSize, Indy: boolean);
    

    SendStream 是这样称呼它的:

      InternalSendStream(Stream, true, false);
    

    所以WithSize 为真,流的大小在实际数据之前发送。

    如果您使用 SendStreamRaw,所有发送的内容都没有前面的大小。

    procedure TForm1.SpeedButton3Click(Sender: TObject);
    var Client:TTCPBlockSocket;
        FS: TFileStream;
    begin
      Client:=TTCPBlockSocket.Create;
      Client.RaiseExcept:=True;
      Client.Connect('192.168.1.95','9999');
      FS:=TFileStream.Create('/tmp/test_print.pdf',fmOpenRead);
      FS.Position:=0;
      Client.SendStreamRAW(FS);
      FS.Free;
      Client.CloseSocket;
      Client.Free;
    end; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      相关资源
      最近更新 更多