Java sockets. Communication between server and client

I am trying to connect a gui client with a gui server. The connection is making, but I don't see any messages between the two applications. (I have to get the SERVER HERE on the client and the CLIENT HERE on the server)

Client connection code:

@Override
public void ClientRunning(){
    try {
       connectToServer();
        setStreams();
        ClientRun();

    }catch(EOFException oefException){
        showMessage("\n Client terminated the connection\n");
    }catch(IOException ioException){
        ioException.printStackTrace();
    }finally{
        close();
    }
}



public void connectToServer() throws IOException{
    showMessage("Attempting Connection... \n");
    connection = new Socket(InetAddress.getByName(serverIP),6789);
    showMessage("Connected to: "+ connection.getInetAddress().getHostName());
}


public void setStreams() throws IOException{
   output = new PrintWriter(connection.getOutputStream(),true);
   output.flush();
   input= new BufferedReader(new InputStreamReader(connection.getInputStream()));

    showMessage("\n Streams are now set. \n");
}

public void close(){
    showMessage("\n closing...");
    try{
      output.close();
      input.close();
      connection.close();

    }catch(IOException ioException){
        ioException.printStackTrace();
    }
}
public void showMessage(final String text){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            cwindow.append(text);
        }
    });
}

public void sendMessage(String message){
    output.write("CLIENT - "+message);
    output.flush();
    showMessage("\nCLIENT - "+message);
}

private void ClientRun() throws IOException{
    String message="CLIENT HERE!";
    sendMessage(message);
    do{
        try{
            message=input.readLine();
            showMessage("\n"+message);
        }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");
    }

    }while(message!="EXIT");
    }

      

(I / O is defined in the GUI class in which this client class is extended. Defined as "BufferedReader input; PrintWriter protected output;")

Also, the server code:

public class ServerClass {


private ServerSocket server;
private Socket connection;
private BufferedReader input;
private BufferedWriter output;


public void startServer(){
    try{
        server=new ServerSocket(6789,100);
        while(true){
            try{
                waitForConnection();
                setStreams();
                ServerRunning();
            }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");

            }finally{
            close();

        }
        }

    }catch(IOException ioException){
        ioException.printStackTrace();
    }
}

private void waitForConnection() throws IOException{
    showMessage("Waiting for someone to connect... \n");
    connection=server.accept();
    showMessage("Now connected to "+ connection.getInetAddress().getHostName());
}

private void setStreams() throws IOException{
   output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
   output.flush();
   input= new BufferedReader(new InputStreamReader(connection.getInputStream()));

    showMessage("\n Streams are now set. \n");
}
public void ServerRunning() throws IOException{
    String message="SERVER HERE!";
    sendMessage(message);
    do{
        try{

            message=input.readLine();
            showMessage("\n"+message);
        }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");
    }

    }while(message!="EXIT");

  }
private void close(){
    showMessage("\n Closing connections... \n");
    try{
        output.close();
        input.close();
        connection.close();

}catch(IOException ioException){
    ioException.printStackTrace();
}
}
private void showMessage(String text){
    System.out.println(text);
}

private void sendMessage(String message){
    try{
       output.write("SERVER - "+message);
        output.flush();
        showMessage(message);
    }catch(IOException ioException){
        System.out.println("\n ERROR!");
    }
}

      

The connection seems to be fine, so I don't understand what happened. Any help would be appreciated.

PS: I also tried PrintWriter on the server and also tried trying to catch on a stream expression, the problem remains.

+3


source to share


3 answers


You are using readLine()

that expects a newline character. either add \n

to messages you send or don't use readLine()

.



+2


source


[Solved] As TedTrippin and Alfie said, readline () messed up my code. After adding "\ n" in every message, everything seems smooth! I also changed printwriter to bufferwritter in the client code, and also added a try-catch function to sendMessage, because, for some reason, the printwritter seems to be causing some problems as well. Anyway, everything is done and done! Thank you, guys!

UPDATED CLIENT CLIENT:

private void ClientRun() throws IOException{
    String message="CLIENT HERE! \n"; <----added \n here.
    sendMessage(message);
    do{
        try{
            message=input.readLine();
            showMessage("\n"+message);
        }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");
    }

    }while(message!="EXIT");
    }

public void sendMessage(String message){
    try{                                 <-----added try-catch statement
       output.write("CLIENT - "+message+"\n"); <----adding "\n" here is super userful, instead of having to add \n in each message.
        output.flush();
        showMessage(message);
    }catch(IOException ioException){
        System.out.println("\n ERROR!");
    }
}

protected BufferedReader input;
protected BufferedWriter output; <----changed printwriter to BufferWriter

      

UPDATED SERVER CODE:



public void ServerRunning() throws IOException{
    String message="SERVER HERE! \n";    <--- added \n here as well.
    sendMessage(message);
    do{
        try{

            message=input.readLine();
            showMessage("\n"+message);
        }catch(EOFException eofException){
                showMessage("\n Server ended the connection!");
    }

    }while(message!="EXIT");

      

}

Thanks guys!

+1


source


After compiling this code with some of them that I've written in the past using sockets, I noticed that you are using output.write(string)

and input.readline()

- they don't mix well, readline()

expects a newline and write

won't produce one.

Replace write()

with println()

.

0


source







All Articles