Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Sockets: One Server and Multiple Clients

So I created a basic client-server program in java. It starts out like this:

  1. Client connects to Server
  2. Server asks for Client's name
  3. Client responds with name
  4. Server greets Client
  5. After this, Client speaks and the Server repeats the words back

I got this to work without too much trouble using this tutorial. The problem comes whenever I try to introduce multiple clients. I thought that it would work because I'm using multiple threads, however, the second clients just hangs until the first client quits and then it starts it work (the server does accept input from the second client, but it doesn't respond with anything until the first client quits).

Here is the code I'm using:

import java.net.*;
import java.io.*;

public class Server extends Thread {
  private ServerSocket listener;

  public Server(int port) throws IOException {
    listener = new ServerSocket(port);
  }

  public void run() {
    while(true) {
      try {
        Socket server = listener.accept();
        DataOutputStream out = new DataOutputStream(server.getOutputStream());
        out.writeUTF("What is your name?");
        DataInputStream in = new DataInputStream(server.getInputStream());
        String user_name = in.readUTF();
        out.writeUTF("Hello "+user_name);
        while(true) {
          String client_message = in.readUTF();
          out.writeUTF(client_message);
        }
      }
      catch(IOException e) {
        e.printStackTrace();
      }
    }
  }

  public static void main(String[] args) {
    int port = 6006;
    try {
      Thread t = new Server(port);
      t.start();
    } catch(IOException e) {
      e.printStackTrace();
    }
  }
}

Can someone explain what I'm doing wrong?

I have looked at the using Runnable instead of Extends Thread, but I ran into even more problems there, so I want to try and work with this first.

like image 586
projectdelphai Avatar asked Sep 03 '13 01:09

projectdelphai


People also ask

Can multiple clients connect to same socket java?

@asma, yes. Both clients can run on the same machine.

Can multiple clients connect to same server socket?

Irrespective of stateful or stateless protocols, two clients can connect to same server port because for each client we can assign a different socket (as client IP will definitely differ). Same client can also have two sockets connecting to same server port - since such sockets differ by SRC-PORT .

Can a socket be both server and client?

For a server, you usually create a socket, then bind it to a specific port, and accept connections. For the client, you create a socket, and connect to a specified address (an IP address and port pair for a TCP/IP connection). The same device can run a TCP server and client at the same time.


2 Answers

Incoming connections are only handled by the line listener.accept();. But after you got a client connected, you're stuck in the while loop. You need to create a new Thread (or Runnable executed on an ExecutorService if you expect high load), and start it, then immediately accept the next connection.

like image 116
dst Avatar answered Oct 01 '22 19:10

dst


In a nutshell, this is what is going wrong.

  1. You are using exactly ONE thread as the server.
  2. Blocking this thread when you call listener.accept()

This is what you need to do:

Create two classes 1: Server - Similar to what you have now, but instead of doing the actual work of acting as an echo server, it just spawns a new Thread which starts listening on a NEW PORT (which you can select randomly), and sends the client the address for this new port. The client will then get the new port number and would try to connect to the server on the new port. 2: The Echo thread - This starts a new listener on the port passed, and does the job of echoing to whoever is listening.

OR:

You start a UDP server rather than a TCP server, and all this will not matter then, but that is out of the purview of this specific question.

like image 24
Neeraj Avatar answered Oct 01 '22 20:10

Neeraj