Note that there are some explanatory texts on larger screens.

plurals
  1. POClient-Server TCP communication
    text
    copied!<p>The server should send a message "Hello :: enter QUIT to exit" to the client, then the client types in any text and the server echos back the client's text adding "From server: " before their message. But there seems to be a mix up in the order and I can't seem to find where! I've been on this all day! </p> <p>This is the Server's code:</p> <pre><code> import java.net.*; public class Server { public static void main(String[] args) { int nreq = 1; try { ServerSocket sock = new ServerSocket (8080); for (;;) { Socket newsock = sock.accept(); System.out.println("Creating thread ..."); Thread t = new ThreadHandler(newsock,nreq); t.start(); } } catch (Exception e) { System.out.println("IO error " + e); } System.out.println("End!"); } } </code></pre> <p>ThreadHandler code:</p> <pre><code> import java.io.*; import java.net.*; class ThreadHandler extends Thread { Socket newsock; int n; ThreadHandler(Socket s, int v) { newsock = s; n = v; } // @SuppressWarnings("deprecation") public void run() { try { PrintWriter outp = new PrintWriter(newsock.getOutputStream(), true); BufferedReader inp = new BufferedReader(new InputStreamReader( newsock.getInputStream())); outp.println("Hello :: enter QUIT to exit"); boolean more_data = true; String line; while (more_data) { line = inp.readLine(); if (line == null) { more_data = false; } else { outp.println("From server: " + line + "\n"); if (line.trim().equals("QUIT")) more_data = false; } } newsock.close(); } catch (Exception e) { System.out.println("IO error " + e); } } } </code></pre> <p>And the Client code:</p> <pre><code> import java.io.*; import java.net.*; import java.util.Scanner; public class Client { // @SuppressWarnings("deprecation") public static void main(String args[]) { Scanner scanner = new Scanner(System.in); try { Socket s = new Socket("localhost", 8080); PrintWriter outp = new PrintWriter(s.getOutputStream(), true); BufferedReader inp = new BufferedReader(new InputStreamReader( s.getInputStream())); boolean more_data = true; System.out.println("Established connection"); String line; while (more_data) { line = inp.readLine(); String userInput = scanner.nextLine(); outp.println(userInput); if (line == null) { more_data = false; } else System.out.println(line); } System.out.println("end of while"); } catch (Exception e) { System.out.println("IO error " + e); } } } </code></pre> <p>I'm testing it out so after I'm going to make the client an Android phone - if that's possible -</p> <hr> <p>Update:</p> <p>I've changed the server's code to:</p> <pre><code>outp.println("Hello :: enter QUIT to exit \n"); boolean more_data = true; String line; while (more_data) { line = inp.readLine(); System.out.println("Message '" + line + "' echoed back to client.");// !! if (line == null) { System.out.println("line = null"); more_data = false; } else { outp.println("From server: " + line + ". \n"); if (line.trim().equals("QUIT")) more_data = false; } } newsock.close(); System.out.println("Disconnected from client number: " + n); </code></pre> <p>and added "\n" at the end of the Hello message as Luis Miguel Serrano suggested, And changed the Client's side as below:</p> <pre><code>boolean more_data = true; System.out.println("Established connection"); String line;// = inp.readLine(); while (more_data) { line = inp.readLine(); System.out.println(line); if (line == null) { // nothing read more_data = false; } else line = inp.readLine(); System.out.println(line); String userInput = scanner.nextLine(); if (userInput.trim() == "QUIT") { s.close(); System.out.println("Disconnected from server."); more_data = false; } else outp.println(userInput); } System.out.println("end of while"); </code></pre> <p>And it works fine now.</p> <p><strong>If anyone could suggest me some Android client-java server tutorials would appreciate it.</strong></p>
 

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