0

I have a client server program where the serverscoket will accept a connection form the client, and the client can receive messages from the server, but not vice versa. The Client:

public class ChatClient {

private final String serverName;
private final int serverPort;
private Socket socket;
private OutputStream serverOut;
private InputStream serverIn;
private BufferedReader bufferedInputStream;
//private ArrayList<UserStatusListener> userListners = new ArrayList<UserStatusListener>();


private ChatClient(String serverName, int serverPort) {
    super();
    this.serverName = serverName;
    this.serverPort = serverPort;
}


public static void main(String[] args) throws IOException {

    ChatClient client = new ChatClient("localhost", 8818);

    // Make sure serverboot is running and listenig first
    if (! client.connect()) {
        System.out.println("Connection Failed. Is ServerBoot running/listening?");
    }else {
        System.out.println("Connection succesful");
    client.login("guest");
    }

}


private boolean connect() {
    // TODO Auto-generated method stub
    try {
        this.socket = new Socket(serverName, serverPort); 

        // get streams
        this.serverOut = socket.getOutputStream();
        this.serverIn = socket.getInputStream();
        this.bufferedInputStream = new BufferedReader(new InputStreamReader(serverIn));

        return true;
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        return false;
    }

}

private void login (String login) throws IOException {

    // send login to server
    try {

    String serverResponse = bufferedInputStream.readLine();
    System.out.println("Server response: " + serverResponse);

    while (bufferedInputStream.readLine() != null) {
        System.out.println(bufferedInputStream.readLine());
    }
String send = "Login : " + login;
        serverOut.write(send.getBytes());


    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

Snippets form the server:

 try {
                ServerSocket serverSocket = new ServerSocket(serverPortNumber);
                while (true) {
                    // Listen for incoming connections and craete connection with accept
                    System.out.println("Waiting for client connection.... on localhost port " + serverPortNumber
                            + ". \n Client connect via netcat localhost 8818");

                    Socket clientSocket = serverSocket.accept();// returns a client Socket object
                    System.out.println("Conenction established with " + clientSocket);


                    // Each new connection will be handled on a new thread and can run concurrenlty
                    ManageMultipleConnections multipleConnections = new ManageMultipleConnections(this, clientSocket);
                    clientList.add(multipleConnections);
                    multipleConnections.start();
                }

// get client socket input out streams

clientInput = clientSocket.getInputStream();
        clientoutput = clientSocket.getOutputStream();

// write to the client socket

clientoutput.write((message).getBytes());

// attempt to read form the client but we never receive any messages

BufferedReader br = new BufferedReader(new InputStreamReader(clientInput, ENCODING));
        String inputLine;
        String returnMessage;

        String msg = br.readLine();
        System.out.println(br.readLine());

        while ((inputLine = br.readLine()) != null && inputLine != "") {....do stuff

Any input appreciated.

dancingbush
  • 2,131
  • 5
  • 30
  • 66

1 Answers1

0

After a week of trying just about ever possible solution out there I found that appending a return carriage and a new line character to every message the client sent to the server via an outputtsteam did the trick.

private static final String CRLF = "\r\n"; // newline

try {

                outputStream.write((text + CRLF).getBytes());

                outputStream.flush();

            } catch (IOException e) {

                e.printstacktrace();

            }

I am not sure why this worked so anyone out there that could shed light on this would be great. I am using java swing in eclipse on Windows 10 OS.

dancingbush
  • 2,131
  • 5
  • 30
  • 66
  • You mean 'carriage return'. Reading lines requires that you send lines. – user207421 Dec 22 '19 at 07:28
  • Expand on the context please. Many tutorials I have seen do not include any new lines in the outputstream to server , or vice versa, so why in this case is my question. – dancingbush Dec 22 '19 at 07:33
  • The context is that you are reading lines, and I've already said so. No expansion required. I can't comment on uncited sources, but any code anywhere that uses `readLine()` and doesn't send line terminators is wrong. – user207421 Dec 22 '19 at 09:27