如何使用套接字正确发送和接收字节/文件?
我有一个客户端和服务器程序。客户端发送文件到服务器时,首先将文件转换为字节,然后发送到服务器。然后,服务器将使用它接收到的字节重建文件。我的服务器程序有问题。有时,它接收到的字节不完整。
现在,我已经通过互联网搜索发现这是像我这样的初学者程序员的常见问题。我尝试了各种找到的解决方案,但没有任何效果。 (我已经为此工作了大约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说“不完整” - 这通常指向两件事之一:
没有代码就不可能解释更多;但是,正如另一个答案中所述 - 它可能有助于将套接字详细信息卸载到库。碰巧的是,我目前正在开发一个项目,我打算很快将其发布到 OSS。
You say "incomplete" - this usually points to one of two things:
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.
这个很难回答,我强烈推荐你一个.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