Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Two TCP Servers (threads) on the same computer

Reply
Thread Tools

Two TCP Servers (threads) on the same computer

 
 
Sona
Guest
Posts: n/a
 
      09-28-2003
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

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      09-28-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird issue, same code, same browser, two different apache servers,very different css bluebaron HTML 3 11-04-2009 07:13 PM
Remote desktop on two servers at the same LAN Martin Cisco 4 12-14-2006 10:31 AM
Need same viewstate for same page across multiple servers =?Utf-8?B?UHVuaXNoZXI=?= ASP .Net 1 03-23-2006 03:54 PM
NAT two outside TCP ports to one inside TCP port Kevin Cisco 1 11-10-2004 08:15 AM
TCP servers in Python - two processes want to use same port Pif Paf Python 4 02-24-2004 12:46 PM



Advertisments