Close all sockets after use? server side

Do I need to close all sockets after using it? Where should I put them in this code? My program works fine when I run it. However, when I re-run it, he said "Exception in thread" main "java.net.BindException: address already in use: JVM_Bind". Therefore, I think that I did not close all sockets after using it.

import java.io.*;
import java.net.*;
import java.util.*;
public class Server2 {
public static void main(String args[]) throws Exception {
    int PORT = 5555;      // Open port 5555
    //open socket to listen
    ServerSocket server = new ServerSocket(PORT);
    Socket client = null;
    while (true) {
        System.out.println("Waiting for client...");
        // open client socket to accept connection
        client = server.accept();
        System.out.println(client.getInetAddress()+" contacted ");
        System.out.println("Creating thread to serve request");
        ServerStudentThread student = new ServerStudentThread(client);
        student.start();


    }

}

}

+3
source share
3 answers

Call server.close()in block finally.

ServerSocket server = new ServerSocket(PORT);
try {
    while (true) {
        System.out.println("Waiting for client...");
        // open client socket to accept connection
        Socket client = server.accept();
        System.out.println(client.getInetAddress()+" contacted ");
        System.out.println("Creating thread to serve request");
        ServerStudentThread student = new ServerStudentThread(client);
        student.start();
    }
} finally {
    server.close();
}
+2
source

Address already in use: JVM_Bind- means that after the previous work, your operating system is not closed. It closes with a timeout of about 30-180 seconds.

, java, C , bind :

int yes = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

: () SO_REUSEADDR sockfd.

java .

0
  • You start an endless while loop, have a boolean to say when to stop, I think you are not exiting gracefully, so the port is not closed.

Maybe you can try like this

import java.io.*;
import java.net.*;
import java.util.*;
public class Server2 {
static int NUM_CONN_TO_WAIT_FOR=15;
boolean exitServer =false;
public static void main(String args[]) throws Exception {
    int PORT = 5555;      // Open port 5555
    //open socket to listen
    ServerSocket server = new ServerSocket(PORT);
    Socket client = null;
    static int connections =0;
try
{

    while (!exitServer ) {
        System.out.println("Waiting for client...");
        // open client socket to accept connection
        if ( connections < NUM_CONN_TO_WAIT_FOR )
        {
          client = server.accept();
          System.out.println(client.getInetAddress()+" contacted ");
          System.out.println("Creating thread to serve request");
          ServerStudentThread student = new ServerStudentThread(client);
          student.start();
        } else
        {
            exitServer =true;
        } 
        connections++;

    }
} catch (Exception e)
{
   System.out.println(e.printStackTrace());
}
finally
{

   if ( client != null)
        client.close();

   if ( server!= null)
        server.close();

}

}

}
0
source

Source: https://habr.com/ru/post/1791641/


All Articles