J
john
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, 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
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, 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