Socket Programming
Socket Programming
Socket Programming
..
Sockets
• A network socket is a virtual end point where entities can perform
inter-process communication.
• Sockets are the endpoints of a bidirectional, point-to-point
communication channel e.g. one process sitting in a computer,
exchanges data with another process sitting on the same or another
computer.
• Given an internet connection, say between client(a browser) and the
server (uz.ac.zw), we will have two sockets. A Client Socket and a
Server Socket
• The first process which initiates the communication as the client and
the latter one as the server.
• Socket acts on two parts: IP Address + Port Number
Sockets
• When you click on a link to a page, your browser does the
following:
#a socket object is created for communication:
Clientsocket =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# now connect to the web server on port 80
# the normal http port
clientsocket.connect((“www.uz.ac.zw", 80))
Ports
• A port is an endpoint to a logical connection.
• The port number identifies what type of port it is e.g, port 80 is used
for HTTP traffic.
• Three categories of TCP and UDP ports:
Categories of ports
Well-known ports:
•Used for services that need to use specific ports.
•Ports 0–1023 are considered well-known ports because they are
used by many of the core services on the Unix servers, and most
required privilege permissions on the server to implement.
•Examples:
• Telnet (23)
• Simple Mail Transport Protocol (SMTP) (25)
•HyperText Transfer Protocol (HTTP) (80)
•File Transfer Protocol (FTP) (20 &21)
•Domain Name System (DNS) (53)
Categories of ports
Registered ports
• The Internet Assigned Numbers Authority (IANA) keeps a list of all
services that run on both the well-known ports and on all registered
ports.
• The registration process puts a permanent association in place with
the port number and the service.
• These services are all long-running services and would be assigned to
ports between 1,024 and 49,151.
• The Microsoft Remote Desktop Protocol (RDP) (3389) and Network
File System (NFS) (2049) are two examples of registered ports.
Categories of ports
Dynamic and/or private ports
• All other ports, from 49,152 to 65,535, are referred to as dynamic, or
private ports. (they are not permanently associated to any service)
• If you write your own service, you can configure it to use any dynamic
port that you want, but someone else may write his own service and
use the same port. This will not cause any issue until you install both
services on the same IP host because they are both going to want to
use the same port, and that is just not possible.
Python Sockets
• Two type of sockets:
• SOCK_STREAM [Stream Socket (TCP) ] and SOCK_DGRAM [(Datagram Socket
for UDP connection)]. Below we have a comparison of both types of
sockets
SOCK_STREAM SOCK_DGRAM
For TCP protocols For UDP protocols
Reliable delivery Unrelible delivery
Guaranteed correct ordering of
No order guaranteed
packets
Connection-oriented No notion of connection(UDP)
Bidirectional Not Bidirectional
Abstract View of Communication between
Web and Server
Creating Sockets
To create a socket, we must use:
socket.socket() function available in the Python socket module, which
has the general syntax as follows:
s = socket.socket(socket_family, socket_type, protocol=0)
• socket_family: This is either AF_UNIX or AF_INET.
• socket_type: This is either SOCK_STREAM or SOCK_DGRAM
• Protocol: This is usually left out, defaulting to 0
.
Socket Methods
Client
connect( )
• To connect to a remote socket at an address.
• An address format(host, port) pair is used for AF_INET address family
Socket Methods
Server