Domanda

I am working on class project for a remote string processor. It's my very first project in Java, so I do not really understand what all is going on...

I have everything finally working from Client to Server, but when it comes for the server to send the results back to the client, they both stay hang up and won't do anything until I forcefully close the server. Then "voilà!"... everything I was waiting for shows up on the client side.

If you have encountered such an issue, you probably are smiling right now as you click in the Answer box down below :)

Here is part of the server-side functions code, they give an idea about what I'm expecting to see on client-side as output.

//Get an output stream for writing to the client socket dataSocket 

PrintStream socketOutput = new PrintStream(dataSocket.getOutputStream());

// some code here then

// This is where the various functions are executed depending on
// the value of the code entered by the user.
// A string, an integer, or a
// Boolean value depending on the case will be returned and printed
             if (string1 != null && string2 != null) {
             System.out.println("STRINGS 1 AND 2 WERE NOT NULL, SO LET'S ENTER SWITCH");
                switch (code) {
                    case 1:
                        System.out.println("IN CASE 1");
                        int returnedSign = string1.compareTo(string2);

                        if (returnedSign > 0) {
                            System.out.println("IN CASE 1, returnedSign > 0");
                            socketOutput.println(string1+" lexically follows "+string2);
                        }
                        else if (returnedSign < 0) {
                            System.out.println("IN CASE 1, returnedSign < 0");
                            socketOutput.println(string2+" lexically follows "+string1);
                        }
                        else if (returnedSign == 0) {
                            System.out.println("IN CASE 1, returnedSign == 0");
                            socketOutput.println(string1+" equals "+string2);
                        }
                        System.out.println("AT END OF CASE 1");
                        socketOutput.flush();// forcing the socketOutput to let go of any data
                        break;

Alright, and here is some client-side code that's supposed to receive the output of the server and display it on the client screen:

// Get an output stream for writing to the socket
            ObjectOutputStream myOS = new ObjectOutputStream(clientSocket.getOutputStream());

            // Get stuff from the server and display them on the client's screen
            BufferedReader ois = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); ...

...// Write to the Stream, that is get the code and the two strings
            // to the stream

            String codeMadeString =  Integer.toString(code);

            String messageString = code+";"+string1+";"+string2;
            System.out.println("User entered: "+messageString);

            myOS.writeObject(messageString);

            //String serverOut = "";
            String serverOut = (String)ois.readLine();
            serverOut+='\n';
            serverOut+=(String)ois.readLine();
            serverOut+='\n';
            serverOut+=(String)ois.readLine();
            serverOut+='\n';
            serverOut+=(String)ois.readLine();

            System.out.println("Server said she saw... \n  "+serverOut);

            myOS.flush();

            // Close the data socket
            clientSocket.close();

Again, my question can also be stated as "what should I do to make sure that what's sent by the server is seen by the client without me having to close force the server to close first?"

Notes: I am working from the Terminal on MacOS Mavericks. Any constructive ideas are welcome. Also, I am not a specialist in this, so please...

UPDATE1: I added the declaration of socketOutput.

È stato utile?

Soluzione

I was not closing the buffered reader at first. Then I closed it at the wrong place. And finally, I found out it needs to be just at the end of the infinite loop server-side.

So:

while(true){
// Some code; 

// Get stuff from the server and display them on the client's screen
BufferedReader ois = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

// Some more code and then
ois.close(); // before I get outside the loop.
}

And that's it!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top