LAB 1-Introduction To Socket Programming: Objectives
LAB 1-Introduction To Socket Programming: Objectives
LAB 1-Introduction To Socket Programming: Objectives
Objectives:
Objective of this lab is to get basic knowledge about sockets, server and client. Also the Introduction to
socket programming in python.
Explanation:
OSI model
The Open Systems Interconnection model (OSI model) is a conceptual model that characterizes and
standardizes the communication functions of a telecommunication or computing system without regard
to its underlying internal structure and technology. Its goal is the interoperability of diverse
communication systems with standard communication protocols. The model partitions a communication
system into abstraction layers. The original version of the model had seven layers.
A socket is one endpoint of a two-way communication link between two programs running
on the network. A socket is bound to a port number so that the TCP layer can
identify the application that data is destined to be sent to. An endpoint is a combination of an IP address
and a port number. Sockets are commonly used for client and server interaction. Typical system
configuration places the server on one machine, with the clients on other machines. The clients connect
to the server, exchange information, and then disconnect.
A socket has a typical flow of events. In a connection-oriented client-to-server model, the socket on the
server process waits for requests from a client. To do this, the server first establishes (binds) an address
that clients can use to find the server. When the address is established, the server waits for clients to
request a service. The client-to-server data exchange takes place when a client connects to the server
through a socket. The server performs the client's request and sends the reply back to the client.
1. Socket programming is started by importing the socket library and making a simple socket.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Here we made a socket instance and passed it two parameters. The first parameter is AF_INET and the
second one is SOCK_STREAM. AF_INET refers to the address family ipv4. The SOCK_STREAM means
connection oriented TCP protocol.
Now we can connect to a server using this socket.
2. Connecting to a server:
Note that if any error occurs during the creation of a socket then a socket.error is thrown and we can
only connect to a server by knowing it’s ip. You can find the ip of the server by using this :
$ ping www.google.com
ip = socket.gethostbyname('www.google.com')
print ip
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket successfully created"
except socket.error as err:
print "socket creation failed with error %s" %(err)
try:
host_ip = socket.gethostbyname('www.google.com')
except socket.gaierror:
Client-Server Programming:
Server:
• Create a socket object for socket creation by import socket
Socket=socket.socket() // this will create a socket object
• bind() method which binds it to a specific ip and port so that it can listen to incoming
requests on that ip and port
socket.bind((host,IP)) // this takes host IP address and port number as inputs
• listen() method which puts the server into listen mode. This allows the server to listen to
incoming connections.
socket.listen(2) // this means it can take two active connections on 3rd one it will fail
• recv() The recv method receives the data packet containing the message from the
client
data = conn.recv(1024).decode() // conn is return result from accept and 1024 means
data packets cannot be larger from 1024 bytes
• send() The send method is used to send the data packet containing the message to the client
Client:
• Create socket object same as created for server
• Connect to the server using connect(host ip, server port)
• Send message to the server using send(message.encode())
• Receive response from server using recv()
• At the end close connection with server by close()
TASK:
I. Write a simple server and client code to send and receive data to
and from server-client. Continue the communication between
server and client and close server connection as soon as client
enters bye. [You can follow the steps given above and syntax
with logic you want to develop]