Bad ServerSocket ! Listen! Listen! Listen!

Discussion in 'Java' started by Donny, Aug 1, 2004.

  1. Donny

    Donny Guest

    ok .. just wanted some help .. ( throw new YeahWeKnowException() !!)

    after extensive 're-search' , i've found out that a ServerSocket is
    supposed to go back to listening after starting a new Thread to handle
    the returned Socket...

    here's the problem-code:

    ServerSocket serv_sock = new ServerSocket(80);
    while(true) {
    Socket temp = serv_sock.accept();
    new ServiceManager(temp).start(); //code is blocking here!!
    }


    ServiceManager is a class that extends Thread.
    what is happening is that code blocks at the area shown... the while
    loop is not continued, until the socket that is passed to the
    ServiceManager is closed...
    the serv_sock doesn't go back to listening...

    .... could anyone please throw some light on the matter..??

    thanks in advance....
     
    Donny, Aug 1, 2004
    #1
    1. Advertising

  2. Donny

    kjc Guest

    #1, the ServerSocket doesn't start a new thread, you do.
    #2, The code is blocking because a) you don't have the thread properly
    coded, and b) in your thread, your're waiting for I/O

    Donny wrote:
    > ok .. just wanted some help .. ( throw new YeahWeKnowException() !!)
    >
    > after extensive 're-search' , i've found out that a ServerSocket is
    > supposed to go back to listening after starting a new Thread to handle
    > the returned Socket...
    >
    > here's the problem-code:
    >
    > ServerSocket serv_sock = new ServerSocket(80);
    > while(true) {
    > Socket temp = serv_sock.accept();
    > new ServiceManager(temp).start(); //code is blocking here!!
    > }
    >
    >
    > ServiceManager is a class that extends Thread.
    > what is happening is that code blocks at the area shown... the while
    > loop is not continued, until the socket that is passed to the
    > ServiceManager is closed...
    > the serv_sock doesn't go back to listening...
    >
    > ... could anyone please throw some light on the matter..??
    >
    > thanks in advance....
     
    kjc, Aug 1, 2004
    #2
    1. Advertising

  3. Donny wrote:
    > ok .. just wanted some help .. ( throw new YeahWeKnowException() !!)
    >
    > after extensive 're-search' , i've found out that a ServerSocket is
    > supposed to go back to listening after starting a new Thread to handle
    > the returned Socket...
    >
    > here's the problem-code:
    >
    > ServerSocket serv_sock = new ServerSocket(80);
    > while(true) {
    > Socket temp = serv_sock.accept();
    > new ServiceManager(temp).start(); //code is blocking here!!
    > }
    >
    >
    > ServiceManager is a class that extends Thread.
    > what is happening is that code blocks at the area shown... the while
    > loop is not continued, until the socket that is passed to the
    > ServiceManager is closed...
    > the serv_sock doesn't go back to listening...
    >
    > ... could anyone please throw some light on the matter..??
    >
    > thanks in advance....


    I think you should have a good look at the ServiceManager constructor,
    as I think this is where the problem is. I say that because I don't
    think Thread.start() ever blocks for a significant time. You could
    try breaking it down like this to prove the point:

    ServerSocket serv_sock = new ServerSocket(80);
    while(true) {
    System.out.println("Waiting to accept...");
    Socket temp = serv_sock.accept();
    System.out.println("Creating ServiceManager...");
    ServiceManager sm = new ServiceManager(temp);
    System.out.println("Starting service manager...");
    sm.start();
    }

    Steve
     
    Steve Horsley, Aug 1, 2004
    #3
  4. Donny

    Donny Guest

    Thanks Steve and kjc for throwing some light on the matter..

    Some doubts about the blocking on I/O part ....

    < for reference..>

    ServerSocket serv_sock = new ServerSocket(80);
    while(true) {
    Socket temp = serv_sock.accept();
    new ServiceManager(temp).start(); //code is blocking here!!
    }


    In my program, each new ServiceManager waits on unique queues, for a
    message that's relevant to the particluar connection it is initialized
    with..
    I wanted them all to wait independent of each other..
    would this cause the whole while loop to lock??

    meanwhile, i'll try the suggestions.. thanks again..
     
    Donny, Aug 1, 2004
    #4
  5. Donny

    kjc Guest

    Be sure that the waiting on the Queue part happens AFTER the thread has
    forked. It sounds like you are doing this BEFORE the thread has started.


    Donny wrote:
    > Thanks Steve and kjc for throwing some light on the matter..
    >
    > Some doubts about the blocking on I/O part ....
    >
    > < for reference..>
    >
    > ServerSocket serv_sock = new ServerSocket(80);
    > while(true) {
    > Socket temp = serv_sock.accept();
    > new ServiceManager(temp).start(); //code is blocking here!!
    > }
    >
    >
    > In my program, each new ServiceManager waits on unique queues, for a
    > message that's relevant to the particluar connection it is initialized
    > with..
    > I wanted them all to wait independent of each other..
    > would this cause the whole while loop to lock??
    >
    > meanwhile, i'll try the suggestions.. thanks again..
     
    kjc, Aug 2, 2004
    #5
  6. Donny

    Donny Guest

    >
    > Be sure that the waiting on the Queue part happens AFTER the thread has
    > forked. It sounds like you are doing this BEFORE the thread has started.
    >


    Boing!!
    No progress ...

    I've checked the code.. and the waiting on the queue happens only
    in the run() method.. it is called only *after* the thread is invoked,
    right?

    beats me...!
     
    Donny, Aug 2, 2004
    #6
  7. Donny wrote:
    >>Be sure that the waiting on the Queue part happens AFTER the thread has
    >>forked. It sounds like you are doing this BEFORE the thread has started.
    >>

    >
    >
    > Boing!!
    > No progress ...
    >
    > I've checked the code.. and the waiting on the queue happens only
    > in the run() method.. it is called only *after* the thread is invoked,
    > right?
    >
    > beats me...!

    Did you split this line:

    new ServiceManager(temp).start();

    into two lines to prove whether it was the constructor or the
    call to start() that was blocking? I still think it must be the
    constructor rather than start(). Just looking at the code won't
    help much.

    Maybe the constructor is trying to acquire a lock that another
    thread is holding.

    Steve
     
    Steve Horsley, Aug 2, 2004
    #7
  8. Donny

    kjc Guest

    I have to agree with one of the other posters.
    I strongly believe that it is happening in the ctor of your class.
    Can you post some code.


    Donny wrote:
    >>Be sure that the waiting on the Queue part happens AFTER the thread has
    >>forked. It sounds like you are doing this BEFORE the thread has started.
    >>

    >
    >
    > Boing!!
    > No progress ...
    >
    > I've checked the code.. and the waiting on the queue happens only
    > in the run() method.. it is called only *after* the thread is invoked,
    > right?
    >
    > beats me...!
     
    kjc, Aug 2, 2004
    #8
  9. Donny

    tom bender Guest

    Show us your ctor.

    Donny wrote:
    >>Be sure that the waiting on the Queue part happens AFTER the thread has
    >>forked. It sounds like you are doing this BEFORE the thread has started.
    >>

    >
    >
    > Boing!!
    > No progress ...
    >
    > I've checked the code.. and the waiting on the queue happens only
    > in the run() method.. it is called only *after* the thread is invoked,
    > right?
    >
    > beats me...!
     
    tom bender, Aug 2, 2004
    #9
  10. Donny

    Donny Guest

    Well.. i did try adding the println's before and after the
    contructor..
    It is getting called fine..
    the blocking is on the start() call...

    However, here's the constructor:

    public ServiceManager(Socket socket, /*..some app specific variables
    */ )throws Exception
    {
    this.socket = socket;
    this.input = socket.getInputStream();
    this.output = socket.getOutputStream();
    br =
    new BufferedReader(new InputStreamReader(input));
    /*
    * some Strings and int's initialized..
    */

    }

    would the creation of Input and OutputStreams block? No chance, i
    guess..
    what about the buffered reader? it's not a static class member or
    anything...

    any clues? thanks for all the help!
     
    Donny, Aug 4, 2004
    #10
  11. Donny

    kjc Guest

    Wait, are you subclassing thread or implementing Runnable ??
    Also, move all this code into your run(){} method.



    Donny wrote:
    > Well.. i did try adding the println's before and after the
    > contructor..
    > It is getting called fine..
    > the blocking is on the start() call...
    >
    > However, here's the constructor:
    >
    > public ServiceManager(Socket socket, /*..some app specific variables
    > */ )throws Exception
    > {
    > this.socket = socket;
    > this.input = socket.getInputStream();
    > this.output = socket.getOutputStream();
    > br =
    > new BufferedReader(new InputStreamReader(input));
    > /*
    > * some Strings and int's initialized..
    > */
    >
    > }
    >
    > would the creation of Input and OutputStreams block? No chance, i
    > guess..
    > what about the buffered reader? it's not a static class member or
    > anything...
    >
    > any clues? thanks for all the help!
     
    kjc, Aug 4, 2004
    #11
  12. Donny wrote:
    > Well.. i did try adding the println's before and after the
    > contructor..
    > It is getting called fine..
    > the blocking is on the start() call...


    You haven't overridden start() have you?
    If not, then the only other idea that I have is that maybe
    your newly started thread is diving into an infinite loop
    and you are on a machine that doesn't do preemptive task
    switching.

    Steve
     
    Steve Horsley, Aug 4, 2004
    #12
  13. On Wed, 04 Aug 2004 23:58:14 +0100, Steve Horsley wrote:

    > You haven't overridden start() have you?


    A good answer to that is an SSCCE, then the
    folks that are experienced at this stuff
    could stop guessing..
    <http://www.physci.org/codes/sscce.jsp>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Aug 5, 2004
    #13
  14. Donny

    Donny Guest

    Ok ... guys thanx for your time and your suggestions...
    I think i'll rewrite the entire code from scratch, and prepare an
    SSCCE ...
    i realise how little material you had to work on ...

    thanks for the write-up Andrew..
    it's an essential part of posting etiquette i feel .. but sometimes
    people get turned away by lots of code.. that's why i didn't include
    them.

    anyways... brb! :)
     
    Donny, Aug 5, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Kuhn
    Replies:
    4
    Views:
    960
    Thomas Kuhn
    Jul 7, 2003
  2. Jason Teagle
    Replies:
    12
    Views:
    3,420
    Jason Teagle
    Oct 5, 2003
  3. Abraham Khalil
    Replies:
    1
    Views:
    531
    Knute Johnson
    Jan 26, 2004
  4. Thomas Hawkins

    Unusual ServerSocket Behaviour

    Thomas Hawkins, Jan 28, 2004, in forum: Java
    Replies:
    3
    Views:
    509
    Steve Horsley
    Jan 30, 2004
  5. rantingrick
    Replies:
    44
    Views:
    1,301
    Peter Pearson
    Jul 13, 2010
Loading...

Share This Page