Subject: Why?
Author: EricJ
In response to: "Address already in use" errors
Posted on: 04/12/2007 09:35:48 PM
This means that you are out of TCP/IP connections on your machine.
The reson for running out of available ports is that if you creates a lot of connection at a rapid rapid rate, the system does no have enough time to clean up the TIME_WAIT sockets. Eventually, the ports numbers for those TIME_WAIT socks run out, and your JVM crashes.
>
> On 04/12/2007 09:29:53 PM EricJ wrote:
How many sockets can an application open concurrantly? Let's take a look at the following code:
String HOST = "myServer";
int PORT = 1234;
int NUM_OF_CONNECTION = 1000;
InetAddress addr = InetAddress.getByName(HOST);
Socket socket = null;
for(int i=0; i<NUM_OF_CONNECTION; i++){
try{
socket = new Socket(addr, PORT);
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
doSomethingWithThisConnection(socket);
try{
socket.close();
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
}
Once NUM_OF_CONNECTION reaches certain number around 5000, you most likely get somethings like:
java.net.BindException: Address already in use: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
References: