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 p开发者_运维问答layer.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?
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.
精彩评论