You are on page 1of 10

Client-Server Model

➢ Most networking applications are written assuming one side is the client
and the other the server (client to server model or peer to peer model)
➢ The purpose of the application is for the server to provide some defined
service for clients.
➢ We can categorize servers into two classes: iterative or concurrent.
➢ An iterative server iterates through the following steps:
1. Wait for a client request to arrive.
2. Process the client request.
3. Send the response back to the client that sent the request.
4. Go back to step 1.
➢ The problem with an iterative server is when step 2 takes a while. During
this time no other clients are serviced.
➢ A concurrent server performs the following steps.
1. Wait for a client request to arrive.
2. Start a new server to handle this client’s request.
This may involve creating a new process or task, depending on what the underlying
operating system supports. How this step is performed depends on the operating
system. This new server handles this client’s entire request. When complete, this new
server terminates.
3. Go back to step 1.
Dr. Elhossiny Ibrahim 0 4th year: network programming
Client-Server Model

➢ The advantage of a concurrent server is that the server just spawns other
servers to handle the client requests.
➢ Each client has, in essence, its own server.
➢ Assuming the operating system allows multiprogramming, multiple clients
are serviced concurrently.
➢ The reason we categorize servers, and not clients, is because a client
normally can’t tell whether it’s talking to an iterative server or a
concurrent server.
➢ As a general rule:
➢ TCP servers are concurrent.
➢ UDP servers are iterative.
➢ but there are a few exceptions.

Dr. Elhossiny Ibrahim 1 4th year: network programming


TCP Client-Server

Dr. Elhossiny Ibrahim 2 4th year: network programming


Various Methods in Socket Module for
both Server and Client programming
➢ Socket Server Methods
• These methods are used on the server-side.
❑ s.bind(): Binds address to the socket. The address contains the two tuple
of hostname (IP add.) in string format and the port number, so that it can listen
to incoming requests on that IP and port.
❑ s.listen(): Puts the server into listening mode to allow the server to listen
to incoming connections.
❑ s.accept(): Passively accepts the client connection and blocks until the
connection arrives (initiates a session with the client).
❑ c.close(): Closes the connection with the client.
➢ Socket Client Methods
• This method is used on the client side.
❑ s.connect(): Actively starts the TCP server connection
➢ Socket General Methods
• These are the general methods of the socket module.
❑ s.send(): Sends the TCP message
❑ s.recv(): Receives the TCP message.
❑ s.close(): Close the socket
❑ s.sendto(): Sends the UDP message
❑ s.recvfrom(): Receives the UDP message

Dr. Elhossiny Ibrahim 3 4th year: network programming


Socket Programming
➢ Python provides two levels of access to network programming.
• Low-Level Access: you can access the basic socket support of the
operating system. You can implement client and server for both
connection-oriented and connectionless protocols.
• High-Level Access: allows to implement protocols like HTTP, FTP, etc.
➢ What are Sockets?
• Consider a bidirectional communication channel, the sockets are the
endpoints of this communication channel.
• These sockets (endpoints) can communicate within a process, between
processes on the same machine, or on different machines.
• Sockets use different protocols for determining the connection type for
port-to-port communication between clients and servers.
➢ Domain: The family of protocols used for transport mechanisms like
AF_INET, PF_INET, AF_UNIX, etc.
➢ Type: Type of communication between sockets TCP or UDP.
➢ Protocol: Identifies the type of protocol used within domain and type.
Typically it is zero.
➢ Port: The server listens for clients calling on one or more ports. it can be a
string containing a port number, a name of the service, or a Fixnum port.
➢ Hostname: Identifies a network interface. It can be a string or an integer.
Dr. Elhossiny Ibrahim 4 4th year: network programming
Socket Programming
➢ Socket programming is a way of connecting two nodes on a network to
communicate with each other.
➢ One socket (node) listens on a particular port at an IP, while the other socket
reaches out to the other to form a connection.
➢ The server forms the listener socket while the client reaches out to the server.
They are the real backbones behind web browsing.
➢ In simpler terms, there is a server and a client. We can use the socket module
for socket programming.
➢ For this, we have to include the socket module –import socket
➢ to create a socket we have to use the socket.socket() method.
Syntax: socket.socket(socket_family, socket_type, protocol=0)
➢ Where,
• socket_family: specify address family either AF_UNIX or AF_INET (IPv4).
• socket_type: Either SOCK_STREAM (for TCP) or SOCK_DGRAM (for UDP) .
• protocol: Usually left out, defaulting to 0.
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> print(s)
socket.socket fd=5096, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0
OR >>> from socket import *
>>> s = socket(AF_INET, SOCK_STREAM)
Dr. Elhossiny Ibrahim 5 4th year: network programming
Simple Server-Client Program
Server Side
• From socket import * # first of all import the socket library
• s = socket(AF_INET,SOCK_STREAM) # next create a socket object
• print ("Socket successfully created") socket successfully created
# reserve a port on your computer in our case it is 40674 but it can be anything
• host ='127.0.0.1'
• port = 40674
# Next bind to the host (IP address) and port number.
# We may not type any ip in the ip field or input an empty string this makes the server listen to
requests coming from other computers on the network.
• s.bind((host, port)) # the arguments in bind is in tuple format
• print ("socket binded to ",port) socket binded to 40674
• s.listen(5) # put the socket into listening mode
• print ("socket is listening") socket is listening
# a forever loop until we interrupt it or an error occurs
• while True:
• c, addr = s.accept() # Establish connection with client.
• print ('Get connection from', addr )
Get connection from (‘127.0.0.1‘, 55182)
• c.send(b'Thank you for connecting') # send a message to the client.
• c.close() # Close the connection with the client
Dr. Elhossiny Ibrahim 6 4th year: network programming
Simple Server-Client Program
Server Side
➢ Explanation:
➢ We made a socket object and reserved a host and port on our pc.
➢ After that, we bound our server to the specified host and port.
➢ The host may be IP address or empty string.
➢ Passing an empty string means that the server can listen to incoming
connections from any available interface (other computers).
➢ Here, we passed 127.0.0.1 (standard address of loopback), it would have
listened to only those calls made within the local computer.
➢ Port number accept connection from client (any integer from 1024 to 65535).
➢ After that, we put the server into listening mode that has a backlog parameter.
5 here means that 5 connections are kept waiting if the server is busy and if a
6th socket tries to connect then the connection is refused.
➢ At last, we make a while loop and start to accept all incoming connections.
➢ When a client connects, it returns a new socket object (session) representing
the connection and a tuple holding the address of the client. The tuple will
contain (host, port) for IPv4 connections
➢ Now we have a new socket object from s.accept(). This is important because
it’s the socket that we’ll use to communicate with the client. It’s distinct from
the listening socket that the server is using to accept new connections.
➢ Finally, close this connection after sending a thank you message to this
connected sockets.
Dr. Elhossiny Ibrahim 7 4th year: network programming
Simple Server-Client Program
Server Side
• Now we need something with which a server can interact. We could telnet
to the server just to know that our server is working.
• Type this command in another terminal:
➢ telnet localhost 40674
• # start the server python server.py. keep the above terminal open and
open another terminal and type:

Dr. Elhossiny Ibrahim 8 4th year: network programming


Simple Server-Client Program
Client Side
• from socket import * # Import socket module
• s = socket(AF_INET,SOCK_STREAM) # Create a socket object
• host ='127.0.0.1'
• port = 40674 # Define the port on which you want to connect
• s.connect((host, port)) # connect to the server on local computer
• print(s.recv(1024)) # receive data from the server
• s.close() # close the connection

➢ Explanation:
• We connect to localhost on port 40674 (the port on which our server runs) and
lastly, we receive data from the server and close the connection.
• Now save this file as client.py and run it from the terminal after starting the
server script.

Dr. Elhossiny Ibrahim 9 4th year: network programming

You might also like