TCP / IP连接中断包(套接字编程) [英] Broken Packet recieve in TCP/IP connection(Socket Programming)

查看:106
本文介绍了TCP / IP连接中断包(套接字编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在研究客户端/服务器应用程序,这里服务器正在列出来自客户端的多个请求。我已经使用了Dot Net套接字编程来实现它。除了一段时间之外,一切正常我的数据包被破坏了。我用Google搜索得到了答案,但我的运气对我没有多大帮助。
我收到的数据包具有明确定义的格式(即结束分隔符)。所以我的问题是:

1)组合损坏的数据包的最佳方法是什么。
2)在实时场景中,我的服务器应用程序将一次收到大量请求(如我正在使用异步套接字编程),所以在这种情况下,如果客户端应用程序同时发送数据包'Hi,C#|'和'Hello,Java |'并且这些数据包收到我的服务器处于损坏状态。
所以我的逻辑可以将这些数据包加入'Hi,Java |'或'Hello,C#|'这是错误的。
3)收到破坏的数据包是常见的问题在套接字编程中使用的TCP / IP协议。

感谢


解决方案

< blockquote> TCP是一种基于流的协议,因此当您使用NetworkStreamReader从套接字读取时,您实际上看不到各个数据包。如果你知道你希望在每条消息中收到多少字节,你应该尝试阅读那么多。如果read方法接收的字节数少于您期望的字节数,则在循环中执行读取,直到收到所有预期的字节。



Hi everyone,
  I am working on client/server application, here server is listining multiple request from client.I have used Dot Net socket programming to achieve it.Every thing works fine except some time,my data packet get broken.I googled it to get the answer but my luck not helped me much.
       The packet that I am recieving is having well defined format(i.e end delimiter). So my questions are:

1)What is the best way to combine the broken packets.
2)In real time scenerio, my server application will recieve lots of request at a time( as i am using asynchronous socket programming), so in this case can it is possible that broken packets combine wrongly means if  client application sends  data packets 'Hi,C#|' and 'Hello,Java|' at a same time and these packet recieved at my server in broken state.
So my logic  may join these packet as 'Hi,Java|' or 'Hello,C#|' which is wrong.
3)Is recieving of broken data packet is common problem in TCP/IP protocol used in Socket Programming.

Thanks


解决方案

TCP is a stream based protocol so you dont actually see the individual packets when you use a NetworkStreamReader to read from the socket. If you know how may bytes you expect to receive in each message, you should try and read that many. If fewer bytes are received by the read method than what you would expect, perform the read in a loop till all the expected bytes are received.



这篇关于TCP / IP连接中断包(套接字编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆