如何确保 WinSock 发送到所有客户端?

发布于 2024-11-16 17:23:04 字数 698 浏览 1 评论 0原文

我写了一个vb6游戏,可以让4个玩家玩纸牌游戏。 其中一名玩家将主持游戏,其他玩家将加入。

我使用套接字数组将所有其他玩家与主机套接字连接起来,

一切都很顺利,我能够从每个玩家向其他玩家发送消息,从主机到客人,反之亦然。

但是,在游戏过程中,一个玩家会“告诉”主机一张牌已被选择。然后,主机将向连接到套接字数组的每个客户端发送相同的消息,如下所示

For i=1 to 3
    Me.SocketArray(i).SendData player.selectedCard
    DoEvents
Next

,但由于某种原因,其中一个玩家从未收到该消息。尽管同一玩家在此之前确实收到了消息,并且稍后可能会收到其他消息。

因此,发送的内容和未发送的内容存在同步问题。 我到处寻找答案,有些人建议更频繁地使用 DoEvents 来强制 Winsock 立即发送数据。

然后我想到为每个玩家实现一个收件箱,其中每个玩家必须回复收到的消息的确认,并且主机在确认到达之前不会发送任何更多消息,但这使游戏变慢,我也有同样的情况我以前遇到过这个问题,但这次丢失的不是消息,而是确认。

我认为我遇到这一切的第一个原因是 Winsock 控件有时会发送不完整的消息(缺少一个字符),这会造成混乱,因为我需要另一端的每个字符,否则消息是无用的。

我如何确保每当主人发送一条消息时,所有其他客人都完好无损地收到所有消息?

I have written a vb6 game to allow 4 players to play a card game.
One of the players will host the game and the others will join.

I used a socket array to join all of the other players with the host socket

Everything went smooth and I am able to send messages from each player to the rest of the players, from host to guests and vice versa

However, during the game, one player would "tell" the host that a card is chosen. The host will then send the same message to each of the clients connected to the socket array like this

For i=1 to 3
    Me.SocketArray(i).SendData player.selectedCard
    DoEvents
Next

but for some reason, one of the player never receives the message. Although the same player did receive messages before this point, and possible will receive other messages later.

So there is a Syncing problem with what was send and what was not.
I looked for answers everywhere, and some suggested to use DoEvents more often to force the winsock to send the data immediately.

I then thought of implementing an inbox for each player in which each player must reply with an acknowledgment of the received message, and the host will not send any more messages until the arrival of acknowledgement, but that made the game slower and I had the same problem I had before, but this time it's not the messages that got lost, it's the acknowledgment.

The number 1 reason I think I am having all of this is that the Winsock control sometimes sends incomplete messages (one character missing) and that will create a mess, because I need every character at the other end, otherwise the message is useless.

How can I make sure that whenever the host sends a message, all the other guests receive ALL of it intact?

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

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

发布评论

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

评论(1

诗酒趁年少 2024-11-23 17:23:04

DoEvents() 调用在错误的人手中是危险的。

您可能会遇到的情况是“神奇”消息框架的假设、无法缓冲和解析入站数据流中的消息以及由于您正在调用 DoEvents() 而导致对 DataArrival 处理程序的可重入调用。

Nagle 也可能是一个问题,但它应该会导致响应问题,而不是明显的数据丢失。

在过去的十年里,同样的问题已经被无数次地询问和回答。

DoEvents() calls are hazardous in the wrong hands.

What you probably have going on is the assumption of "magic" message framing, a failure to buffer and parse messages from inbound data streams, and reentrant calls to your DataArrival handler because you are calling DoEvents().

Nagle could be an issue as well but it should result in responsiveness problems and not apparent data loss.

This same issue has been asked about and answered innumerable times over the last decade.

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