wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Question about ServerSocket port number and Socket port number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Question about ServerSocket port number and Socket port number " Watch "Question about ServerSocket port number and Socket port number " New topic
Author

Question about ServerSocket port number and Socket port number

Silvester Du
Greenhorn

Joined: Dec 14, 2006
Posts: 3
Hello, all

I'm a new learner in Java and I'm reading the Head First Java(2nd). I found one qustion about the ServerSocket in the chapter 15,page 483,"Writing a simple server" Step 3 in "How it works"

The code:
ServerSocket serverSock=new ServerSocket(4242);
Socket sock=serverSock.accept();

Head First Java said,"When a client finally tries to connect, the method returns a plain old Socket(on a different port) that knows how to communicate with the client. The Socket is on a different port than the ServerSocket, so that the ServerSocket can go back to waiting for other clients."

I found this is different with what I thought before. The Java Tutorials said "when a connection is requested and successfully established, the accept method returns a new Socket object which is bound to the same local port(4242 in this case) and has it's remote address and remote port set to that of the client. "
see here

I've tested and it also shows that the port number of the Sockte object which the method accept() returns is the SAME of the ServerSocket(4242 in this case).


I wonder whether there is something wrong with what Head First Java said here? Or, did I understood incorrectly?

Thanks so much!

[ February 20, 2007: Message edited by: Silvester Du ]

[ February 20, 2007: Message edited by: Silvester Du ]
[ February 20, 2007: Message edited by: Silvester Du ]

A greenhorn...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

The normal way how this works is how Head First Java describes it: (1) you create a server socket and make it listen on a specific port (4242 for example), (2) you call accept() to wait for a client to connect, (3) you get a socket back from accept() that is connected to a different port (assigned by the system at the moment the client makes the connection) and the server socket continues to listen on its port (4242) for new clients.

This is not specific to Java; this is how TCP/IP sockets work in general on most operating systems.

Note that The Java Tutorial says:

"When a connection is requested and successfully established, the accept method returns a new Socket object which is bound to the same local port and has it's remote address and remote port set to that of the client. The server can communicate with the client over this new Socket and continue to listen for client connection requests on the original ServerSocket."

Note that a distinction is made between the local and remote ports. This is probably where the confusion comes from. Note that class Socket has two methods: getLocalPort() and getPort(), which will tell you the local and remote ports that the socket is connected to.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Silvester Du
Greenhorn

Joined: Dec 14, 2006
Posts: 3
Jesper, thank you for your reply.

So when a new socket is created by the code below:
ServerSocket serverSock=new ServerSocket(4242);
Socket sock=serverSock.accept();

the local port of this socket(sock) is the same as the local port of ServerSocket(4242 in this case).
and the remote port of this socket is the same as the local port of the client socket who requests a connection.

Am I right?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

Originally posted by Jesper Young:
The normal way how this works is how Head First Java describes it: (1) you create a server socket and make it listen on a specific port (4242 for example), (2) you call accept() to wait for a client to connect, (3) you get a socket back from accept() that is connected to a different port (assigned by the system at the moment the client makes the connection) and the server socket continues to listen on its port (4242) for new clients.


That's the way I understand TCP/IP, but take a look at the diagrams in the tutorial. It sure looks like the tutorial author is confused.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
vu lee
Ranch Hand

Joined: Apr 19, 2005
Posts: 189
(3) you get a socket back from accept() that is connected to a different port (assigned by the system at the moment the client makes the connection) and the server socket continues to listen on its port (4242) for new clients.


We know the client's IP address and port number remain the same. The socket back from accept() has different port number (not 4242). The server uses the new socket to send the response back to the client. If that is the case, then the clientSocket.getPort() should not be 4242, but in the console I see it is 4242 which mean from the client point of view, the client still talks to the same port.

If the same server port number is used to accept request and send response to multiple clients, would that degrade the overall throughput?
[ March 05, 2007: Message edited by: vu lee ]
vu lee
Ranch Hand

Joined: Apr 19, 2005
Posts: 189
Any comments on this guys. On the server side, could response be simutaneously sent back to multiple clients from a single port on a mutiprocessors app server? Does the web server using default port 80 send responses back to the web browsers simutaneously? I think it does, but I don't know how it works.
[ March 08, 2007: Message edited by: vu lee ]
Yong Bakos
Greenhorn

Joined: Jul 18, 2005
Posts: 29
I too would love to hear a response to this question.
Yong Bakos
Greenhorn

Joined: Jul 18, 2005
Posts: 29
I've got the answer. Note the two methods Socket#getPort and Socket#getLocalPort.
Below, see the chat client and server from pages 481 and 484 (HF Java 2nd Ed, chapter 15) where I've added some printing of port numbers.

Run the server, and run the client. If you run the client multiple times, you'll see the "local socket port" change, as it should per TCP's behavior.

But given the output, it seems like the socket is connected FROM 57xxx on the client TO port 4242 on the server, which still doesn't coincide with the explanation in the book (and other online examples) where the server seems to use a different port.

Anyone care to enlighten me?



Yong Bakos
Greenhorn

Joined: Jul 18, 2005
Posts: 29
Here's the big issue: the illustration on page 483 where the server "switches" ports is incorrect. See http://stackoverflow.com/questions/4307549/serversocket-accept-method?rq=1
 
GeeCON Prague 2014
 
subject: Question about ServerSocket port number and Socket port number