Restrict ports of ServerSocket.accept() sockets

Discussion in 'Java' started by Thomas Kuhn, Jul 4, 2003.

  1. Thomas Kuhn

    Thomas Kuhn Guest

    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

    --
    **

    **
    Thomas Kuhn, Jul 4, 2003
    #1
    1. Advertising

  2. On Fri, 4 Jul 2003 16:44:42 +0200, Thomas Kuhn wrote:
    > 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

    --
    [ do not send me private copies of your followups ]
    g o r d o n . b e a t o n @ e r i c s s o n . c o m
    Gordon Beaton, Jul 4, 2003
    #2
    1. Advertising

  3. On Fri, 4 Jul 2003 17:50:42 +0200, Thomas Kuhn wrote:
    >> 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.

    >
    > 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

    --
    [ do not send me private copies of your followups ]
    g o r d o n . b e a t o n @ e r i c s s o n . c o m
    Gordon Beaton, Jul 4, 2003
    #3
  4. Thomas Kuhn

    Sudsy Guest

    Thomas Kuhn wrote:
    > Hi Gordon,
    >
    >
    >>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.

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

    >
    > port = 35009
    > client: connected.
    >
    >>java Client

    >
    > port = 35010
    > client: connected.
    >
    >>java Client

    >
    > 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.
    Sudsy, Jul 4, 2003
    #4
  5. Thomas Kuhn

    Thomas Kuhn Guest

    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.
    Thomas Kuhn, Jul 7, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Liße
    Replies:
    15
    Views:
    15,290
    Tony Morris
    Feb 19, 2004
  2. oziris
    Replies:
    5
    Views:
    2,712
  3. Replies:
    1
    Views:
    820
    Gordon Beaton
    Mar 26, 2007
  4. Paul J. Lucas

    Hanging on ServerSocket.accept()

    Paul J. Lucas, Jul 10, 2008, in forum: Java
    Replies:
    5
    Views:
    1,987
  5. Jozza

    ServerSocket accept

    Jozza, Nov 6, 2008, in forum: Java
    Replies:
    1
    Views:
    384
    Knute Johnson
    Nov 6, 2008
Loading...

Share This Page