我正在尝试通过TCP套接字发送文件,但它只发送前1024个字节,我试图查看c#代码但无法弄明白。该文件是二进制格式,如果这有帮助可以让一些人让我知道我错过了什么?
客户端
$test=[io.file]::ReadAllBytes("c:\test");
$socket = New-Object net.sockets.tcpclient('192.168.1.202',8080);
$stream = $socket.GetStream();
$stream.write($test,0,$test.Length);
$writer.flush();
$socket.close()
服务器
#!/usr/bin/env ruby
require 'socket'
begin
server = TCPServer.open(8080)
client = server.accept
recv_length = 1024
out_put = ''
while (tmp = client.recv(recv_length))
out_put += tmp
break if tmp.length < recv_length
end
File.open("test.txt",'w') {|f| f.write(out_put)}
server.close
end
旁注如果我尝试Base64编码$ test它有一个合适的,甚至不会尝试发送任何数据。
编辑:
以下作品。我改变服务器来获取而不是recv现在它只是拉动所有东西并等待客户端关闭套接字。
客户端
$test=[System.Convert]::ToBase64String([io.file]::ReadAllBytes("c:\test"));
$socket = New-Object net.sockets.tcpclient('172.26.4.26',8080);
$stream = $socket.GetStream();
$writer = new-object System.IO.StreamWriter($stream);
$buffer = new-object System.Byte[] 1024;
$writer.WriteLine($test);
$socket.close()
服务器
#!/usr/bin/env ruby
require 'socket'
require 'base64'
begin
server = TCPServer.open(8080)
client = server.accept
out_put = client.gets()
File.open("test","w") {|f| f.write(Base64.decode64(out_put))}
end
答案 0 :(得分:0)
我只能猜测,但它可能与服务器代码有关?!
recv_length = 1024
编辑:
您可以尝试关闭流,也许这就是问题?!
$stream.write($test,0,$test.Length);
$stream.close();
$writer.flush();
答案 1 :(得分:0)
<强>客户端强>
$test=[System.Convert]::ToBase64String([io.file]::ReadAllBytes("c:\test"));
$socket = New-Object net.sockets.tcpclient('172.26.4.26',8080);
$stream = $socket.GetStream();
$writer = new-object System.IO.StreamWriter($stream);
$buffer = new-object System.Byte[] 1024;
$writer.WriteLine($test);
$socket.close()
服务器强>
#!/usr/bin/env ruby
require 'socket'
require 'base64'
begin
server = TCPServer.open(8080)
client = server.accept
out_put = client.gets()
File.open("test","w") {|f| f.write(Base64.decode64(out_put))}
end