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

发布于 2025-01-06 10:35:45 字数 977 浏览 1 评论 0原文

我有一个客户端和服务器程序。客户端发送文件到服务器时,首先将文件转换为字节,然后发送到服务器。然后,服务器将使用它接收到的字节重建文件。我的服务器程序有问题。有时,它接收到的字节不完整。

现在,我已经通过互联网搜索发现这是像我这样的初学者程序员的常见问题。我尝试了各种找到的解决方案,但没有任何效果。 (我已经为此工作了大约 2 天)

我想知道在 LAN 中的两个程序之间发送和接收文件/字节的正确方法是什么? (一个是服务器,另一个是客户端,当然,可以有多个客户端程序连接到服务器程序)

我希望有人可以帮助解决这个问题。请...我希望精通套接字编程和字节的人可以提供有关此的有用信息。

一些额外信息: 实际上,我的代码来自此论坛主题:DANIWEB。通过阅读该线程,该程序运行良好,甚至成功发送了 400MB 以上的视频文件。就我而言,我只发送大小小于 10mb 的小图像和文档文件,而且我的服务器程序失败的次数比成功的次数多。

我已经问了 问题 与这个问题相关,尝试了给我的答案,但我的程序仍然失败。我还在 MSDN 中找到了一些内容 这与我在问题中给出的答案有点相似。我也尝试了,但我的服务器程序仍然失败。

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)

I was wondering what is the proper way of sending and receiving files/bytes between two programs in a 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. Reading through the thread, the program worked perfectly and even managed to send a 400MB+ video file. 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.

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. Tried it as well, but my server program still fails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深爱成瘾 2025-01-13 10:35:45

你说“不完整” - 这通常指向两件事之一:

  • 发送者可能启用了“nagle”,并且最后几个字节可能仍保留在其末端(操作系统/NIC),直到有足够大的数据包可用。含义:即使他们调用发送/写入/等 - 它仍然没有离开客户端计算机。这并不意外。他们可以通过关闭套接字(或至少发送关闭)或通过禁用nagle(设置NoDelay = true)并负责不产生太多碎片(BufferedStream对此很有用)来强制它发送
  • 。并不总是获得完整的帧;在任何 Read/BeginRead/ReceiveAsync/etc 中,您所保证的只是“一些字节”或“EOF”。因此,你必须继续阅读,直到你有一个完整的框架。那么问题是,您的协议如何定义帧?常见的方法是长度前缀(或标头中的长度),或特殊的帧终止序列。

没有代码就不可能解释更多;但是,正如另一个答案中所述 - 它可能有助于将套接字详细信息卸载到库。碰巧的是,我目前正在开发一个项目,我打算很快将其发​​布到 OSS。

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. Meaning: even though they called Send/Write/etc - it still hasn't left the client machine. 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)
  • the receiver does not always get entire frames; at any Read/BeginRead/ReceiveAsync/etc all you are guaranteed is "some bytes" or "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.

只有影子陪我不离不弃 2025-01-13 10:35:45

这个很难回答,我强烈推荐你一个.net平台上的Socket Framework。这将为您节省大量自己开发的时间。
SuperSocket

It's difficult to answer, I strongly recommend you a Socket Framework on .net platform. It will save you lots of time to develop by yourself.
SuperSocket

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文