- Joined
- Nov 30, 2015
- Messages
- 2
- Reaction score
- 0
Hi. My name is Clint this is my first time here, anyway I have a chat app can you simply my code for me because I'm not sure what will happen if a million clients connect.
<server side> My main class listen for incoming socket connection requests from socket clients, then I assign each socket object to a thread and construct the thread with the socket object because later on I want the clients to send and receive data to each other.
the main listens and accept connection...
What I would really want this code to do is accept up to an infinite number of socket clients ==> assign each to a thread and I want to be able to call any socket client's output or inpustream to communicate. I pray my question is clear enough to answer. Thank you in advance. Ps. How would you do that, what you think I'm trying to do...
<server side> My main class listen for incoming socket connection requests from socket clients, then I assign each socket object to a thread and construct the thread with the socket object because later on I want the clients to send and receive data to each other.
the main listens and accept connection...
Code:
try{
serverSocket=new ServerSocket(serverPort);
exe = Executors.newFixedThreadPool(NTHREADS);
while(true){
try{
clientSocket=serverSocket.accept();
int si;
for(si=0;si<NTHREADS;si++){
if(client[si]==null){
client[si]=new ClientThread(clientSocket,client,gui,si,server,methods);
exe.execute(client[si]);
break;
}
}
if(si==NTHREADS){
PrintStream os = new PrintStream(clientSocket.getOutputStream());
os.println("99");
os.close();
clientSocket.close();
}
} catch(IOException ioe){
gui.textArea1.append("\nError accepting connection:"+ioe.getMessage()+"\n");
}
}
}catch(IOException e){
gui.textArea1.append("\nError starting Server on port "+serverPort+"\n");
gui.textArea1.append(e.getMessage()+"\n");
}
What I would really want this code to do is accept up to an infinite number of socket clients ==> assign each to a thread and I want to be able to call any socket client's output or inpustream to communicate. I pray my question is clear enough to answer. Thank you in advance. Ps. How would you do that, what you think I'm trying to do...