1

I have the following client - side server communicator:

public class ServerAdapter
{
    private static final int serverPort = 8888;
    private static final String host = "localhost";
    private Socket clientSocket;
    private PrintWriter out;
    private BufferedReader in;

    private void startConnection(String host, int port) throws IOException {
        clientSocket = new Socket(host, port);
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    }

    private String sendMessage(String msg) throws IOException {
        out.println(msg);
        String resp = in.readLine();
        return resp;
    }

    private void stopConnection() throws IOException {
        in.close();
        out.close();
        clientSocket.close();
    }
    public double sendServerForexRequest(Request clientRequest) throws IOException {
        startConnection(host, serverPort);
        String result = sendMessage(clientRequest.toString());
        stopConnection();
        return Double.parseDouble(result);
    }

When invoking sendServerForexRequest, the client established the connection successfully, sends the message successfully, but then get stuck when trying to read back.

This is because, according to debugging, the server side is stuck when trying to read from the client, even tho the client already had sent data:

import java.io.*;
import java.net.Socket;
import java.util.stream.Collectors;

public class UserRequestHandlerThread extends Thread
{
    Socket userSocket;
    public UserRequestHandlerThread(Socket userSocket)
    {
        this.userSocket = userSocket;
    }
    @Override
    public void run() {
        ForexRate forexRate = new ForexRate();
        InputStream input = null;
        try {
            input = userSocket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        String requestString = null;
        try {
            requestString = reader.lines().collect(Collectors.joining()); //RIGHT HERE
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        Request request = Request.fromString(requestString);
        double exchangeResult = forexRate.rate(request.getFrom(), request.getTo()) * request.getAmount();
        OutputStream outputStream = null;
        try {
            outputStream = userSocket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        PrintWriter streamWriter = new PrintWriter(outputStream, true);
        streamWriter.print(exchangeResult);
        try {
            userSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I don't know why that is. I checked multiple sources on the internet and they all seem to write both the client side and the server side in this exact same way. this is extremely odd.

Why does the server fails to read from the client?

Aviv Vetaro
  • 363
  • 5
  • 12

1 Answers1

0

Found it out. DO NOT use reader.lines().collect(Collectors.joining());. use a char[] buffering methoud instead.

Aviv Vetaro
  • 363
  • 5
  • 12