Java DatagramSocket - 无法同时收到两个数据包

时间:2014-12-25 12:43:35

标签: java

所以我正在编写2D MMO游戏并且我被卡住了。我的客户端无法同时收到两个数据包,我不知道为什么。所以第一段代码非常简单 - 我将ping数据包发送到服务器,然后服务器向我发送包含有关级别信息的数据包 - 每秒都有效。但现在我想让它变得更容易。我想在游戏开始时(登录游戏后)然后在移动后重新接收数据包。但它不起作用。这是我的客户网络线程代码:

public void run()
{
    while(clientRunning)
    {
        byte[] data = new byte [1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);

        //SendPingPacket.SendPing(c_socket, c_ipAdress, c_port);

        try
        {
            c_socket.receive(packet);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

        PacketHandler.HandlePacket(data, c_socket, packet.getAddress(),
                packet.getPort());
    }
}

客户的JPanel:

public void keyPressed(KeyEvent e)
    {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_UP)
        {
            MoveSender.SendMoveData(m_playerID, 0, m_socket.GetSocket(), m_ipAddress, m_port);
            AskForLevelSender.SendAskForLevel(GameBoard.m_playerID,
                    m_socket.GetSocket(), m_ipAddress, m_port);
        }
        else if (key == KeyEvent.VK_DOWN)
        {
            MoveSender.SendMoveData(m_playerID, 1, m_socket.GetSocket(), m_ipAddress, m_port);
            AskForLevelSender.SendAskForLevel(GameBoard.m_playerID,
                    m_socket.GetSocket(), m_ipAddress, m_port);
        }
        else if (key == KeyEvent.VK_RIGHT)
        {
            MoveSender.SendMoveData(m_playerID, 3, m_socket.GetSocket(), m_ipAddress, m_port);
            AskForLevelSender.SendAskForLevel(GameBoard.m_playerID,
                    m_socket.GetSocket(), m_ipAddress, m_port);
        }
        else if (key == KeyEvent.VK_LEFT)
        {
            MoveSender.SendMoveData(m_playerID, 2, m_socket.GetSocket(), m_ipAddress, m_port);
            AskForLevelSender.SendAskForLevel(GameBoard.m_playerID,
                    m_socket.GetSocket(), m_ipAddress, m_port);
        }
    }

请帮助解决这个问题:)

0 个答案:

没有答案