Reputation:
I have 2 classes, a server and a client. The server uses multiple threads to accept many clients. So x clients can join the same server. However in an attempt to identify the threads from the client method, I seem to have found that its not making multiple threads as the ID are the same for all clients. The code I have is as follows:
SERVER:
public class Server
{
ServerSocket serverSocket;
int portNumber;
public static volatile String userInput;
public volatile int noOfClients = 0;
public static void main(String[] args)
{
Server s = new Server();
s.startup();
}
/**
* Start the server on the user picked port
*/
public void startup()
{
try
{
System.out.println("Enter a port");
Scanner dif = new Scanner(System.in);
portNumber = Integer.parseInt(dif.nextLine());
dif.close();
serverSocket = new ServerSocket(portNumber);
newThread();
}
catch (IOException e) {
System.out.println("Error");
System.exit(0);
}
}
public void newThread()
{
Thread thread =new Thread()
{
public void run()
{
while(true) {
try {
accept();
} catch (Exception e) {
System.out.println("Error");
}
}
}
};
thread.start();
}
public void accept()
{
try
{
Socket clientSocket = serverSocket.accept();
new Thread(new ClientSocket(clientSocket)).start();
System.out.println("A new client has just connected.");
noOfClients++;
} catch(IOException e)
{
System.out.println("Error");
System.exit(0);
}
}
class ClientSocket implements Runnable {
Socket clientSocket;
public ClientSocket(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
{
try
{
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while (true)
{
userInput = in.readLine();
}
} catch (IOException e)
{
System.out.println("Error");
}
}
}
}
}
CLIENT:
public class Client
{
Socket clientSocket;
public static int threadName;
public static void main(String[] args) throws IOException {
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try {
Socket serverSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(serverSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
Thread thread = Thread.currentThread();
System.out.println("RunnableJob is being run by " + thread.getName() + " (" + thread.getId() + ")");
String userInput;
while ((userInput = stdIn.readLine()) != null)
{
out.println(userInput);
System.out.println("Server: " + userInput);
}
} catch(UnknownHostException e) {
System.out.println("error in host");
} catch(IOException e) {
System.out.println("error in IO");
}
}
}
When running two separate clients, the
System.out.println("RunnableJob is being run by " + thread.getName() + " (" + thread.getId() + ")");
line of code prints out the same. How can i fix it so that each new client connection is started within is own UNIQUE thread. so 2 clients will have 2 threads in total? Thanks :)
Upvotes: 0
Views: 140
Reputation: 46841
Multiple clients will connect at different port no with server. You can use that port no to distinguish between clients.
You can store ClientSocket
some where to retrieve other information of each client in future if needed as shown in below code.
Here is the code:
private static HashMap<Integer, ClientSocket> clientInfo = new HashMap<Integer, ClientSocket>();
class ClientSocket implements Runnable {
Socket clientSocket;
public ClientSocket(Socket clientSocket) {
this.clientSocket = clientSocket;
System.out.println(clientSocket.getPort());
clientInfo.put(clientSocket.getPort(), this);
}
...
Read more about Java Server with Multiclient communication.
Upvotes: 0
Reputation: 323
First, you are checking the thread ids for the clients, which are separate from each other, so that won't work.
However using a thread id is not a very good way to identify clients. Instead why don'y you keep a count of the number of clients, then when a new one joins, increment the number and give the client object that number as an id.
Upvotes: 1