Note that there are some explanatory texts on larger screens.

plurals
  1. POCommunication between Client and Server using Sockets
    primarykey
    data
    text
    <p>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.</p> <pre><code>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; } </code></pre> <p>Server Code:</p> <pre><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(); } } } </code></pre> <p>The code is a bit of a mess and I can see a few duplicates, I have commented where I feel the problems occour. </p> <p>Thanks for any help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload