a simple question about sockets

J

Jerem38

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

Christophe Vanfleteren

Jerem38 said:
The accept() function of the class ServerSocket create a new Socket and
return it.
<snip code>

Please don't multipost, it is quite annoying.
 
G

Gordon Beaton

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
 
J

Jerem38

Gordon Beaton said:
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 :)
 
M

Michiel Konstapel

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
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top