Search code examples
javasocketsbufferedreaderjava-threads

Buffered Reader for a socket is never ready


Just to be completely transparent, this is for an assignment.

There is more to do, but at the moment I'm just trying to get the following:

  1. Node A reads in from a text file
  2. Node A sends text file (minus the first line) to Node B using a socket
  3. Node B read in from said socket, and prints it out to the console

However, right now, it seems that either the information isn't being sent, or it's not being read correctly by Node B.

In my main class, I set up the nodes like this:

NodeA nodeA = new NodeA();
NodeB nodeB = new NodeB();

new Thread(nodeA).start();
new Thread(nodeB).start();

In node A, I do this:

//Open a socket for talking with NodeB
Socket mySocket = new Socket(InetAddress.getLocalHost(), portNum);

//Set up the socket's output
PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true);

//Loop through the lines of the confA file, writing them to the socket
String line = bufferedReader.readLine();
while (line != null)
{
    //Write the line to the socket, get the next line
    out.println(line); //updated to println, this flushes and fixes another problem
    out.flush();
    line = bufferedReader.readLine();
}

//Close the socket
mySocket.close();

Note that Node A's loop works fine. It doesn't loop forever and does go through the intended lines of text when I tested with print statements.

Then, on Node B's end: Updated to show current Node B code

//Open the socket 
ServerSocket mySocket = new ServerSocket(portNum);
Socket connectionSocket = mySocket.accept();

//Set up a reader on the socket to get what's coming from it
BufferedReader in = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

String line = in.readLine(); //hang occurs here

while(line != null) {
    System.out.println(line);
    line = in.readLine();;
}

However, in.ready() is never true. I've tried waiting around for that to happen using a while loop but it never occurs.

I'm really not sure why. I have no idea if I set up the socket correctly, if I set up the server correctly, if I am listening correctly, etc.

I just figured that making B into a server which is listening for A made the most sense. I hope that's right. It looks similar to what I saw some other examples on SO did.

Thank you for any and all help. I'm extremely unfamiliar with sockets, ports, listening and otherwise, so forgive me if I don't understand your suggestions at first. I'll do my best to understand it as I go.

I refrained from adding the whole of the code to hopefully make it more readable and clear where the issue might be, but if you need more information just feel free to ask and I'll do my best to provide it.


Solution

  • The server must first get from the ServerSocket the Socket to the client.

    connectionSocket = mySocket.accept();
    

    The server thread will be sleep till a client causes it to accept the connectionSocket.

    Then you can read from the connectionSocket. ready not being needed.

    As this is an assignment, I leave the rest to you.


    By the way a typical server would do:

    for (;;) {
        Socket socket = serverSocket.accept();
        ... pass the socket to a thread from a pool of threads
    }