0

I am attempting to create a very simple Socket/ServerSocket program in Java. My two classes are listed below, both of which run on the same machine. The problem is, the connection seems to be established just fine, but when I attempt to write over the socket from the client to the server, nothing seems to happen. The server never returns from in.readLine(), and the program comes to a halt. I cannot figure out why this is happening.

Here are my classes:

TheServer.java:

public class TheServer {
  public static void main(String[] args) {
    BufferedReader in = null;
    PrintWriter out = null;

    try {
        ServerSocket serv = new ServerSocket(2255);
        Socket sock = serv.accept();
        in = new BufferedReader(
                new InputStreamReader(sock.getInputStream()));
        out = new PrintWriter(sock.getOutputStream());

    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Connection established by Server");

    try {
        String line;
        while ((line = in.readLine()) != "Bye") {
            System.out.println(line + "Received");
            out.println(line.toUpperCase());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

  }
}

TheClient.java:

public class theClient {
  public static void main(String[] args) {
    BufferedReader in = null;
    PrintWriter out = null;

    try {
        Socket sock = new Socket("localhost", 2255);
        in = new BufferedReader(
                new InputStreamReader(sock.getInputStream()));
        out = new PrintWriter(sock.getOutputStream());
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("Connection established by Client");

    try {
        while (true) {
            BufferedReader read = new BufferedReader(new InputStreamReader(
                    System.in));
            String line = read.readLine();
            out.println(line.trim());
            System.out.println(in.readLine().trim());

            if (line == "Bye")
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

  }
}
3
  • Are you passing an end of line (\n) in the data? I think I ran into this in the past too. Since you are reading lines on each side, you need to send the EOL. Commented Feb 17, 2014 at 4:25
  • 1
    println() appends an EOL to every line it sends Commented Feb 17, 2014 at 4:26
  • I ran the fix and it looks like it works. It looks like the termination case doesn't though. != "Bye" may need to be --> while (!(line = in.readLine()).equalsIgnoreCase("Bye")) { Commented Feb 17, 2014 at 4:47

2 Answers 2

3

for PrintWriter object, after each write try,

    obj.flush();
Sign up to request clarification or add additional context in comments.

Comments

0

You are not flushing the outputstream, do printwriter_obj.flush();

You have to do this after sending each and every message. I would suggest to add auto flush parameter to the Printwriter,like this:

PrintWriter out=new PrintWriter(OutputStream:socket.getOutputStream(),AutoFlush:true);

Comments

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.