Re: Basic SocketServer & Thread Question



Lee Fesperman wrote:

shakah wrote:

john wrote:

How could I have a SocketServer accept connections from a handful of known
IP addresses, when the connections come and go in random order? Right now I
have one instance of ServerSocket accepting connections from all client IP
addresses:

ServerSocket ss = new ServerSocket(5000);  // listen on port 5000
while (true) {
 Socket s = serverSocket.accept();
 (new MyProcessor(s)).start();  // process stream in a new thread
}

But I want to bind to only specific IP addresses, something like this:

static final String[] validIP = {"100.200.300.1", "100.200.300.2",
"100.200.300.3"};
while (true) {
 for (int i=0; i<validIP.length; i++) {
   Socket s = serverSocket.bind(new InetSocketAddress(validIP[i], 5000));
   (new MyProcessor(s)).start();  // process stream in a new thread
 }
}

But this design requires the clients to connect in order. I could create a
thread that waits for a connection from each valid IP, but that would mean
having multiple instances of ServerSocket, and multiple ServerSocket
instances cannot listen to the same port number simultaneously. What can I
do?

Any hints or examples would be appreciated. I could just have MyProcessor
disconnect if it's not a valid address, but I was looking for a more elegant
solution.

Thanks.

-John

Try extending your first example to use Socket.getInetAddress().getHostAddress() on the accept'ed socket, e.g. something like:

 ServerSocket ss = new ServerSocket(5000);  // listen on port 5000
 while (true) {
   Socket s = serverSocket.accept();
   String sIncomingHostAddress = s.getInetAddress().getHostAddress() ;
   if(/*sIncomingHostAddress is in list of acceptable hosts*/) {
     (new MyProcessor(s)).start();  // process stream in a new thread
   }
   else {
     s.close() ;
   }
 }


That's a reasonable solution depending on the circumstances. Alternatively, you can create a ServerSocket for each acceptable host and do accepts in separate threads.

I don't understand how this suggestion is supposed to work but you can also control the hosts which are accepted via a policy file SocketPermission/accept setting if you are prepared to let a SecurityManager loose.
.




Relevant Pages

  • Re: Basic SocketServer & Thread Question
    ... when the connections come and go in random order? ... >> have one instance of ServerSocket accepting connections from all client IP ... > Socket.getInetAddress().getHostAddress() on the accept'ed socket, ... * FirstSQL/J Object/Relational DBMS ...
    (comp.lang.java.programmer)
  • Re: Basic SocketServer & Thread Question
    ... when the connections come and go in random order? ... > have one instance of ServerSocket accepting connections from all client IP ... > having multiple instances of ServerSocket, ... Socket.getInetAddress().getHostAddress() on the accept'ed socket, ...
    (comp.lang.java.programmer)
  • Re: Question with regard to Sockets
    ... Atirya Yodha wrote: ... > a) Socket is used by the client to initiate ... > listen for incoming connections and using a ServerSocket ... > then how can I make connections to certain web servers (an NTP ...
    (comp.lang.java.programmer)
  • throttling iocp threads in async server
    ... I'm designing an async socket server and I've read that the IOCP thread pool ... threads by only allowing up to MAX connections to be accepted at any given ... accepting a new connection increments N and closing an accepted socket ...
    (microsoft.public.dotnet.framework)
  • Re: Problem with BackLog (TCP Queue)..
    ... Trying to set the backlog above this value will result in either ... you are guaranteed a thread switch between accepting ... with a thread that's doing something other than accepting connections. ... the async i/o methods for the Socket class instead. ...
    (microsoft.public.dotnet.languages.csharp)