OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 6:49 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: How does TCP know the server socket's port?
PostPosted: Thu Aug 13, 2015 7:13 am 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
My OS has a developing TCP/IP stack, but currently only support raw sockets (SOCK_RAW). I am now trying to add support for TCP. I understand the idea of the three-way handshake: client sends SYN, server responds with SYN+ACK, and the client responds with ACK.

I assume the first SYN (sent by the client) must be directed at the server's listening port (e.g. port 80 for HTTP), and the server responds with its SYN+ACK from port 80? In this case, how does the client know the port number allocated to the communicating socket (the socket returned to the server by accept() function)?


Top
 Profile  
 
 Post subject: Re: How does TCP know the server socket's port?
PostPosted: Thu Aug 13, 2015 7:40 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
A socket consists of a tuple of numbers:

source ip address; source port; destination ip address; destination port.

In the scenario that you mention the destination port of the socket returned by accept() is port 80. The source IP address and port will be those determined by the client when it initiated the connection. These four numbers form a unique identifier.


Top
 Profile  
 
 Post subject: Re: How does TCP know the server socket's port?
PostPosted: Thu Aug 13, 2015 10:48 am 
Offline
Member
Member
User avatar

Joined: Sun Sep 19, 2010 10:05 pm
Posts: 1074
The initial connection packet contains the source IP address and port number in the TCP header. The server simply uses those values as the destination IP address and port of the response packet.

In your scenario, the client uses a random port number for the entire connection, and the server always uses port 80 for all connections.

_________________
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott


Top
 Profile  
 
 Post subject: Re: How does TCP know the server socket's port?
PostPosted: Thu Aug 13, 2015 2:25 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
I think my question may have been misunderstood. If 3 different clients connect to port 80, the server uses 3 different sockets to talk to the 3 different clients - each socket must be bound to a different port. How does the server tell the clients which port is to be used for communication, as opposed to the listening socket?

Alternative wording: when I call accept(), I get back a file descriptor for a socket. When I look at the address that the socket is bound to, sin_port is not 80 (otherwise the server could only handle one connection at a time). It is some different port number that was allocated for this particular connection. How does the client, which calls connect(), know which port the server has chose for that communicating socket?


Top
 Profile  
 
 Post subject: Re: How does TCP know the server socket's port?
PostPosted: Thu Aug 13, 2015 2:57 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
mariuszp wrote:
When I look at the address that the socket is bound to, sin_port is not 80 (otherwise the server could only handle one connection at a time). It is some different port number that was allocated for this particular connection. How does the client, which calls connect(), know which port the server has chose for that communicating socket?

That is incorrect. The client continues to communicate on port 80. What makes you think that only one connection to port 80 can be handled at a time?

It's like post boxes in a post office. The IP address corresponds to the address of the post office, the port number corresponds to the box number. But more than one person can send letters to that box, and get replies from it. Their letters contain the address they were sent from so the recipient can separate the messages. He knows to send replies to letters from the bank to the bank and letters from the electricity company to the electricity company. TCP/IP does the same.


Top
 Profile  
 
 Post subject: Re: How does TCP know the server socket's port?
PostPosted: Thu Aug 13, 2015 3:17 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
iansjack wrote:
mariuszp wrote:
When I look at the address that the socket is bound to, sin_port is not 80 (otherwise the server could only handle one connection at a time). It is some different port number that was allocated for this particular connection. How does the client, which calls connect(), know which port the server has chose for that communicating socket?

That is incorrect. The client continues to communicate on port 80. What makes you think that only one connection to port 80 can be handled at a time?

It's like post boxes in a post office. The IP address corresponds to the address of the post office, the port number corresponds to the box number. But more than one person can send letters to that box, and get replies from it. Their letters contain the address they were sent from so the recipient can separate the messages. He knows to send replies to letters from the bank to the bank and letters from the electricity company to the electricity company. TCP/IP does the same.


Oh I see. Guess I had a misconception.


Top
 Profile  
 
 Post subject: Re: How does TCP know the server socket's port?
PostPosted: Thu Aug 13, 2015 3:40 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
So if accept() is called twice and 2 different connections are opened, each of the 2 sockets is bounds to port 80 but just has a different peer address?


Top
 Profile  
 
 Post subject: Re: How does TCP know the server socket's port?
PostPosted: Thu Aug 13, 2015 3:50 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
The two sockets will both be bound to port 80 and the IP address of the server; but they will have different values for the IP address and port of the client (actually, it is conceivable that they have the same client IP address with different port numbers or different client IP addresses with the same port number, but the combination of the 4 items will be unique for each socket).


Top
 Profile  
 
 Post subject: Re: How does TCP know the server socket's port?
PostPosted: Thu Aug 13, 2015 3:53 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
iansjack wrote:
The two sockets will both be bound to port 80 and the IP address of the server; but they will have different values for the IP address and port of the client (actually, it is conceivable that they have the same client IP address with different port numbers or different client IP addresses with the same port number, but the combination of the 4 items will be unique for each socket).


OK, I get it now. I'll start implementing TCP but I'll probably go for UDP first as it's easier to implement.


Top
 Profile  
 
 Post subject: Re: How does TCP know the server socket's port?
PostPosted: Thu Aug 13, 2015 3:56 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
mariuszp wrote:
I'll probably go for UDP first as it's easier to implement.
Ain't that the truth!

I'm actually in the middle of trying to implement a TCP/IP stack too, but it is very complicated with all the error checking and retransmittal of lost packets.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 42 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group