Java client-server chat (server not receiving string)
<p>trying to make a simple client server chat program. I've already go it so that the server reads a user input which is then sent to the client. the client then receives this and displays it. I then have the client reading the user input and sending it to the server however the server doesn't receive it. Heres my code:</p>

<p>Client:</p>

<pre><code>import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.Scanner;


class client {
   public static void main(String args[]) {
      try {
         Socket skt = new Socket("localhost", 1234);
         BufferedReader in = new BufferedReader(new
         InputStreamReader(skt.getInputStream()));
         System.out.print("Waiting for server to respond... \n");

         while (!in.ready()) {}
         System.out.print("Received Message: ");
         System.out.println(in.readLine()); // Read one line and output it

         System.out.print("\n");
         //in.close();

         System.out.print("Message:");
         Scanner sc = new Scanner (System.in);
         String data2 = sc.nextLine();
         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         System.out.print("Sending Message: '" + data2 + "'\n");
         out.print(data2);
         out.close();


      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}
</code></pre>

<p>Server:</p>

<pre><code>import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.Scanner;



class server {


   public static void main(String args[]) {

      try {
         System.out.println("Waiting for client connection...");
         ServerSocket srvr = new ServerSocket(1234);
         Socket skt = srvr.accept();
         System.out.println("Client has connected!\n");
         System.out.print("Message:");
         Scanner sc = new Scanner (System.in);
         String data = sc.nextLine();
         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         System.out.print("Sending Message: '" + data + "'\n");
         out.print(data);

         BufferedReader in = new BufferedReader(new
         InputStreamReader(skt.getInputStream()));


         while (!in.ready()) {out.close();}      
         System.out.print("Received Message: ");
         System.out.println(in.readLine()); // Read one line and output it
         System.out.print("\n");


         //in.close();

         //in.close();
         //out.close();
         //skt.close();
         //srvr.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}
</code></pre>

<p>Both the client and the server use the same code for send/receive so I don't understand why the server won't receive.</p>

<p>Cheers,</p>

<p>Tom</p>
