TCP/Socket mechanics of "Accept()" etc.

Tony Ruiter

Greenhorn

Posts: 3

posted 11 years ago

Hi,

I wouldn't mind some clarification on the issue of listening sockets and the "serverSocket.Accept()" functionality for creating socket connections. I have not yet been completely satisfied with the explanations in the posts here. In particular where we are talking about source and destination TCP ports at each end of the connection.

I've had a pretty good look for this information on these posts, and elsewhere and wondered if somebody might offer their wisdom!

Here is my understanding of the socket connection process from a server/client point of view :

NOTES : I am glazing over the strict definition/implementation of "host name" and "IP address" and just considering everything an IP address.

I am assuming any port numbers we pick are not held/bound/used by any other applications etc.

1) On the server... We create a "listening socket" which is bound to a "listening port", which listens for incoming connections from clients :serverListeningSocket = new ServerSocket(serverListeningPort);

We then wait around for an incoming connection by calling the blocking method "accept()" :Socket serverConnectionSocket = serverListeningSocket.accept();

2a) On the client... We create a socket, which will establish a connection to the server, by using a constructor with the server's IP and the "Destination Port".clientConnectionSocket = new Socket(serverIP, clientDestinationPort);

The "clientDestinationPort" must of course be the same as the "serverListeningPort".

2b) I understand you can also use another constructor on the client (substititute for step (2a)) :clientConnectionSocket = new Socket(serverIP, clientDestinationPort, clientIP, clientSourcePort);

where "clientIP" is the IP of the client machine, and the client application can now "pick" some available port to be used as the source port, rather than relying on a random selection.

4) At the server end, we'll assume the "accept()" succeeded etc, and now we have a new ordinary "socket" called "serverConnectionSocket", Which we use to do the talking and all the other good stuff.

Now here's where I am not clear :

What TCP parameters are being used for the connection (the new "serverConnectionSocket") from the SERVER'S END now? How's my guessing.... Dest. IP : clientIP {pretty intuitive} Dest. Port : {this should be the client's source port right? A random one (2a) or a "picked" one (2b)} Source IP : serverIP {pretty intuitive} Source Port: {is this still "serverlisteningPort" ???}

And how about the details of the connection at the client end? Are they still the same as in (3) ?

Thanks in advance guys, Tony.

osman cinar eren

Ranch Hand

Posts: 78

posted 11 years ago

Source Port: {is this still "serverlisteningPort" ???}--> YES!

that is as you write for all of your questions.

SCJP/SCWCD

Tony Ruiter

Greenhorn

Posts: 3

posted 11 years ago

ok so

Source Port: {is this still "serverlisteningPort" ???}--> YES!

great, i'm getting somewhere!

but didn't quite understand what you meant by :

that is as you write for all of your questions.

I think you are telling me that I was right for all my other guesses!? In which case :