Reputation: 417
I have created client server application in the client side when i send multiple requests to the server some time later it gives below error.When i monitor TCPview there are lot of port connections on CLOSE_WAIT status.Has any one come across with such issue.Please find the bellow error log
java.net.SocketException: No buffer space available (maximum connections reached?): connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at com.lk.cc.socketserver.Main.isHostRunning(Main.java:90)
at com.lk.cc.socketserver.Main.main(Main.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Exception in thread "main" java.lang.NullPointerException
at com.lk.cc.socketserver.Main.isHostRunning(Main.java:102)
at com.lk.cc.socketserver.Main.main(Main.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Server Host class
public class ServerHost extends Thread {
private ServerSocket serverSocket;
public ServerHost(int port) throws IOException {
serverSocket = new ServerSocket(port);
}
public void run() throws IOException{
while (true) {
Socket server = null;
DataInputStream in = null;
DataOutputStream out = null;
try {
System.out.println("Host 1 Established .. ");
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
server = serverSocket.accept();
System.out.println("Just connected to "
+ server.getRemoteSocketAddress());
in =
new DataInputStream(server.getInputStream());
if (in.available() > 0) {
System.out.println("Recieved client message :" + in.readUTF());
out = new DataOutputStream(server.getOutputStream());
// send recieved response from host X to client
out.writeUTF("Response from Host 1 " + server.getLocalSocketAddress() + " \nGoodbye!");
System.out.println("client socket isClosed() " + server.isClosed());
}
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
// break;
} catch (IOException e) {
e.printStackTrace();
// break;
} finally {
if (out != null) {
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (server != null) {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
int port = 7881;
try {
Thread t = new ServerHost(port);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client
public class Main {
private static ArrayList<HostConnections> hostList;
public static void init() {
hostList = new ArrayList<HostConnections>();
hostList.add(new HostConnections("localhost", 7881));
hostList.add(new HostConnections("localhost", 7882));
hostList.add(new HostConnections("localhost", 7883));
hostList.add(new HostConnections("localhost", 7884));
}
public static void main(String[] args) {
// write your code here
boolean hostStatus=true;
init();
while (hostStatus) {
System.out.println("Used " + hostList.get(0));
HostConnections nextHost = hostList.get(0);
//Collections.rotate(hostList, -1);
System.out.println(hostList);
hostStatus = isHostRunning(nextHost);
System.out.println(hostStatus);
}
}
private static boolean isHostRunning(HostConnections availableHost) {
boolean isAlive = false;
Socket client=null;
try {
client = new Socket(availableHost.getHostIpAddress(), availableHost.getHostPort());
//client.getInputStream().close();
// client.getOutputStream().close();
isAlive = true;
} catch ( Exception e) {
e.printStackTrace();
}finally {
try {
//client.getOutputStream().flush();
client.close();
client.isClosed();
System.out.println(client.isClosed());
} catch (IOException e) {
e.printStackTrace();
}
}
return isAlive;
}
}
My requirement is to check all the connections in the list are available everytime. Thanks
Upvotes: 1
Views: 2744
Reputation: 310907
You're leaking sockets somewhere. CLOSE_WAIT
means that TCP has received a close from the peer and is waiting for the local application to close the socket.
You should also try a sleep in that testing loop. You're burnng sockets like there is no tomorrow.
In fact I question the entire purpose. The only reliable way to know whether any resource is available is to try to use it and handle the errors as they arise in the normal course of execution. Anything else is tantamount to fortune-telling.
NB:
flush()
before close()
is redundant.isClosed()
and available()
is usually a waste of time, and this is no exception.Upvotes: 2