Port vs Socket
Port
Port is a part of the transport layer and helps in network communication. A port is a logical identifier assigned to a process in order to identify that process uniquely in a network system. When two network devices communicate, they do so by sending packets to each other. Each packet received by a receiver device contains a port number that uniquely identifies the process where the packet needs to be sent.
Not all the network protocol uses a port for communication. For example, ICMP doesn’t use a port. On the other hand, protocols like TCP, UDP, HTTP utilize a port for communication.
Socket
Two processes that are running on a computer or running on two different systems can communicate via a socket. A socket works as an inter-process communicator and seen as the endpoint of the process communication. A socket consists of the IP address of a system and the port number of a program within the system. The IP address corresponds to the system and the port number corresponds to the program where the data needs to be sent:
Sockets can be classified into three categories: stream, datagram, and raw socket.
Stream sockets use connection-oriented network point to send and receive data. This type of sockets generally utilizes TCP to permit processes to communicate with each other.
Datagram sockets use connectionless network protocols like UDP to allow process communication.
Raw sockets are datagram oriented and allow the processes to use ICMP for communication purpose.
Port vs Socket : The Difference Between a Port and a Socket | Baeldung on Computer Science
Port | Socket |
---|---|
specifies a number that is used by a program in a computer | combination of IP address and port number |
program running on different computers can use the same port number, port number cannot be used to identify a computer uniquely | identifies a computer as a program within the computer uniquely |
used in transport layer | involved in application layer, interface between transport and application layers |
uses a socket to drop the data to a correct application | server & client use socket to keep an eye on data request and responses |