On a TCP/IP network every device must have an IP address.
The IP address identifies the device e.g. computer.
However an IP address alone is not sufficient for running network applications, as a computer can run multiple applications and/or services.
Just as the IP address identifies the computer, The network port identifies the application or service running on the computer.
The use of ports allow computers/devices to run multiple services/applications.
The diagram below shows a computer to computer connection and identifies the IP addresses and ports.
Analogy
If you use a house or apartment block analogy the IP address corresponds to the street address.
All of the apartments share the same street address.
However each apartment also has an apartment number which corresponds to the Port number.
Port Number Ranges and Well Known Ports
A port number uses 16 bits and so can therefore have a value from 0 to 65535 decimal
Port numbers are divided into ranges as follows:
Port numbers 0-1023 – Well known ports. These are allocated to server services by the Internet Assigned Numbers Authority (IANA). e.g Web servers normally use port 80 and SMTP servers use port 25 (see diagram above).
Ports 1024-49151- Registered Port -These can be registered for services with the IANA and should be treated as semi-reserved. User written programs should not use these ports.
Ports 49152-65535– These are used by client programs and you are free to use these in client programs. When a Web browser connects to a web server the browser will allocate itself a port in this range. Also known as ephemeral ports.
TCP Sockets
A connection between two computers uses a socket.
A socket is the combination of IP address plus port
{outline]Each end of the connection will have a socket.{/outline]
Imagine sitting on your PC at home, and you have two browser windows open.
One looking at the Google website, and the other at the Yahoo website.
The connection to Google would be:
Your PC – IP1+port 60200 ——– Google IP2 +port 80 (standard port)
The combination IP1+60200 = the socket on the client computer and IP2 + port 80 = destination socket on the Google server.
The connection to Yahoo would be:
your PC – IP1+port 60401 ——–Yahoo IP3 +port 80 (standard port)
The combination IP1+60401 = the socket on the client computer and IP3 + port 80 = destination socket on the Yahoo server.
TCP and UDP -The Transport Layer
Note: You may find reading the article on the TCP/IP protocol suite useful to understand the following
IP addresses are implemented at the networking layer which is the IP layer.
Ports are implemented at the transport layer as part of the TCP or UDP header as shown in the schematic below:
The TCP/IP protocol supports two types of port- TCP Port and UDP Port.
TCP – is for connection orientated applications. It has built in error checking and will re transmit missing packets.
UDP – is for connection less applications. It has no has built in error checking and will not re transmit missing packets.
Applications are designed to use either the UDP or TCP transport layer protocol depending on the type of connection they require.
For example a web server normally uses TCP port 80.
It can use any port, but the web server application is designed to use a TCP connection. See TCP vs UDP
Here is a very good video that explains ports and sockets really well
Checking For Open Ports
Windows and Linux systems have a utility called netstat which will give you a list of open ports on your computer.
These articles show you how to use netstat on windows and on linux.
You can check the port status of remote machines using a port scanner line nmap.
You can install NMAP on windows,Linux and Apple. It can be used with a graphical user interface of as a command line tool.
Here is a useful article on using NMAP from the command line.
Here is a good video on using Nmap and also covers TCP/IP connection procedures which is useful for understanding ports.
References and resources:
TCP and UDP basics -Connecting to a website- This is for programmers but there is no coding just an explanation of ports and sockets.
Connection states– if you are wondering what established and listening and the other state descriptions mean. here is a good state diagram that it refers to.
Online port tester Collection of tools for port scanning and web server testing.
Related Articles:
Very well explained, thanks a lot!
Steve, this was very well explained, thank you.
Thank you very much! Realy clear explanation.
I want to read a data from a machine which supports open protocol if the machine is in network and if I know IP address and port number(socket) how can I read a data from it(I know it send and receive data in packet format )
Hi not sure what you mean by supports open protocol.
Rgds
Steve
Thank you, Steve!
Is it possible with TCP/IP sockets to send requests to one IP:PORT and listen for responses on a different IP:PORT. E.g. we want to send to a load balancer IP but listen for response on localhost.
Hi
Not quite sure of what you mean exactly but load balancing is quite common.
This might help
https://www.citrix.com/glossary/load-balancing.html
Question: When a program on your computer sends or receives data over the Internet it sends that data to an ip address and a specific port on the remote computer. How does my computer know what port a specific application is working on another computer to populate the TCP Header?
When Machine A connects to machine B. The source ip and port are contained in the connection packet as well as the destination port and IP address.
The IP packet contains the ip addresses and the tcp layer contains the port numbers
see
https://en.wikipedia.org/wiki/IPv4
https://en.wikipedia.org/wiki/Transmission_Control_Protocol
Hi Steve,
Thanks for the tutorial can you please help me with the following issue
I have multiple devices connected to the same network and I want to transfer data across them, how can I transmit the data the IP of some devices changes after a particular time I can keep the port number constant but the devices don’t know each others IP address and also I don’t want to use UDP because it is not reliable
Hi
You need to use DNS.Because the IP changes you need dynamic DNS. Most devices have MDNS enabled.
See this article on my other site
https://stevessmarthomeguide.com/name-resolution-and-dns-on-home-networks/
To seen if mdns is working try ping computer_name.local
However the easiest solution is to give them static IP addresses.
Rgds
Steve
Hi,
Great site. I have a quick question. If I receive a UDP datagram using recvmsg(), will the msghdr structure be filled out with the sender’s IP and port (in the msg_name field, which is a sockaddr struct)?
Many thanks.
Hi
Haven’t programmed c with UDP but according to IBM then yes for ip address but doesn’t mention the port.
https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.hala001/rcvm.htm
rgds
steve
hi ,I have some questions
When using TCP protocol, the client must use in its message the IP address and Port number of the server. And it must also include its own IP address and port number. why?
4- When using UDP protocol, the client is required to use in its message the IP address and Port number of the server. But it is not required to include its own IP address and port number.why?
TCP is a connection orientated protocol and so the destination device needs to know who to send a reply to.
UDP is connectionless and so doesn’t require a response and hence the source IP address is optional.
Rgds
Steve
really very useful..thanks for your explanation
Hi Steve,
Can Server and client share the port number, eg : Server is listening at port num : 51001 and client also listens on the same port num : 510001 .
Can you pls explain this
Yes providing they are on different hardware.
A port is simply a door into the machine the numbers themselves aren’t really important except for the conventions already adopted and standardised. Machines use 16bits for the port numbers hence the limitation of 64,000.
16 bits = 1+2+4+8+16+32+64+128+256+512+1024+2048+4096+8192+16384+32768 = 65535
Good job Steve. I have a question, and it goes like this: if i have 2 browsers from my PC connecting to the same service, for example both browsers are connecting to google.com, does that mean that the two browsers from my computer are connecting to the same socket in the google server? Meaning can more than one socket connect to a socket at the same time?
Yes they use the same IP and port on the Google server but different ports on the client.
rgds
steve
Hi, Thanks for this information sharing. It is very well explained.
I have following doubt,
What I understand is TCP protocol takes care of data transmission error that duplication of packets or packet in true form delivered to other end. This is not done in UDP protocol. But when I see the Schematic of TCP, UDP & IP in your article I do not find any block for error checking in TCP where as UDP has checksum. Could you please clear my doubt.
The TCP packet also has a checksum which isn’t shown in my schematic you can see the full structure on wiki
https://en.wikipedia.org/wiki/Transmission_Control_Protocol
However it is the ack and nack messages that are responsible or making sure all of the data is received.
Rgds
Steve
In above example, google application/service and yahoo application/service both uses same port number i.e:80.
Then there should be ambiguity that is to which application/service it should refer to either google or yahoo ;How it knows that it is google or yahoo as both has same port number?
Answer will help me alot.
Google and Yaoo have different IP addresses. When you connect to google you use Google_IP +port 80.
When you connect to Yahoo you connect to Yahoo_IP + port 80.
Does that make sense?
rgds
steve
The IP is the who and the Port is part of the how.
The IP address of google and yahoo is different.
say for google 172.13.73.130
and for yahoo 172.23.73.128
so even accessing the same port the two sockets will be totally different and hence different connections.
using the real world analogy two rooms of two different apartments may have the same room number and hence two server system may have same port number as well.
The port number 80 is the open port of server.here Google server had an open port (80) also Yahoo had an open port (80).
Both Google and Yahoo had different IP addresses.
If I want to connect Google server,I will open a new port,for example I will open port 5000, at the same time I will open another port 5002 to connect with Yahoo server.
Example :
My IP + 5000 — connects — Google IP + 80
My IP + 5002 — connects — Yahoo IP + 80
Great work!
Good Job!! Well Explained
Hi! keep up the good work. I have few doubts though, hope you can clear them:
1. Can you tell if a server uses a single port 80 on all of its connections to clients? Or is it used only by the Welcoming socket?
Can port 80 handle multiple connections simultaneously?
A port (e.g port 80) can handle multiple connections each connection is a socket and will have a different source IP address and port number.
Excellent explanation. Thankyou!
very good information
The way you explain ed it, now it’s very easy understand the whole thing thanks alot sir .
Complete details about the TCP / IP ports and the sockets. in this article .The infographic is so good that anybody can understand at a glance. I request the author to post more articles on networking.
Could someone explain what is the socket buffer size. What’s the maximum size we can set? Is socket buffer and MTU same ?
MTU is the largest packet that you can send on the network and is determined by the underlying network and protocols – https://en.wikipedia.org/wiki/Maximum_transmission_unit
Buffer size relates to the software buffers allocated for sending and receiving data from the network.
As far as I understand the two are not directly related.
See this unix network programming book (MSS is equivalent to MTU as far as I understand)
https://books.google.co.uk/books?id=ptSC4LpwGA0C&pg=PA208&lpg=PA208&dq=socket+buffer+size&source=bl&ots=Kt1FTmgoTn&sig=3bumDm1zhIYx1GgPg6hNvSb5jrQ&hl=en&sa=X&ved=0ahUKEwiu9JWo66zZAhUsJ8AKHd0BDYEQ6AEIUzAE#v=onepage&q=socket%20buffer%20size&f=false
Can you help answer this question?
https://stackoverflow.com/questions/46490444/network-socket-use-not-reflected-in-netstat
Sorry but I’ve looked at the documentation and it doesn’t explain what total is.
I did come across this tutorial about ss command that may be of interest.
http://www.binarytides.com/linux-ss-command/
Let me know if you have any luck
One basic thing I have been trying to determine is what is a socket. Eg is a stream socket a means to access tcp or something built on top of tcp? Reading many explanations of sockets this is not clear. Ie, can you read any tcp stream using a socket, or do both ends have to use sockets for it to work?
All data communication over tcp/ip and udp/ip uses sockets. Streaming refers to how the data is transmitted i.e. as contiguous bytes see socket stream om WIKI https://en.wikipedia.org/wiki/Stream_socket.
A socket is effectively an application ID as it is the combination of port and IP address that the application is using.
Both end of the connection will use a socket regardless of whether they are streaming data or sending datagrams (UDP).
See also http://www.tcpipguide.com/free/t_TCPIPSocketsandSocketPairsProcessandConnectionIden.htm
Hello Steve
In this article, u said port numbers from 1024 to 49151 are not to be used by programs.
I don’t know if the port I can enter in a browser is client port or server port but I see :8080 in browser when connecting to my local XAMPP server. Why 8080 and why not just 80
It is because port 80 is reserved for web servers whereas Xampp is an application and so it uses the alternative http port. Ports under 1024 require super user permissions on Linux and so they are usually avoided.
This is what the wiki says about these ports
The range of port numbers from 1024 to 49151 are the registered ports. They are assigned by IANA for specific service upon application by a requesting entity. On most systems, registered ports can be used without superuser privileges.
There is a list of ports and usage here
https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
You might notice that there are lots of ports used but not officially. In this case a developer has chosen a free port and used it but not registered it.