需要一种更好的方式在发送和接收之间等待,UDPclient
这是我的麻烦:
我有一个 udp 类,它允许我从游戏服务器发送和接收数据。
但我发现服务器每 500 毫秒只允许每个客户端发出一个请求。 因此,如果我连续发送两个请求,服务器仅响应第一个请求,并且我无法知道我不会得到第二个答案。
所以我做了一个互斥锁来保护发送部分,当我发送数据时,我使用一个线程来阻塞互斥锁500ms。
这个类由线程使用,这就是我使用互斥锁的原因。
但这并不是很好,有时接收会卡住。
我只是想知道是否有人有更好的方法来做到这一点。
谢谢你,对我的英语感到抱歉。
编辑: 我无法使用 tcp 协议,我需要使用 udp 来完成此操作。 我还需要一种最佳方法,我需要尽快将接收数据发送到视图表单。 我查看了网上发现的有关 udp 和线程的每个主题,但没有找到这种特殊情况。
Here's my trouble:
I have a udp class that allow me to send and receive data from a gameserver.
But i found that the server allow only one request per 500ms per client.
so if i send two request in a row, the server respond only to the first one, and i have no way to know that i won't get the second answer.
So i made a Mutex to protect the send part, when i send data, i use a thread to block the mutex 500ms.
This class is used by thread, that's why i use mutex.
But this doesn't work really good, sometime the receive get stuck.
I just want to know if someone have a better way to do this.
Thank you and sorry for my english.
EDIT:
I can't use tcp protocol, i need to do this with udp.
I also need an optimal way, i need to get the receive data asap to the viewform.
I watch every single topic on the net i found on udp and threading, but don't find this particular case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您知道只能以一定频率发送,为什么不发送阻塞呼叫。然后睡规定的时间。
If you know you can only send with some frequency, why not send calls blocking. And then sleep for the required time.
使用 UDP,您永远不应该假设您发送的任何给定消息都会得到响应。您也不应该假设该消息实际上会被收到,或者多条消息将按照您发送的顺序收到。这就是为什么 UDP 实际上只适合能够容忍信息丢失的协议。如果你想保持完整性,你需要允许重试,此时你可能会更好地使用 TCP(假设你在这件事上有选择)。
如果没有有关协议细节的更多信息,就很难推荐一个好的解决方案。如果它是简单的“状态轮询”样式的消息,一种选择是简单地以固定的时间间隔发送轮询消息并在响应到达时对其进行处理,忽略响应可能与正在发送的消息之间的任何关系。
With UDP, you should never assume that you will get a response to any given message you send. You should also not assume that the message will actually be received or that multiple messages will be received in the same order you send them. That's why UDP is really only suited to protocols that can tolerate loss of information. If you want to maintain integrity, you need to allow for retries, at which point you probably would've been better off using TCP (assuming you had a choice in the matter.)
Without more information on the details of the protocol, it's hard to recommend a good solution. If it's a simple 'status poll' style of message, one option is to simply send the polling messages at a fixed interval and handle the responses as they arrive, ignoring any relation the responses might have to the messages being sent.
正如其他人指出的那样,不能假设将传递 UDP 消息。这可能会导致您的代码在接收时挂起。也许只需设置套接字的 ReceiveTimeout 就会对于你的应用来说足够好。
或者您可以设置一种(稍微复杂一些)方法,对要发送的消息进行排队,每 500 毫秒仅发送一条消息,并在一段时间后重试,直到收到响应。如果我选择这条路线,我会围绕以下一般要点进行设计:
MsgObject
)构建一个结构/类,包括有关上次传输时间的信息。As others have pointed out, it can't be assumed that a UDP message will be delivered. This is probably causing the hanging of your code on the receive. Maybe simply setting the Socket's ReceiveTimeout will be good enough for you're application.
Or you could setup a (somewhat more complex) method of queuing messages to send, only sending one every 500ms, and retrying them after a certain amount of time until you've gotten a response. If I were going this route, I'd design around these general points:
MsgObject
), Including information about the last time it was transmitted.