Win a copy of Rust Web Development this week in the Other Languages forum!

Silvester Du

Greenhorn
+ Follow
since Dec 14, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Silvester Du

Hi Armen,

Can you please send me the SCJP 5.0 material to me also.My email is nakata_du@yahoo.com.cn

Thanks in Advance.
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?
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 ]