Subject: "Address already in use" errors
Author: EricJ
Posted on: 04/12/2007 09:29:53 PM
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)
Replies:
References: