aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes TCP/Socket mechanics of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "TCP/Socket mechanics of "Accept()" etc." Watch "TCP/Socket mechanics of "Accept()" etc." New topic

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

Tony Ruiter

Joined: Jul 03, 2005
Posts: 3

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 :

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.

3) At this point the client establishes some TCP connection using the following parameters :
Dest. IP : serverIP
Dest. Port : clientDestinationPort == serverlisteningPort
Source IP : {whatever the IP address of the client is}
Source Port: {If you used (2a) Majic!!!, or something wisely selected (2b)}

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,
osman cinar eren
Ranch Hand

Joined: Jan 25, 2005
Posts: 78
Source Port: {is this still "serverlisteningPort" ???}--> YES!

that is as you write for all of your questions.

Tony Ruiter

Joined: Jul 03, 2005
Posts: 3
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 :
osman cinar eren
Ranch Hand

Joined: Jan 25, 2005
Posts: 78
yes your guesses were right
also you should have a look at the following thread:
Tony Ruiter

Joined: Jul 03, 2005
Posts: 3
Good on ya mate!

I agree. Here's the link:
subject: TCP/Socket mechanics of "Accept()" etc.
Similar Threads
To know termenology
Socket on server side
Question about ServerSocket port number and Socket port number
server socket and accept call
Problem with simple chat app