London Student
London Student

Reputation: 915

Communication between Client and Server using Sockets

Okay this is a revised question from earlier today, I have included code to help explain the problem. I am sending two messages from the client to the server. The server then picks the messages up and processes them. The server finally attempts to send a message back to the client(please note in the server code "testmessage"), it is here I am having problems. Either I am not recieving the message at the client side or sending it incorrectly from the server side.

public class ClientConnection {

String address, language, message;
int portNumber;
Socket clientSocket = null;

public ClientConnection(String lan, String mes, String add, int pn) throws IOException{
    address = add;
    portNumber = pn;
    language = lan;
    message = mes;
}
public String createAndSend() throws IOException{
    // Create and connect the socket
    Socket clientSocket = null;
    clientSocket = new Socket(address, portNumber);
    PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(),true);
    BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));     

    // Send first message - Message is being correctly received
    pw.write(language+"\n"); 
    pw.flush(); 
    // Send off the data  

    // Send the second message - Message is being correctly received
    pw.write(message); 
    pw.flush();
    pw.close();
    // Send off the data

    // NOTE: Either I am not receiving the message correctly or I am not sending it from the server properly.
    String translatedMessage = br.readLine();       
    br.close();
    //Log.d("application_name",translatedMessage); Trying to check the contents begin returned from the server.
    return translatedMessage;
}

Server Code:

public class ServerConnection {

public static void main(String[] args) throws Exception {       
    // Delete - Using while loop to keep connection open permanently.
    boolean status = false;
    while( !status){
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }
        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }
        // Delete - Working as of here, connection is established and program runs awaiting connection on 4444
        BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));     
        String language = br.readLine();
        String message = br.readLine();
        // Test - Works
        System.out.println(language);      
        // Test - Works
        System.out.println(message);
        // Delete - Working as of here, both messages are passed and applied. Messages are received as sent from client.
        TranslateMessage tm = new TranslateMessage();
        String translatedMessage = tm.translateMessage(language, message);

        // NOTE: This seems to be where I am going wrong, either I am not sending the message correctly or I am not receiving it correctly..
        // PrintWriter writer = new PrintWriter(new BufferedOutputStream(clientSocket.getOutputStream()));
        PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(),true);
        // Send translation back 
        System.out.println(translatedMessage);
        // pw.write(translatedMessage+"\n"); 
        pw.write("Return test"); // Test message!
        pw.flush(); 
        // Send off the data 
        pw.close();
        br.close();
        clientSocket.close();
        serverSocket.close();
    }
}
}

The code is a bit of a mess and I can see a few duplicates, I have commented where I feel the problems occour.

Thanks for any help!

Upvotes: 0

Views: 5911

Answers (1)

Jim Morris
Jim Morris

Reputation: 2890

You are using BufferedReader.readLine() to read the response from the server, but in the test case you are sending a string that is not terminated with a \n or \r\n, so it will not get the line as far as I can tell from the docs...

public String readLine()
            throws IOException

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:
    A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

An additional suggestion...

When writing request response protocols like this I would not rely on line endings to terminate the requests or responses. Typically I would use either a fully formatted JSON string, or my preference is for a binary protocol where all requests and response are prepended with a binary count (usually 4 bytes bigendian/network byte order). Then the client and server reads the 4 bytes then reads the number of bytes that follow. This handles the packet fragmentation that typically happens over network connections, also it helps avoid DOS attacks by malicious users sending long strings that never terminate.

In Java you can use ByteBuffer.order() to handle bigendian numbers.

Upvotes: 1

Related Questions