Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm currently using a Java implementation of the Reliable UDP protocol, found here. 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. 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!".

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 -

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. 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.

Could someone help me identify this issue? I've been looking at the code all day.

like image 746
liamzebedee Avatar asked Dec 07 '11 06:12

liamzebedee


1 Answers

I managed to fix the problem.

You see, since this is an implementation of RUDP, it extends most of the Socket classes. Specifically, ReliableSocket.getInputStream(), was custom coded to a managed input stream. 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.

like image 192
liamzebedee Avatar answered Sep 20 '22 13:09

liamzebedee