0

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

Manoj Kalluri
  • 1,384
  • 2
  • 14
  • 27
  • Why have you got the socket accept inside an infinite loop? – Thraydor Apr 18 '16 at 14:28
  • I have multiple clients ,so I need to create multiple socket accept,if I use a for loop I will be limited by a number,if in I increase the number of clients in future then it is a problem.my goal is to create something near to a real world server where it accepts so many no.of requests i.e socket accepts – Manoj Kalluri Apr 18 '16 at 14:31
  • when I did as you said ,it never reached the line below the if case - so never a thread was created – Manoj Kalluri Apr 18 '16 at 14:43
  • Of course - daft moment from me. The loop wouldn't get started properly because it hasn't been accepted yet. Apologies. I'll take away the other comment ;) – Thraydor Apr 18 '16 at 14:46
  • I'm not entirely sure on this myself. I found some links if you haven;t already read them. http://stackoverflow.com/questions/8001204/how-to-close-serversocket-connection-when-catching-ioexception http://stackoverflow.com/questions/17385452/serversocket-is-it-really-necessary-to-close-it – Thraydor Apr 18 '16 at 14:59
  • Other people seem to be putting the accept inside a `try/catch`. Then you can put the close inside the `finally` part. Although I think that once it is finished with the `try/catch` it will close it anyway, but don't take my word on that. http://stackoverflow.com/questions/10131377/socket-programming-multiple-client-to-one-server – Thraydor Apr 19 '16 at 09:08

0 Answers0