2

So the end result of my program is an updating game client, but what i have so far is a server that is able to accept multiple connections, and a client that connects to the server. this is the code for the client portion:

public void client() {
    Socket socket = null;
    ObjectInputStream in = null;
    ObjectOutputStream out = null;

    try {
        socket = new Socket(IP, Integer.parseInt(port));
        in = new ObjectInputStream(socket.getInputStream());
        out = new ObjectOutputStream(socket.getOutputStream());
    } catch (Exception e) {
        e.printStackTrace();
    }
    do {
        // have a conversation
        try {
            message = (String) in.readObject();
            System.out.println("\n" + message);
        } catch (Exception e) {
            System.out.println("\n idk wtf that user sent!");
        }
    } while (!message.equals("CLIENT - END")); // When the user types "END"

    System.err.println("CLOSED!!!");
    System.exit(0);
}

and this is the code for the server portion:

public void run() {
    // where everything happens
    System.out.println("server- connected");
    try {
        in = new ObjectInputStream(socket.getInputStream());
        out = new ObjectOutputStream(socket.getOutputStream());

        out.writeObject("hi");
        out.flush();
        Thread.sleep(5000);
        out.writeObject("close");
        out.flush();
        System.out.println("closed");
    } catch (Exception e) {
        e.printStackTrace();
    }

}

now, i am running into this problem where, when my server sends the object "hi" the client appears to not receive it. i'm not totally sure if it does, but if it is getting it, it isnt printing it out like i wanted. i previously have made a chat program that does this same thing, and i pretty much copied it to this, but it isnt communicating. the most i get is the confirmation that they are connected. im not sure what is going on, but any help would be appreciated! thanks in advance!

4
  • Are you getting the output of the line System.out.println("server- connected");? Commented Nov 22, 2012 at 2:34
  • yes i do. the problems start happening when i try to send the objects Commented Nov 22, 2012 at 2:36
  • Have you tried sending objects? as JSON encoded objects? check this post Commented Nov 22, 2012 at 2:42
  • Do you really need ObjectInputStream / ObjectOutputStream? I can see only Strings in your code. Commented Nov 22, 2012 at 5:12

1 Answer 1

4

create the ObjectOutputStreams before the ObjectInputStreams and flush them immediately after creation.

the constructor of an ObjectInputStream reads the stream header. this stream header is written by the constructor of the ObjectOutputStream (kind of an ugly implementation, but it is what it is). if you construct the OIS's first, they hang waiting for the header bytes.

Sign up to request clarification or add additional context in comments.

3 Comments

what is the reason for that?
agreed, that was an amazingly simple solution, but why did it happen?
@wbAnon - added explanation. this a fairly well-known gotcha when combining sockets and Object streams in java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.