I am new to socket programming and trying to write a simple cmd line chat application.
Now i use the code below to accept a connection and then create a new thread for the same,but no new thread is created ,and no more then one clients are supported(which is usual when threads are not used),
public class chatserver extends Thread{
public static Socket client;
public static void main(String a[]) throws Exception{
ServerSocket srv = new ServerSocket(4444);
if((client = srv.accept())!=null){
new newthread(client);
}
}
}
class newthread extends Thread{
private Socket client;
public newthread(Socket client){
super("chatchild");
this.client = client;
start();
}
why the threads are not created?I reffered examples at “oracle.com” one of which contains the code for the same but I am not able to figure out the exact sequence what will happen and when???The snippet from exaple code that accepts a connection and creates the thread::
while (listening)
new KKMultiServerThread(serverSocket.accept()).start();
now here listening is bool var sat to true but it is never set to false anywhere in the code.???How does this work?
You have to continually call accept() to create the other threads:
The listening variable should be set to false if and when you want to stop the server.