ADVANCE PROGRAMMING
M. Salim Hamdard
1
Assistant Professor,
Department of Software Engineering,
Kabul University, Afghanistan.
08/15/2025
LEARNING OUTCOME
To learn about the basic concepts of network
programming.
To learn how to exchange data between server
and client.
Practical work
2
CLIENT AND SERVER
We know that in a network, server is a machine
or program that provide services to clients.
In programming clients and servers are
applications or processes that can run locally on
a single computer or remotely across a network
of computers.
In order to communicate the resources required
for this type of application are IP addresses,
sockets, and ports.
3
IP ADDRESS
Every computer on a network has a unique
identifier called an IP address (IP stands for
Internet Protocol).
This address can be specified either as an IP
number or as an IP name.
An IP number typically has the form
ddd.ddd.ddd.ddd, where each d is a digit. For
example, 137.112.194.77.
Because IP numbers can be difficult to
remember, people customarily use an IP name to
specify an IP address.
4
CONT..
Python’s socket module includes two functions
that can provide IP related information:
gethostname() : Returns the IP name of the host
computer running the Python interpreter.
gethostbyname(ipName) : Returns the IP number of
the computer whose IP name is ipName. Raises an
exception if ipName cannot be found.
5
PORTS
Clients connect to servers via objects known as
ports. Ports are usually specified by numbers.
Some ports are dedicated to special servers or
tasks. For example, Port number 80 is reserved
for a web server.
Most computers also have hundreds or even
thousands of free ports available for use by any
network applications.
6
WHAT IS SOCKET?
You can write a python script that is a client to a
server. To do this, you need to use a socket.
A socket is an object that serves as a
communication link between a single server
process and a single client process.
7
CONT..
8
THE SOCKET MODULE
To create a socket, you must use the socket()
function available in socket module, which has
the general syntax:
s = socket.socket (socket_family, socket_type, protocol=0)
Here is the description of the parameters:
socket_family: The family of protocols that will be used
as the transport mechanism such as AF_UNIX or AF_INET.
socket_type: The type of communications between the
two endpoints, typically SOCK_STREAM for connection-
oriented protocols and SOCK_DGRAM for connectionless
protocols.
protocol: This is usually left out, defaulting to 0.
9
SERVER SOCKET METHODS
Method Description
bind() This method binds address
(hostname, port number
pair) to server socket.
listen() This method sets up and
start TCP listener.
accept() This passively accept TCP
client connection, waiting
until connection arrives.
10
CLIENT SOCKET METHODS
Method Description
connect() This method actively
initiates TCP server
connection.
11
GENERAL SOCKET METHODS
Method Description
recv() This method receives TCP
message
send() This method transmits TCP
message
recvfrom() This method receives UDP
message
sendto() This method transmits UDP
message
close() This method closes socket
12
A SIMPLE SERVER
To write Internet servers, we use the socket
function available in socket module to create a
socket object.
Call bind(hostname, port) function to specify a
port for your service on the given host.
Next, call the accept method this method waits
until a client connects to the port you specified,
and then returns a connection object that
represents the connection to that client.
13
CONT..
14
A SIMPLE CLIENT
Now we will write a very simple client program
which will open a connection to a given port and
given host.
The socket.connect(hosname, port ) opens a TCP
connection to hostname on the port.
Once you have opened connection, you can
receive data send by server.
15
CONT..
16
END