使用单个udp套接字send()和receive()时数据冲突的可能性? [英] Chances of data collision when using single udp socket for send() and receive()?

查看:213
本文介绍了使用单个udp套接字send()和receive()时数据冲突的可能性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个udp应用程序,其中单个服务器能够处理x个客户端。在我的服务器内部,有一个主线程继续从客户端继续接收请求/数据,并将每个客户端的信息保存在列表中。



尽快请求来自新客户端(即来自其信息未保存在列表中的客户端),启动新线程并创建单独的套接字以服务该特定客户端。换句话说,对于每个客户端,都有一个单独的套接字和一个单独的专用线程。



现在,我预见的问题是,因为只有一个套接字即单行通信,如果服务器和客户端同时发送数据,则有可能发生数据冲突。



我该怎样迎合这种情况一个情况?等待响应:(

解决方案

这取决于您期望的客户端数量以及发送大量消息。

问题是UDP不保证自己传输数据,因此可能会丢弃一些数据包。

http://technet.microsoft.com/en-us/library/cc785220(v = ws.10).aspx [ ^ ]

同步数据通过发送方和接收方发送这两者对于大量连接至关重要。即使您在UDP上创建自己的协议层来计算发送的消息数,CRC等以减少数据丢失,也会出现最大线程数的问题。

因此,如果您的解决方案以正确的方式设计,十分之一的客户端将没有问题,但仍有可能发生洪水。

UDP应该是足够可靠,快速连接,实现简单,但在某些情况下,我更喜欢完成端口。

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365198(v=vs.85).aspx [ ^ ]

I have developed a udp application in which a single server is able to handle ''x'' number of clients. Inside my server, there is a primary thread keeps on receiving requests/data continuously from the clients and keeps saving the information of each client in a list.

As soon as a request comes from a new client(i.e. from a client that whose information is not saved in the list), a new thread is launched and a separate socket is created for serving that specific client. In other words, for each client, there is a separate socket and a separate dedicated thread.

Now, the problem that I foresee is that since there is only one socket i.e. single line for communication, there are chances that if both Server and Client send data at the same time, there are chances of data collisions.

How shall I cater such a situation? Waiting for response :(

解决方案

It depends on how much clients do you expect and how large messages will be sent.
The issue is that UDP does not guarantee delivery of data by itself, so some packets could be dropped.
http://technet.microsoft.com/en-us/library/cc785220(v=ws.10).aspx[^]
Simultaneous data sending both by sender and receiver will be critical with a large amount of connections. Even if you create your own protocol layer upon UDP to count number of messages sent, CRC etc to reduce data loss there will be a issue with a maximum thread count.
So if your solution designed in a right way, there will be no problems with tenths of clients but still there is a chance of flooding.
UDP should be reliable enough for fast connections and fair simple in realization, but for some cases I would prefer a completion ports.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365198(v=vs.85).aspx[^]


这篇关于使用单个udp套接字send()和receive()时数据冲突的可能性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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