a simple question about sockets

Discussion in 'Java' started by Jerem38, May 5, 2004.

  1. Jerem38

    Jerem38 Guest

    The accept() function of the class ServerSocket create a new Socket and
    return it.

    public class ServeurEcouteur {
    public static int port = 15555;

    public static void main(String[] args) {
    try {
    ServerSocket ecoute = new ServerSocket(port,5);
    while(true) {
    Socket service;

    service = ecoute.accept();
    System.out.println(service.getLocalPort());
    System.out.println(ecoute.getLocalPort());
    new Thread (new ServeurConnection(service)).start();
    }// end while

    }//end try
    catch (Exception e) {
    System.out.println("erreur cote serveur ecouteur"+e);
    }
    }

    I don't understand why the 2 println return the same value. Are the 2
    sockets (the newone created and the ServerSocket) connected to the same
    port ? Is it possible ?
     
    Jerem38, May 5, 2004
    #1
    1. Advertisements

  2. Jerem38 wrote:

    > The accept() function of the class ServerSocket create a new Socket and
    > return it.

    <snip code>

    Please don't multipost, it is quite annoying.

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, May 5, 2004
    #2
    1. Advertisements

  3. On Wed, 05 May 2004 15:23:11 GMT, Jerem38 wrote:
    > The accept() function of the class ServerSocket create a new Socket and
    > return it.

    [...]
    > I don't understand why the 2 println return the same value. Are the
    > 2 sockets (the newone created and the ServerSocket) connected to the
    > same port ? Is it possible ?


    Don't multipost.

    The behaviour is perfectly normal. Consider the perspective of the
    connecting client. When he created his Socket, he asked to be
    connected to a specific port on the server. Should the (now connected)
    Socket discover that it is in fact connected to a *different* remote
    port than the requested one?

    In fact all Sockets created from the ServerSocket will share the same
    port number, despite what the java networking tutorial says.

    What you need to understand is that the port number is only *part* of
    the socket identifier. Each end of the socket connection is identified
    by an IP address and a port number, and the connection itself is two
    such endpoints. As long as the two connections differ in at least one
    of the four fields, the connections are unique.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, May 5, 2004
    #3
  4. Jerem38

    Jerem38 Guest

    Gordon Beaton <> wrote in news:40990f03$:

    > On Wed, 05 May 2004 15:23:11 GMT, Jerem38 wrote:
    >> The accept() function of the class ServerSocket create a new Socket
    >> and return it.

    > [...]
    >> I don't understand why the 2 println return the same value. Are the
    >> 2 sockets (the newone created and the ServerSocket) connected to the
    >> same port ? Is it possible ?

    >
    > Don't multipost.
    >
    > The behaviour is perfectly normal. Consider the perspective of the
    > connecting client. When he created his Socket, he asked to be
    > connected to a specific port on the server. Should the (now connected)
    > Socket discover that it is in fact connected to a *different* remote
    > port than the requested one?
    >
    > In fact all Sockets created from the ServerSocket will share the same
    > port number, despite what the java networking tutorial says.
    >
    > What you need to understand is that the port number is only *part* of
    > the socket identifier. Each end of the socket connection is identified
    > by an IP address and a port number, and the connection itself is two
    > such endpoints. As long as the two connections differ in at least one
    > of the four fields, the connections are unique.
    >
    > /gordon
    >


    Excuse me for multipost

    thanks for your explanation, it is now clear :)
     
    Jerem38, May 5, 2004
    #4
  5. > I don't understand why the 2 println return the same value. Are the 2
    > sockets (the newone created and the ServerSocket) connected to the same
    > port ?


    Yes.

    > Is it possible ?


    Yes.

    :)

    Have a nice day,
    Michiel
     
    Michiel Konstapel, May 7, 2004
    #5
    1. Advertisements

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. Atirya Yodha

    Question with regard to Sockets

    Atirya Yodha, Mar 7, 2004, in forum: Java
    Replies:
    1
    Views:
    569
    Thomas Jollans
    Mar 7, 2004
  2. dp
    Replies:
    0
    Views:
    578
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    1,118
    Daniel Frey
    Jan 12, 2005
  4. Johannes Eble
    Replies:
    1
    Views:
    1,624
    Grant Edwards
    Jul 16, 2003
  5. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    618
    Roland Hall
    Jan 26, 2004
  6. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    480
    Peter Bailey
    Apr 8, 2008
  7. Kirk

    Simple Question = simple answer?

    Kirk, Sep 23, 2003, in forum: Javascript
    Replies:
    1
    Views:
    277
    Martin Honnen
    Sep 23, 2003
  8. db
    Replies:
    7
    Views:
    439
Loading...