Lazarus/FreePascal,Synapse如何将文件发送到TCPBlockSocket?

3

我试图制作一个类似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

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

p.s. 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|
1个回答

9

好的... Hex 00 00 B5 6C(反转后的额外字节)等于46444。因此,我的猜测是TFileStream或SendStream在实际数据之前发送要发送的字节数。

查看Synapse的代码,有一个函数:

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

SendStream这样调用它:
  InternalSendStream(Stream, true, false);

因此,WithSize 为 true 时,在实际数据之前会发送流的大小。

如果使用 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; 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接