简体   繁体   English

TCP Socket接收每秒数据包

[英]TCP Socket receiving every second packet

I'm trying to code a basic game, where when one player makes a move, the other receives the packet and vice versa. 我正在尝试编写基本游戏的代码,当一个玩家进行移动时,另一个玩家接收数据包,反之亦然。 The problem is, only every second packet is being received. 问题是,只接收每一个数据包。 I know it's not the connection because it's consistently the same pattern of missed packets. 我知道这不是连接,因为它始终与丢失数据包的模式相同。

My code is as follows: 我的代码如下:

Socket serverSocket

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    //Take socket from another window that created it and start receiving
    serverSocket = Welcome.MainSocket;
    serverSocket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);

    //The initial definition of the socket was this (in the Welcome window)
    //MainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    //MainSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
    //MainSocket.Bind(new IPEndPoint(OwnLocal, OwnPort));
    //MainSocket.Connect(Address, OppositePort);
}

private void OnReceive(IAsyncResult ar)
{
    OnlineData Data = OnlineData.FromByte(Buffer);

    //Do stuff with data on UI thread
    if (Data is MoveData)
    {   App.Current.Dispatcher.Invoke((Action)delegate
            {
                ((MoveData)Data).Sync(Game, Me == Game.PlayerOne ? 1 : 2);
            });
    }

    //End receive and start receiving again
    serverSocket.EndReceive(ar);
    serverSocket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
}

//Called each time the player makes a move
void SocketSend(OnlineData Data)
{
    serverSocket.Send(Data.ToByte());
}

Any ideas why this would ever happen in my case or in any other situation? 在我的情况下或在任何其他情况下,为什么会发生这种情况的任何想法? Thanks! 谢谢!

The immediate thing I can see is that you aren't calling EndReceive , which means you aren't processing the critical return value from that method: the number of bytes received. 我可以看到的直接事实是你没有调用EndReceive ,这意味着你没有处理该方法的关键返回值:接收的字节数。 I expect your data is being combined and multiple messages are being received in a single "receive" call (TCP is stream based, not message based). 我希望您的数据被合并,并且在单个“接收”呼叫中接收多个消息(TCP是基于流的,而不是基于消息的)。

This also means you aren't seeing any exceptions that you should know about. 这也意味着您没有看到任何您应该了解的例外情况。

Additionally, not calling End* methods can cause leak issues - you are very much meant to call End* methods. 此外,不调用End*方法可能会导致泄漏问题 - 您非常想调用End*方法。 Or switch to the newer async IO API. 或者切换到较新的异步IO API。

What was happening was that I had two different AsyncCallbacks for BeginRecieve, one from the original window ("Welcome") in which the socket was created, and one for this window. 发生的事情是我有两个不同的BeginRecieve AsyncCallbacks,一个来自创建套接字的原始窗口(“Welcome”),另一个用于此窗口。 Each time the other computer sent a message, the AsyncCallback that receieved the data was alternating between the two. 每次另一台计算机发送消息时,接收数据的AsyncCallback在两者之间交替

Moral of this story: Only have one AsyncCallback for a socket unless you want to only receive every second packet. 这个故事的道德:除了你只想接收每一个数据包之外,只有一个套接字的AsyncCallback。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM