6

我目前正在使用 Reliable UDP 协议的 Java 实现,可在此处找到。该项目完全没有教程,所以我发现很难发现问题。

我已经设置了客户端和服务器。服务器在 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 个数据包而无需任何东西出错了。

有人可以帮我确定这个问题吗?我整天都在看代码。

4

1 回答 1

4

我设法解决了这个问题。

你看,因为这是 RUDP 的一个实现,它扩展了大部分的 Socket 类。具体来说,ReliableSocket.getInputStream() 被自定义编码为托管输入流。我的问题是,我正在接收数据包,但没有从缓冲区读取。

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

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

于 2011-12-07T07:59:05.840 回答