简体   繁体   English

如何使用套接字正确发送和接收字节/文件?

[英]How to properly send and receive bytes/files using sockets?

I have a client and server program. 我有一个客户端和服务器程序。 The client sends a file to the server by first converting the file to bytes and then send it to the server. 客户端首先将文件转换为字节,然后将其发送到服务器,从而将文件发送到服务器。 The server will then reconstruct the file using the bytes it receives. 然后,服务器将使用它接收的字节重建文件。 I am having a problem with the server program. 我遇到了服务器程序的问题。 Sometimes, the bytes it receives are incomplete. 有时,它收到的字节不完整。

Now, I have already searched through the internet and found out that it is a common problem among beginner programmers like myself. 现在,我已经在互联网上搜索了,发现这是像我这样的初学者程序员中的常见问题。 I have tried various solutions that I found but nothing worked. 我尝试了各种解决方案,但是没有用。 (I've been working on this for about 2 days already) (我已经做了大约2天了)

I was wondering what is the proper way of sending and receiving files/bytes between two programs in a LAN? 我想知道在LAN中的两个程序之间发送和接收文件/字节的正确方法是什么? (One being the server and the other is the client, though of course, there can be more than 1 client program that will connect to the server program) (当然,一个是服务器,另一个是客户端,但是当然可以有多个客户端程序连接到服务器程序)

I hope someone can help solve this problem. 我希望有人可以帮助解决这个问题。 Please... I hope someone who is well-versed in socket programming and about bytes can provide helpful information on this. 请...我希望精通套接字编程和字节的人可以提供有用的信息。

Some extra information: I actually based my code from this forum topic: DANIWEB . 一些额外的信息:我实际上是基于这个论坛主题的代码: DANIWEB Reading through the thread, the program worked perfectly and even managed to send a 400MB+ video file. 通过线程阅读,该程序运行良好,甚至设法发送400MB +视频文件。 In my case, I'm only sending small images and document files less than 10mb in size and my server program fails more often that it succeeds. 就我而言,我只发送小于10mb的小图像和文档文件,而我的服务器程序失败通常会失败。

I already asked a question related to this problem, tried the answer given to me but my program still fails. 我已经问了一个问题,涉及到这个问题,试图回答给我,但我的程序仍然失败。 I also found something in MSDN that is a little similar to the answer given to me in my question. 我也在MSDN中发现了一些与我在问题中给出的答案有点类似的东西。 Tried it as well, but my server program still fails. 尝试过,但我的服务器程序仍然失败。

You say "incomplete" - this usually points to one of two things: 您说“不完整”-这通常指向两件事之一:

  • the sender may have "nagle" enabled, and the last few bytes may still be held at their end (OS/NIC) until a large enough packet is available. 发送方可能已启用“ nagle”,并且最后几个字节可能仍保留在其末尾(OS / NIC),直到有足够大的数据包可用为止。 Meaning: even though they called Send/Write/etc - it still hasn't left the client machine. 含义:即使他们调用了Send / Write / etc-它仍然没有离开客户端计算机。 This is not unexpected. 这并不意外。 They can force it to send by closing the socket (or at least, a send-shutdown), or by disabling nagle (set NoDelay = true) and taking responsibility for not fragmenting too much (BufferedStream can be useful for this) 他们可以通过关闭套接字(或者至少是发送关闭),或者通过禁用nagle(设置NoDelay = true)来强制它发送,并承担不分段太多的责任(BufferedStream对此有用)
  • the receiver does not always get entire frames; 接收器并不总是得到整个帧; at any Read/BeginRead/ReceiveAsync/etc all you are guaranteed is "some bytes" or "EOF". 在任何Read / BeginRead / ReceiveAsync / etc中,您所保证的都是“某些字节”或“ EOF”。 As such, you must keep reading until you have a compete frame. 因此,您必须继续阅读,直到您有竞争框架。 The question then, is, how is a frame defined by your protocol? 那么问题是,协议如何定义帧? Common approaches are length-prefixed (or length in a header), or a special frame-termination sequence 常见的方法是长度固定(或标头中的长度)或特殊的帧终止序列

It is impossible to interpret more without code; 没有代码就不可能解释得更多; however, as noted in another answer - it may help to offload the socket details to a library. 但是,如另一个答案中所述-可能有助于将套接字详细信息卸载到库中。 As it happens, I'm working on one currently that I intend to release to OSS pretty soon. 碰巧的是,我正在研究一个打算很快发布到OSS的程序。

It's difficult to answer, I strongly recommend you a Socket Framework on .net platform. 很难回答,我强烈建议您在.net平台上使用Socket Framework。 It will save you lots of time to develop by yourself. 它将为您节省大量的时间来自己开发。 SuperSocket SuperSocket

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM