简体   繁体   English

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

[英]Reliable UDP Protocol Implementation in Java - Why does this happen?

I'm currently using a Java implementation of the Reliable UDP protocol, found here . 我目前正在使用可靠UDP协议的Java实现,可在此处找到。 The project has absolutely no tutorials so I have found it really hard to identify problems. 该项目绝对没有教程,所以我发现很难找出问题。

I have set up a client and server. 我已经设置了客户端和服务器。 The server runs on localhost:1234 and the client runs on localhost:1235. 服务器在localhost:1234上运行,客户端在localhost:1235上运行。 The server is first established, and loops listening for connections - 服务器首先建立,并循环侦听连接 -

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();
        }

When a connection is established, it creates a listener for events on that socket - 建立连接后,它会为该套接字上的事件创建一个侦听器 -

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);
    }
}

When a packet is received, it passes it to server.handlePacket, which performs a debug routine of printing "Packet Received!". 收到数据包后,它会将其传递给server.handlePacket,后者执行打印“Packet Received!”的调试程序。

My client connects to the server as so - 我的客户端如此连接到服务器 -

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();
            }

        }

I have linked my code so when I press the key 'Z', it will send a packet to the server as so - 我已经链接了我的代码所以当我按下'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();
        }

    }

My problem is, after sending 32 packets, the server no longer receives packets, and after sending 64 packets, it crashes. 我的问题是,在发送32个数据包后,服务器不再接收数据包,并且在发送64个数据包后,它会崩溃。 I have investigated into the code, and it appears that its something associated with packets not being removed from the receive queue, as when I changed the _recvQueueSize variable in ReliableSocket.java:1815 from 32 to 40, I could now send 40 packets without something going wrong. 我已经调查了代码,看起来它与数据包没有从接收队列中删除相关联,就像我将ReliableSocket.java:1815中的_recvQueueSize变量从32更改为40时,我现在可以发送40个数据包没有东西出错了。

Could someone help me identify this issue? 有人可以帮我识别这个问题吗? I've been looking at the code all day. 我整天都在看代码。

I managed to fix the problem. 我设法解决了这个问题。

You see, since this is an implementation of RUDP, it extends most of the Socket classes. 你看,由于这是RUDP的一个实现,它扩展了大多数Socket类。 Specifically, ReliableSocket.getInputStream(), was custom coded to a managed input stream. 具体而言,ReliableSocket.getInputStream()被自定义编码为托管输入流。 My problem was, I was receiving the packets, but not reading from the buffer. 我的问题是,我收到了数据包,但没有从缓冲区读取。

When you receive a packet you're supposed to read from the buffer, otherwise the packet will not be dropped from the queue. 当您收到数据包时,您应该从缓冲区中读取数据包,否则数据包将不会从队列中删除。

So all I had to do, was everytime I received a packet, read the size of the packet, and continue. 所以我所要做的就是,每当我收到一个数据包,读取数据包的大小,然后继续。

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

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