I tried to find in exact solution for this problem but I'm not able to find it
I have written a while loop to accept client sockets in server program
because of this I'm unable to close the serversocket as it is always listening
here is my server code
public class nserver {
public static void main(String[] args) throws IOException {
int port=4413;
//server is running on port
try(ServerSocket ss = new ServerSocket(port)){;
// socket accepts data from the incoming clients
while(true){
Socket socket = ss.accept();
// id I don't put the if case then this loop never breaks and
// although I wrote ss.close() - it will never reached - so use if case
if(socket.equals(null))
break;
System.out.println("Server is running on port "+port);
//socket.getInetAddress gives address of client ip not the server ip as the socket is mada as client socket
System.out.println("server recieved connection from "+socket.getInetAddress()+" : "+socket.getPort());
//create two threads to send and receive from client
rxClientData rx = new rxClientData(socket);
Thread t = new Thread(rx);
t.start();
sdServerData sd = new sdServerData(socket);
Thread t2 = new Thread(sd);
t2.start();
// if I put ss.close() here then it will accept only one client socket and serversocket will be closed after this line
}
ss.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
how to close serversocket after my client socket requests,I have 3 clients
with for loop I will have a limit of no.of client sockets,but a server should accept so many no.of requests - so I used while loop
for a server do we need to keep serversocket open in real world scenario ? - so that it accepts client requests all the time