LAB 1-Introduction To Socket Programming: Objectives

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

LAB 1- Introduction to Socket Programming

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.

What are sockets? How they work?

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.

Socket creation needs : <Port No.><IP address>

Python Programming for Socket Creation:

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

You can also find the ip using python:


import socket

ip = socket.gethostbyname('www.google.com')
print ip

# An example script to connect to Google using socket


# programming in Python
import socket # for socket
import sys

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)

# default port for socket


port = 80

try:
host_ip = socket.gethostbyname('www.google.com')
except socket.gaierror:

# this means could not resolve the host


print "there was an error resolving the host"
sys.exit()

# connecting to the server


s.connect((host_ip, port))

print "the socket has successfully connected to google \


on port == %s" %(host_ip)
Output :
Socket successfully created
the socket has successfully connected to google
on port == 173.194.40.19
• First of all we made a socket.
• Then we resolved google’s ip and lastly we connected to google.
• Now we need to know how can we send some data through a socket.
• For sending data the socket library has a sendall function. This function allows you to send data to
a server to which the socket is connected and server can also send data to the client using this
function.

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

• accept() The accept method initiates a connection with the client


Conn,address=socket.accept() // server accept the active connection and return the
infor about socket connection in conn and ip,port no of client in address

• 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

conn.send(data.encode()) // data is the message server entered for sending to client

• close() it closes the socket connection with the particular client

conn.close() // connection with the conn socket client closes

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]

You might also like