The problem: Client doesn't receive any message.
Here is the full code for both client and server:
CLIENT
public class Client extends Socket{
public Client(String hostName, int port) throws UnknownHostException, IOException {
super(hostName,port);
BufferedReader in = new BufferedReader(new InputStreamReader(getInputStream()));
while(true) {
String line = in.readLine();
System.out.println("Text received: " + line);
}
}
SERVER
public final class Server extends ServerSocket{
public Server(int port) throws IOException {
super(port);
System.out.println("Server waiting for client 1");
Socket client1 = accept();
PrintWriter writer = new PrintWriter(client1.getOutputStream(), true);
writer.write("Hello user 1");
System.out.println("Server waiting for client 2");
Socket client2 = accept();
PrintWriter writer2 = new PrintWriter(client2.getOutputStream(), true);
writer2.write("Hello user 2");
System.out.println("Clients connected");
}
- I start the server to listen to port 4444
- I start the clients with hostname of "localhost" and port 4444
Socket client1 = accept();and see if it prints.println(). NB Your read loop is not adequate. It doesn't test for end of stream. It should bewhile ((line = in.readLine()) != null) {...}.