2

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?

buch11
  • 872
  • 3
  • 13
  • 29
  • 1
    Which examples exactly were you following? It seems _very_ strange to me that your classes derive from `Thread` rather than simple _use_ `Thread` to get their work done. – sarnold Jul 03 '11 at 01:38
  • 1
    One more thing.. Do not start threads within constructors. It's not safe because a non-fully constructed object can be visible to this thread. – Enno Shioji Jul 03 '11 at 01:50
  • @sarnold I am following examples at [link](http://download.oracle.com/javase/tutorial/networking/sockets/),\ – buch11 Jul 03 '11 at 01:56
  • @Enno Shioji can you share an example explaining the concept you are saying?I have half understood the meaning but just want to confirm...Thanks a lot – buch11 Jul 03 '11 at 01:59
  • 2
    Sure enough, [one of their examples does exactly what I think is so strange](http://download.oracle.com/javase/tutorial/networking/sockets/examples/KKMultiServerThread.java). I wish tutorial authors clearly state what is Done For Convenience vs The Right Way For Production Code. – sarnold Jul 03 '11 at 02:00
  • 1
    @buch11: Your code starts a new thread within the constructor of `newthread`. This can result in that thread seeing a incompletely constructed `newthread` object. Instead, you should call `start()` after the constructor has returned. See http://www.ibm.com/developerworks/java/library/j-jtp0618/index.html for more details. – Enno Shioji Jul 03 '11 at 03:05
  • Here's a related [example](http://stackoverflow.com/questions/3244400/socket-using-in-a-swing-applet/3245805#3245805). – trashgod Jul 03 '11 at 09:12

3 Answers3

2

You have to continually call accept() to create the other threads:

public static void main(String a[]) throws Exception{
    ServerSocket srv = new ServerSocket(4444);

    while (listening) {
        new newthread(srv.accept());
    }
}

The listening variable should be set to false if and when you want to stop the server.

Martin Larente
  • 510
  • 2
  • 9
2

Where are your run methods? Without a run method in the Thread or Runnable, I don't think that your thread will be able to do anything.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    +1 - More specifically, it will execute the default `run` method which does nothing ... and the thread will immediately terminate. – Stephen C Jul 03 '11 at 01:58
  • I have extended the Thread superclass so now no need to implement Runnable interface,i can use methods from Thread class itself. – buch11 Jul 03 '11 at 02:00
  • @Stephen C hey i have the run() method in my program,I just didnt post that part as there was no need threads were not even being generated for fraction of time... – buch11 Jul 03 '11 at 02:02
  • @buch11: true and answer edited, but you still need a `public static void run()` method. Where is that? – Hovercraft Full Of Eels Jul 03 '11 at 02:08
  • 1
    @buch11 - IMO the problem is in code that you haven't shown us. – Stephen C Jul 03 '11 at 02:39
0

Finally I have solved the problem ,the thing was socket.accept() method was not remaining active after the first client contact...nothing was wrong with the run method ,it as the property of socket.accept() method...

buch11
  • 872
  • 3
  • 13
  • 29