Question

I am currently having difficulty understanding why my code is not working. I've included my client and server code below. I've figured out that my problem happens somewhere in the while loops but I'm not sure how to fix it so that it doesn't get stuck. I've searched around the forum for a while and some said adding a newline character would fix it, but I'm still having trouble.

My main question is how can I avoid the process from getting stuck and not communicating properly. Can anybody out there point me in the right direction?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;


public class My_Client {

public static void main(String[] args) throws UnknownHostException, IOException {

    Socket s = new Socket("localhost", 5555);

    BufferedReader r = new BufferedReader(new InputStreamReader( 
            s.getInputStream()));

    PrintStream w = new PrintStream(s.getOutputStream());

    w.print("hello world");
    w.print('\n');

    String line;

    while ((line = r.readLine()) != null) {
        System.out.println("Received: " + line);

        //System.out.println("Error");
    }
    w.close();
}
}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

-----------------------------------------------------------------
public class My_Server {

private static final int PORT = 5555;

public static void main(String[] args) {
    try {
        ServerSocket ss = new ServerSocket(PORT); 
        System.out.println("Server Socket Created");

        while (true) {

            System.out.println("Waiting on connection");
            Socket cs = ss.accept(); 
            System.out.println("Client connected");

            BufferedReader r = new BufferedReader(new InputStreamReader( 
                    cs.getInputStream()));
            PrintStream w = new PrintStream(cs.getOutputStream());

            String line;

            while ((line = r.readLine()) != null) {
                w.print(line + "!!!!");  
                w.print('\n');
            }

            System.out.println("Client disconnected");
            r.close();
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

   }

 }
Was it helpful?

Solution

Both ends are reading until EOS and neither is closing until after that. So you have a classic deadlock. You need to rethink your application protocol.

You also need to tell your PrintStream or PrintWriter to autoflush, or else call flush() yourself, but this is a relatively minor matter compared to the mistake above.

OTHER TIPS

You should use autoflush on your PrintWriters like this:

PrintStream w = new PrintStream(cs.getOutputStream(),true);

You can setup a PROTOCOL to end the communication something like this:

In your client:

w.println("[END]");

In your server:

 while (!(line = r.readLine()).equals("[END]")) {

Hope this helps:

Check the comments

And be sure that you get Client Connected on console of server side

CLIENT SIDE
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.Socket;
    import java.net.UnknownHostException;


    public class My_Client {

    public static void main(String[] args) throws UnknownHostException, IOException {

        Socket s = new Socket("localhost", 5555);

        BufferedReader r = new BufferedReader(new InputStreamReader( 
                s.getInputStream()));

        PrintStream w = new PrintStream(s.getOutputStream());

        w.print("hello world");
        w.print("\n"); // enter new line
        w.flush();// flush the outputstream
        String line;

        while ((line = r.readLine()) != null) {
            System.out.println("Received: " + line);

            //System.out.println("Error");
        }
        w.close();
    }
    }
    SERVER SIDE
    ----------------------------------------------------------
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class My_Server {

    private static final int PORT = 5555;

    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(PORT); 
            System.out.println("Server Socket Created");

            while (true) {

                System.out.println("Waiting on connection");
                Socket cs = ss.accept(); 
                System.out.println("Client connected");

                BufferedReader r = new BufferedReader(new InputStreamReader( 
                        cs.getInputStream()));
                PrintStream w = new PrintStream(cs.getOutputStream());

                String line;

                while ((line = r.readLine()) != null) {
                    w.print(line + "!!!!");  
                    w.print("\n");// entering new line
                }

                System.out.println("Client disconnected");
                r.close();
                w.close();// close w
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

       }

     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top