服务器传输文件损坏,当服务器和客户端在1台PC上运行时,TCP传输文件有效,但在2台PC上运行时,TCP传输文件损坏...

So I'm trying to make a program which sends a file that I want to print from my client in my main PC, to my 2nd PC that runs the server and that is connected to a printer.

When I was testing my code I ran the client and server on my main pc and it worked fine. However, when I ran the client on my main PC and the server on my 2nd PC the file was corrupted and I'm not sure why.

Here's my Listener (I removed the parts that I thought were unnecessary):

void Listener()

{

//All of these strings and bools are filled correctly I just removed it because its long

string file="";

string size = "";

bool print;

Socket server = myList.AcceptSocket();

var output = File.Create(file);

Console.WriteLine("Client connected. Starting to receive the file");

int k = 0;

int read = 0;

byte[] buffer = new byte[long.Parse(size)];

NetworkStream stm = new NetworkStream(server);

while ((k = stm.Read(buffer, 0, buffer.Length-read)) > 0)

{

read += k;

}

output.Write(buffer, 0, buffer.Length);

output.Close();

if (print) { PrintFile(file); }

server.Close();

Thread.CurrentThread.Abort();

}

Here is the client code (I removed the parts that I thought were unnecessary):

void startClient()

{

FileInfo f = new FileInfo(file);

Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

client.Connect(IPAddress.Parse("10.0.0.13"), 27015);

Console.WriteLine("Connected");

byte[] bytes = File.ReadAllBytes(file);

client.SendFile(file);

Console.WriteLine("File Sent");

client.Close();

}

Does anybody know how to solve this? Thanks in advance!

Talk1:

Is there an error or are the clients just unconnected? Are you sure the ip and the port are right? Maybe you can use the DNS instead of an ip. See stackoverflow.com/questions/14838421/…

Talk2:

not relevant at all. OP mentions the connection is fine, just that the file becomes corrupted.

Solutions1

You don't apply the read offset to the buffer, starting to write at index 0 at every NetworkStream.Read() call. On local, or when testing with smaller files, this will work fine as everything will arrive in one Read(). On a real network or when handling larger files, you'll find that you need multiple Read() calls to read all data.

So change it to:

stm.Read(buffer, read, buffer.Length-read)

You also may want to reconsider reading the entire file in memory at once. You might want to write it to disk in the meantime, especially since allocating large arrays can cause an OutOfMemoryException way earlier than you expect.

Also consider using existing networking protocols as opposed to rolling your own. Besides this very basic issue, you're prone to running into many other common socket pitfalls.

Talk1:

Wow, it worked Thanks! I'm really new to networking, so this stuff is kinda very hard for me but I'm willing to put my time into learning it. Thanks again!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值