An simplified introduction to sockets
Sockets are a mechanism that allows programs to communicate, either on
the same machine or across a network. The way it works is pretty simple:
Each machine on a network is identified by some address, so I’ll talk
about tcp/ip networking, so by network address I mean an IP Address. (like 192.168.4.4) Apart from the IP address that specifies a machine,
each machine has a number of PORTS that allow handling multiple
connections simultaneously.
A program that wishes to receive a connection from another program, asks
the operating system to create a socket and bind it to some port number. Then the program sits and listens on the socket it has created to
receive incoming connections via the specified PORT number. The other
program also creates a socket for communicating with the receiver. The
caller needs to specify the IP address and the port number of the
receiving end. If all goes well, the two programs establish a
communication through the network using their sockets. The two programs
may exchange information, each by writing to and reading from the socket
it has created.
EXAMPLE: A web server (web site) will have open sockets binded to port "80" which is
designated for HTTP traffic, when you connect to a web address then that
address is resolved into a IP number, which your internet browser will
connect to by opening a socket and connecting to the specified address
on port 80 which the web server will receive that data and respond to
the request with the information requested.
An Introduction to PORTS
In computer networking, a port is an application-specific or process-specific software construct serving as a communications endpoint. It is used by Transport Layer protocols of the Internet Protocol Suite,
such as Transmission Control Protocol (TCP) and User Datagram Protocol
(UDP). A specific port is identified by its number, commonly known as
the port number, the IP address with which it is associated, and the
protocol used for communication.
Transport Layer protocols, such as the Transmission Control Protocol
(TCP), the User Datagram Protocol (UDP), specify a source and destination port number in their packet headers (see packets above) A port number is a
16-bit unsigned integer, thus ranging from 0 to 65535. A process
associates its input or output channel file descriptors (sockets) with a
port number and an IP address, a process known as binding, to send and
receive data via the network. The operating system's networking software
has the task of transmitting outgoing data from all application ports
onto the network, and forwarding arriving network packets to a process
by matching the packets IP address and port numbers.
Example: You request a connection to a P2P program then the
program opens a new socket and binds that to the specified port and
destination IP address to connect and transfer packets (data/bytes) back
and forth through the created socket and binded port to the destination
IP.