I know that a user is assigned a routable IP to access internet so I was wondering if user is also assigned with TCP/UDP port too? I mean just like there is a specific IP of the destination server with whom we are connecting to and there is also a TCP/UDP port on that server on which we are connected. On client side we have external IP of the client. Does that client also have a port number? How to check it?
Many ISP's assign you an IP Address for Some Period. Most of the time they have one or more DHCP servers, so that every time you connect your modem or corresponding networking device you get a different IP and if they are not using NAT then most probably you will get a Temporary Public IP Address.
OK, now you have an working IP address and you can start communicating with some Internet Services whether it is a Network Layer Service, Transport Layer Service or Application Layer Service.
For Network layer Service you do not need to open a port on your computer. For example when you are pinging a machine from your computer by using ping command then you are actually using the ICMP protocol and more specifically you are using the ICMP Echo Request and In response you will get an ICMP echo Response. The Network layer can be also used for some other purposes for example IGMP or other Layer 3 protocols. The most interesting fact about this is that you do not need to have any Port open to work with this protocol. You need only an IP address for this.
But in terms of Transport Layer Service whether it is TCP or UDP you need to open a port to communicate with the service. And in terms of Application layer service that port will work with the socket API that will work with the particular Application. Most of the time it will open a higher value port (>1000) that is called an Ephemeral Port aka short lived port because the port will be closed as long as the transport layer is done.
Note that the Ephemeral port number varies from machine to machine.
Now the best Part is that ISP does not restrict you using any port number so you can use any short lived port with you connection.
Think of ports as doors. Your PC has a lot of doors and the server also.
A web server has the door 80 open, so if a PC acting as a client wants to connect to the web server, then:
1- The PC opens a random port (door), for example 50000.
2 - From that port the client PC sends a package of data to web server IP address port (door) 80.
3 - If the package is a correct request, then the web server replies from port 80 to client IP address port 50000.
4 - If the reply is correct and the communication ends, the client PC closes its port (door) 50000.
The client starts the communication process to the server, and therefore he chooses a random port in the range known as ephemeral ports (
49152-65535) to connect to the server. The port stays open while there is a data transmission with the server. Whenever the transmission ends, the port is closed.
Internet connections require the use of a public IP address. In IPv4 there are 4294967296 (2^32) addresses. Those addresses have been exhausted years ago. So ISPs use
NAT (Network Address Translation) to reduce the number of IP Public addresses on each connection.
In a typical home connection to internet, the devices in the home network use addresses in the range 192.168.X.X because that range is considered private and can be used on every home or office network.
The router or modem uses a single public IP address to connect to the external world, so the device has to translate the internal private address to the external one and the ports opened/used inside and outside to avoid confusions.
NAT maps the pair internal address:port to a external address:different port and that mapping let it process the replies correctly, avoiding the confusion if two clients coincidentally open the same source port.
The modem and the NAT process are designed to do this in an automatic way as long as the communication starts from inside the network to external servers.
If you decide to put, for example, a web server inside your home network (therefore, on a private IP address) and you want that the server can be accessed from the external world then you have to manually configure NAT to let it know that incoming connections to port 80 (web server port) must get through the device and go to the internal IP private address of the web server.
yes.. both sides use port number but don't belong portnumber to users and clients... it's related to apps you work..
TCP/UDP numbers are actually identifiers for finding responsible app (multiplexing using ports)
whenever you open an app and want connection and communication with another side that app in client-side (notice: your client can be server-side for some services! i.g. when you share a folder in your win.. you actually open these ports TCP 139, 445 UDP 137, 138.. waiting for establishing a connection..) randomly assign a number to its port and attach it to your IP (Socket) afterwards that socket talks with server-side which should have well-known port number (telnet 23 , SSH 22 ... )
(The dynamic or private ports are those from 49152 through 65535)
try "netstat -a -n" command and see your current sessions it will help you more to figure out
Multiple Connection Management
This identification of connections using both client and server sockets is what provides the flexibility in allowing multiple connections between devices that we take for granted on the Internet. For example, busy application server processes (such as Web servers) must be able to handle connections from more than one client, or the World Wide Web would be pretty much unusable. Since the connection is identified using the client's socket as well as the server's, this is no problem. At the same time that the Web server maintains the connection mentioned just above, it can easily have another connection to say, port 2,199 at IP address 22.214.171.124. This is represented by the connection identifier:
(126.96.36.199:80, 188.8.131.52:2199). In fact, we can have multiple connections from the same client to the same server. Each client process will be assigned a different ephemeral port number, so even if they all try to access the same server process (such as the Web server process at 184.108.40.206:80), they will all have a different client socket and represent unique connections. This is what lets you make several simultaneous requests to the same Web site from your computer.
Again, TCP keeps track of each of these connections independently, so each connection is unaware of the others. TCP can handle hundreds or even thousands of simultaneous connections. The only limit is the capacity of the computer running TCP, and the bandwidth of the physical connections to it—the more connections running at once, the more each one has to share limited resources.