Restrict ports of ServerSocket.accept() sockets

T

Thomas Kuhn

Hi,
I try to get ServerSocket.accept() to spawn only Sockets within a specified
range of ports. Eg. I'd like to have only Sockets returned by accept() with
port between 1400 and 1600.

Thanks in advance,
Thomas
 
G

Gordon Beaton

I try to get ServerSocket.accept() to spawn only Sockets within a
specified range of ports. Eg. I'd like to have only Sockets returned
by accept() with port between 1400 and 1600.

Any Sockets returned by ServerSocket.accept() will use the same port
number as the ServerSocket itself. There is absolutely nothing you can
do about that.

If you are trying to accept only connections *from* specific remote
port numbers, then you need to accept(), check Socket.getPort() and
close() the unwanted ones. But why, really?

/gordon
 
G

Gordon Beaton

I don't think so: the ServerSocket.accept() method spawns sockets of
which each has another port. The following two classes illustrate
this:

I think so. Get a tool like tcpdump or netstat, and look at the
packets on your network or the connection reported by your OS kernel.
public class Server {
public Server() {
try {
ServerSocket serverSocket = new ServerSocket(1234);

while (true) {
Socket s = serverSocket.accept();
System.out.println("port = "+s.getPort());
}
} catch (IOException e) {
e.printStackTrace();
}
}

Every Socket connection has two port numbers, one at each end. The
above code reports the port number of the *remote* (client) end of the
new Socket, not the *local* (server) end. Try displaying
Socket.getLocalPort() here instead and you'll find that they all use
port 1234 at that end, which is what I said in my original reply.

Again, which port number do you want to restrict?

/gordon
 
S

Sudsy

Thomas said:
Hi Gordon,




I don't think so: the ServerSocket.accept() method spawns sockets of which
each has another port. The following two classes illustrate this:

port = 35009
client: connected.


port = 35010
client: connected.


port = 35011
client: connected.

From the javadocs (your friend, remember?):
getPort

public int getPort()

Returns the remote port to which this socket is connected.

Returns:
the remote port number to which this socket is connected, or 0 if the socket is not connected yet.
getLocalPort

public int getLocalPort()

Returns the local port to which this socket is bound.

Returns:
the local port number to which this socket is bound or -1 if the socket is not bound yet.


You're just printing the remote port number.
 
T

Thomas Kuhn

I am talking about the port on the local maching. I had a look at what you
said, you both were right. sorry for my wrong assertion!
You're just printing the remote port number.

Our application is using RMI - it seems that RMI creates sockets with a
different port that the one the RemoteObjects are bound to:
tcp4 0 0 aix01.47732 ws.1184 ESTABLISHED
(aix is the machine, where netstat was called)
the RemoteObject was bound to port 1277. does anybody know how I can get
control over what port number RMI is using for it's extra sockets? btw: what
are these sockets for?

Regards
Thomas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top