Question

I am having a problem where I can connect to my ServerSocket and send Objects (A custom Packet class to be exact) to my client, but when I use the readObject() method, my code hangs and doesn't read any packets.

Here is the Client code:

public class ObjectReaderThread extends Thread {

public void run() {

    Object obj = null;

    while(!ClientTest.clientTest.socket.isClosed()) {

        try {

            obj = ClientTest.serverReader.reader.readObject();

            System.out.println("Packet recieved.");

        } catch(Exception ex) { ex.printStackTrace(); System.exit(-1); }

    }

}

As you can see, I am using a separate thread for my readObject loop. Can someone please help me identify what is incorrect? If any more code is needed, tell me and I can post it. Also please note that no exception/error is ever given. Thanks!

EDIT: Here is the code sending the Packet:

public void sendNextPacketInQueue() {
    Iterator<Integer> queue = packetQueue.keySet().iterator();
    try {

        if(outputStreamInitialized) ;

        if(queue.hasNext()) {

            int key = queue.next();
            Packet packet = packetQueue.get(key);

            if(packet.getLocation().equals(PacketLocation.ALL) && GlobalVars.onlinePlayers.size() > 0) {

                for(int i = 1; i <= GlobalVars.onlinePlayers.size(); i++) {
                    Player player = GlobalVars.onlinePlayers.get(i);
                    ObjectOutputStream out = new ObjectOutputStream(player.getOutputStream());
                        out.writeObject(packet);

                    if(GlobalVars.debugOutput) GlobalClassVars.window.printToLog("Packet sent to " + player.getUsername());
                }
            }
            else {
                if(GlobalVars.debugOutput) GlobalClassVars.window.printToLog("Not enough players online to send packet to.");
            }

            packetQueue.remove(key);
            if(GlobalVars.debugOutput) GlobalClassVars.window.printToLog("Packet removed.");

        }

    } catch(Exception ex) { ex.printStackTrace(); }
}

This is run constantly in a server loop:

while(!serverSocket.isClosed()) {

        ...

        this.sendNextPacketInQueue();

}
Was it helpful?

Solution 2

I did a lot wrong with this. First off I was creating a new ObjectOutputStream each time. Second, I was getting the player from the list using an int, instead of the username.

Don't even ask how I made these stupid mistakes...

OTHER TIPS

Your client code is blocking on readObject call. It waits until the next object becomes available in the socket. Though you are writing the object on the server socket, your client will not get it immediately because of the socket buffer. For the client to get the object immediately you have to flush the stream on server side after writing every object. In sendNextPacketInQueue method add out.flush() call after out.writeObject call.

out.writeObject(packet);
out.flush();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top