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

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

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

    Question with regard to Sockets

    Atirya Yodha, Mar 7, 2004, in forum: Java
    Replies:
    1
    Views:
    454
    Thomas Jollans
    Mar 7, 2004
  2. Stefan Willmert

    Applets, Security, Sockets General Question

    Stefan Willmert, Jun 5, 2004, in forum: Java
    Replies:
    3
    Views:
    531
    Stefan Willmert
    Jun 5, 2004
  3. Ike

    Sockets Question

    Ike, Sep 10, 2004, in forum: Java
    Replies:
    3
    Views:
    474
    Chris Smith
    Sep 10, 2004
  4. dp
    Replies:
    0
    Views:
    508
  5. Johannes Eble
    Replies:
    1
    Views:
    1,319
    Grant Edwards
    Jul 16, 2003
Loading...

Share This Page