Two TCP Servers (threads) on the same computer

Discussion in 'Java' started by Sona, Sep 28, 2003.

  1. Sona

    Sona Guest

    Hi,

    I want to create different threads that will run from the same program
    and will listen for incoming TCP Connections. Doing this with a single
    thread works fine.. but when I try to launch another thread, it says
    java.net.BindException: Address already in use: JVM_Bind.

    I even tried making the second thread listen on a different port but
    this is not working. How can I make this work? Thanks


    Sona
    Sona, Sep 28, 2003
    #1
    1. Advertising

  2. On Mon, 29 Sep 2003 03:04:25 +1000, Sona wrote:
    > I want to create different threads that will run from the same
    > program and will listen for incoming TCP Connections. Doing this
    > with a single thread works fine.. but when I try to launch another
    > thread, it says java.net.BindException: Address already in use:
    > JVM_Bind.
    >
    > I even tried making the second thread listen on a different port but
    > this is not working. How can I make this work? Thanks


    There should be no problem creating a second ServerSocket that listens
    on a *different* port number, I can't say why that didn't work for you
    without seeing any code.

    However for a given port number, there can be only one ServerSocket
    (unless they also specify different local addresses), which is why
    your attempt to create two failed. You could create the ServerSocket
    once and wait in accept() from different threads, but that isn't the
    normal way to handle multiple connections.

    Instead, have one thread that creates the ServerSocket and listens for
    incoming connections. As each connection arrives, create a thread to
    handle the resulting Socket (or perhaps pass it to a thread pool):

    ServerSocket ss = new ServerSocket(port);

    while (!done) {
    Socket s = ss.accept();
    Runnable r = new MyRunnable(s);
    Thread t = new Thread(r);
    t.start();
    }

    Your "MyRunnable" should close the Socket before terminating.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Sep 28, 2003
    #2
    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. Mark
    Replies:
    3
    Views:
    502
    bruce barker
    Aug 26, 2004
  2. =?Utf-8?B?UHVuaXNoZXI=?=

    Need same viewstate for same page across multiple servers

    =?Utf-8?B?UHVuaXNoZXI=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    404
    John Timney \( MVP \)
    Mar 23, 2006
  3. Pif Paf
    Replies:
    4
    Views:
    1,595
    Irmen de Jong
    Feb 24, 2004
  4. bluebaron
    Replies:
    3
    Views:
    726
    Jonathan N. Little
    Nov 4, 2009
  5. Replies:
    17
    Views:
    314
    Big and Blue
    Jun 22, 2006
Loading...

Share This Page