Java中可靠的UDP协议实现 - 为什么会这样?

时间:2011-12-07 06:12:36

标签: java networking udp queue rudp

我目前正在使用可靠UDP协议的Java实现,找到here。该项目绝对没有教程,所以我发现很难找出问题。

我已经设置了客户端和服务器。服务器在localhost:1234上运行,客户端在localhost:1235上运行。首先建立服务器,并循环侦听连接 -

try {
            ReliableSocket clientSocket = server.socket.accept();
            InetSocketAddress clientAddress = (InetSocketAddress) clientSocket.getRemoteSocketAddress();

            Logger.getLogger("ServerConnectionListener").info("New Connection from "+
                    clientAddress.getHostName()+":"+clientAddress.getPort()+" Processing...");
            LessurConnectedClient client = new LessurConnectedClient(clientSocket);
            ClientCommunicationSocketListener listener = new ClientCommunicationSocketListener(this, client);
            clientSocket.addListener(listener);
        } catch (Exception e) {
            e.printStackTrace();
        }

建立连接后,它会为该套接字上的事件创建一个侦听器 -

class ClientCommunicationSocketListener implements ReliableSocketListener {
    ServerConnectionListener connectionListener;
    LessurConnectedClient client;

    public ClientCommunicationSocketListener(ServerConnectionListener connectionListener, LessurConnectedClient client){
        this.connectionListener = connectionListener;
        this.client = client;
    }

    @Override
    public void packetReceivedInOrder() {
        connectionListener.server.handlePacket(client);
    }

    @Override
    public void packetReceivedOutOfOrder() {
        connectionListener.server.handlePacket(client);
    }
}

当收到数据包时,它会将其传递给server.handlePacket,后者执行打印“Packet Received!”的调试程序。

我的客户端连接到服务器 -

LessurClient client = new LessurClient();
        InetSocketAddress a = (InetSocketAddress) server.getSocket().getLocalSocketAddress();
        Logger.getLogger("client-connector").info("Trying to connect to server "+
                a.getAddress().toString()+":"+
                a.getPort());
        client.connect(a.getAddress(), a.getPort());
// LessurClient.connect
    public void connect(InetAddress address, int port){
            try {
                socket = new ReliableSocket(address, port, InetAddress.getLocalHost(), 1235);
                isConnected = true;
                Logger.getLogger("LessurClient").info("Connected to server "+address.getHostAddress()+":"+port);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

我已经链接了我的代码,所以当我按下'Z'键时,它会向服务器发送一个数据包 -

public void sendPacket(GamePacket packet){
        if(!isConnected){
            Logger.getLogger("LessurClient").severe("Can't send packet. Client is not connected to any server.");
            return;
        }
        try {
            OutputStream o = socket.getOutputStream();
            o.write(packet.getData());
            o.flush();
            Logger.getLogger("LessurClient").info("Sending Packet with data \""+packet.getData()+"\" to server "+socket.getInetAddress().toString()+":"+socket.getPort());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

我的问题是,在发送32个数据包后,服务器不再接收数据包,并且在发送64个数据包后,它会崩溃。我调查了代码,看起来它与未从接收队列中删除的数据包相关联,就像我将ReliableSocket.java:1815中的 _recvQueueSize 变量从32更改为40,我现在可以发送40个包,没有出错。

有人可以帮我识别这个问题吗?我一直在看代码。

1 个答案:

答案 0 :(得分:4)

我设法解决了这个问题。

你知道,由于这是RUDP的一个实现,它扩展了大多数Socket类。具体而言,ReliableSocket.getInputStream()被自定义编码为托管输入流。我的问题是,我收到了数据包,但没有从缓冲区读取。

当您收到数据包时,您应该从缓冲区中读取数据包,否则数据包将不会从队列中删除。

所以我所要做的就是,每当我收到一个数据包,读取数据包的大小,然后继续。