• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question about ServerSocket port number and Socket port number

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Silvester Du
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(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
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too would love to hear a response to this question.
 
Yong Bakos
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic