0

Hello stackoverflow community,

i am stuck at a problem regarding socket communication in Java.

Here is the sample code of my Server and Client class:

Server:

public class OTPServer {


static ServerSocket serverSocket;
final static int PORT = 4242;
static Socket clientConnection;

public static void main(String[] args) {

    try {
        serverSocket = new ServerSocket(PORT);
        System.out.println("Socket initialized");
        String serverMessage = "Hello, I am the Host";
        ServerTool serverTool = new ServerTool();

        while (true) {
            clientConnection = serverSocket.accept();
            if(clientConnection.isConnected()) {
                System.out.println("Client connected");
            }

            BufferedReader clientInputReader = new BufferedReader(new InputStreamReader(clientConnection.getInputStream()));
            DataOutputStream serverOutput = new DataOutputStream(clientConnection.getOutputStream());
            System.out.println("Sending message to client: " + serverMessage);
            serverOutput.writeBytes(serverTool.encodeMessage(serverMessage));
            serverOutput.flush();
            String clientMessage = clientInputReader.readLine();
            System.out.println("Encoded answer from client: " + clientMessage);
            String decodedMessage = serverTool.decodeMessage(clientMessage);
            System.out.println("Decoded answer from client: " + decodedMessage);

            serverOutput.close();
            clientInputReader.close();

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Hello, I am the OTP Server!");

}

Here is the Client:

public class OTPClient {

static Socket clientSocket;
final static int PORT = 4242;
final static String HOST = "localhost";


public static void main(String[] args) {
    System.out.println("I am the OTP Client!");
    String serverMessage;
    String clientResponse = "I am the Client";
    OTPTool otpTool = new OTPTool();

    try {

        clientSocket = new Socket(HOST, PORT);
        BufferedReader serverInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        DataOutputStream outputStream = new DataOutputStream(clientSocket.getOutputStream());
        System.out.println("Connection to Host established");
        serverMessage = serverInput.readLine();
        System.out.println("Encoded Message from Server: " + serverMessage);
        String decodedMessage = otpTool.decodeMessage(serverMessage);
        System.out.println("Decoded message from Server: " + decodedMessage);
        System.out.println("Answering with own message: " + clientResponse);
        outputStream.writeBytes(clientResponse);
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Now where is my problem:

The connection establishes and the Server seems to send its message to the client and waits for a answer. The Client does not print the message he got from the Server. As soon as i cancel the Server the client prints the message it gets from the server as well as the information, that the answer is send end exits with exit code 0 so it seems that this part is fine it just is stuck somehow. I already tried to flush the outputStream as you see in the example code given.

Is there something obvious im missing? I know, this is really basic stuff but its my first time using sockets for communication.

Thank you in advance!

Best Regards, Ronny

Btw: i know that the server only connects to one client requesting a connection. Thats absolutely sufficient for my use.

3
  • Definitely a deadlock / concurrency issue. In which order do you run each of these classes? Commented Jun 19, 2016 at 13:12
  • You are calling readLine. Is your serverTool.encodeMessage including a line break at the end of the message? Commented Jun 19, 2016 at 13:21
  • @BrettOkken at the moment the serverTool.encodeMessage does only return the parameter string. @vontell at first i start the server and then the client which should be no problem as the server is waiting on a client to connect to it. Commented Jun 19, 2016 at 13:31

1 Answer 1

1

It is getting stuck because serverInput.readLine(); blocks until either a line break or end of file is encountered. On the server side, you are not sending a line break, so the client blocks.

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

2 Comments

Thank you! I did not know, that a regular String return value doesnt include neither of them. Works now perfektly. Is there a way to read the input without a linebreak or end of file?
Sure. Don't call readLine. Use one of the other read methods. This requires that you define a "protocol" which defines the start/stop of messages.

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.