0

I am trying to develop a client/Server communication application in java. But at the very initial stage I am unable to read data string from the socket's Output stream using BufferedReader.readline(). I have googled a lot but could not find the specific answer to my question.
Debugging the client application shows that the client application halts at response.readline().

Server:

package myserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyServer {
    public static void main(String[] args) {
        String[] tokens = new String[10];
        int count = 0;
        ServerSocket server = null;
        try {
            server = new ServerSocket(16);
        } catch (IOException ex) {
            Logger.getLogger(OdeskServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (server != null) {
            while (true) {
                PrintWriter response;
                BufferedReader command;
                String inCommand;
                try {
                    Socket client = server.accept();
                    System.out.println("Connected!");
                    response = new PrintWriter(client.getOutputStream());
                    command = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    response.print("EHLO\r\n");
                    command.read(); //just to stop the application to start a new iteration
                } catch (IOException ex) {
                    Logger.getLogger(OdeskServer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}

Client:

package myclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyClient {

    public static void main(String[] args) {
        Socket server = null;
        PrintWriter command;
        BufferedReader response;
        BufferedReader Console = new BufferedReader(new InputStreamReader(System.in));
        try {
            server = new Socket("175.107.231.218", 16);
            System.out.println("Connected");
            command = new PrintWriter(server.getOutputStream());
            response = new BufferedReader(new InputStreamReader(server.getInputStream()));
            while (server.isBound()) {
                String input = response.readLine(); //Here is where the communication halts
                System.out.print(input);
            }
        } catch (UnknownHostException ex) {
            Logger.getLogger(OdeskClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(OdeskClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Any idea about this issue?

2
  • Testing server.isBound() is pointless. The socket is bound as soon as you connect it, if not sooner, and isBound() isn't going to change afterwards. You should be testing the result of readLine() for null as a loop breaker. Commented Mar 15, 2013 at 23:41
  • never mind! I am a beginner. Commented Mar 16, 2013 at 14:23

1 Answer 1

3

You may need to call response.flush() from the server to ensure that the socket does not buffer your message waiting for more writes to batch together.

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

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.