[go: up one dir, main page]

0% found this document useful (0 votes)
14 views12 pages

exp 14 to 16 of computer networking

The document outlines experiments for a Computer Networks Lab course, focusing on socket programming using Python and flow control mechanisms in data link layers. It provides detailed descriptions of socket terminology, methods for server and client sockets, and algorithms for implementing flow control mechanisms such as Stop and Wait and Selective Repeat protocols. Sample code is included for both server and client implementations, as well as flow control simulations.

Uploaded by

Subham Patra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

exp 14 to 16 of computer networking

The document outlines experiments for a Computer Networks Lab course, focusing on socket programming using Python and flow control mechanisms in data link layers. It provides detailed descriptions of socket terminology, methods for server and client sockets, and algorithms for implementing flow control mechanisms such as Stop and Wait and Selective Repeat protocols. Sample code is included for both server and client implementations, as well as flow control simulations.

Uploaded by

Subham Patra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ASANSOL ENGINEERING COLLEGE

Department of Computer Science and Engineering

Course Name: COMPUTER Experiment No. 14


NETWORKS LAB

Course Code : PCC-CS692 Branch: Semester: 5


CSE

TCP/UDP Client and server socket(using python)

Aim: Study of socket program and their implementation steps.

What is Sockets?
Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a
process, between processes on the same machine, or between processes on different continents.

Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and
so on. The socket library provides specific classes for handling the common transports as well as a generic
interface for handling the rest.

Sockets have their own vocabulary −

Sr.No. Term & Description

1 Domain

The family of protocols that is used as the transport mechanism. These values
are constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.

2 type

The type of communications between the two endpoints, typically


SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for
connectionless protocols.

1
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering

3 protocol

Typically zero, this may be used to identify a variant of a protocol within a


domain and type.

4 hostname

The identifier of a network interface −

 A string, which can be a host name, a dotted-quad address, or an IPV6


address in colon (and possibly dot) notation

 A string "<broadcast>", which specifies an INADDR_BROADCAST


address.

 A zero-length string, which specifies INADDR_ANY, or

 An Integer, interpreted as a binary address in host byte order.

5 port

Each server listens for clients calling on one or more ports. A port may be a
Fixnum port number, a string containing a port number, or the name of a
service.

The socket Module


To create a socket, you must use the socket.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 − This is either AF_UNIX or AF_INET, as explained earlier.

 socket_type − This is either SOCK_STREAM or SOCK_DGRAM.

 protocol − This is usually left out, defaulting to 0.

Once you have socket object, then you can use required functions to create your client or server program.
Following is the list of functions required −
2
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering

Server Socket Methods


Sr.No. Method & Description

1 s.bind()

This method binds address (hostname, port number pair) to socket.

2 s.listen()

This method sets up and start TCP listener.

3 s.accept()

This passively accept TCP client connection, waiting until connection arrives
(blocking).

Client Socket Methods


Sr.No. Method & Description

1 s.connect()

This method actively initiates TCP server connection.

General Socket Methods


Sr.No. Method & Description

1 s.recv()

This method receives TCP message

2 s.send()

This method transmits TCP message

3
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering

3 s.recvfrom()

This method receives UDP message

4 s.sendto()

This method transmits UDP message

5 s.close()

This method closes socket

6 socket.gethostname()

Returns the hostname.

A Simple Server
To write Internet servers, we use the socket function available in socket module to create a socket object. A
socket object is then used to call other functions to setup a socket server.

Now call bind(hostname, port) function to specify a port for your service on the given host.

Next, call the accept method of the returned object. 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.

#!/usr/bin/python # This is server.py file

import socket # Import socket module

s = socket.socket() # Create a socket object

host = socket.gethostname() # Get local machine name

port = 12345 # Reserve a port for your service.

s.bind((host, port)) # Bind to the port

4
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering

s.listen(5) # Now wait for client connection.

while True:

c, addr = s.accept() # Establish connection with client.

print 'Got connection from', addr

c.send('Thank you for connecting')

c.close() # Close the connection

A Simple Client
Let us write a very simple client program which opens a connection to a given port 12345 and given host. This
is very simple to create a socket client using Python's socket module function.

The socket.connect(hosname, port ) opens a TCP connection to hostnameon the port. Once you have a socket
open, you can read from it like any IO object. When done, remember to close it, as you would close a file.

The following code is a very simple client that connects to a given host and port, reads any available data from
the socket, and then exits −

#!/usr/bin/python # This is client.py file

import socket # Import socket module

s = socket.socket() # Create a socket object

host = socket.gethostname() # Get local machine name

port = 12345 # Reserve a port for your service.

s.connect((host, port))

print s.recv(1024)

s.close # Close the socket when done

Now run this server.py in background and then run above client.py to see the result.
5
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering

# Following would start a server in background.

$ python server.py &

# Once server is started run client as follows:

$ python client.py

Data=”hello client”

6
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering

Course Name: COMPUTER Experiment No. 15


NETWORKS LAB

Course Code : PCC-CS692 Branch: Semester: 5


CSE

Data Link Layer Flow Control Mechanism(Stop and wait)


Aim: Study of flow control mechanism.

ALGORITHM:

Step 1: Start the program.


Step 2: Generate a random that gives the total number of frames to be transmitted.
Step 3: Set the size of the window.
Step 4: Generate a random number less than or equal to the size of the current window
and identify the number of frames to be transmitted at a given time.
Step 5: Transmit the frames and receive the acknowledgement for the frames sent.
Step 6: Find the remaining frames to be sent.
Step 7: Find the current window size.
Step 8: If an acknowledgement is not received for a particular frame retransmit the
frames from that frame again.
Step 9: Repeat the steps 4 to 8 till the number of remaining frames to be send becomes
zero.
Step 10: Stop the program.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int temp1,temp2,temp3,temp4,i,winsize=8,noframes,moreframes;
char c;

int receiver(int);
int simulate(int);
clrscr();
temp4=0,temp1=0,temp2=0,temp3=0;
for(i=0;i<200;i++)
rand();
7
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
noframes=rand()/200;
printf("\n number of frames is %d",noframes);
getch();
moreframes=noframes;
while(moreframes>=0)
{
temp1=simulate(winsize);
winsize-=temp1;
temp4+=temp1 ;
if(temp4 >noframes)
temp4 = noframes;
for(i=temp3+1;i<=temp4;i++)
printf("\nsending frame%d",i);
getch();
temp2=reciever(temp1);
temp3+=temp2;
if(temp3 > noframes)
temp3=noframes;
printf("\n acknowledgement for the frames up to %d",temp3);
getch();
moreframes-=temp2;
temp4=temp3;
if(winsize<=0)
winsize=8;
}
printf("\n end of sliding window protocol");
getch();
}
int reciever(int temp1)
{
int i;
for(i=1;i<100;i++)
rand();
i=rand()%temp1;
return i;
}
int simulate(int winsize)
{
int temp1,i;
for(i=1;i<50;i++)
temp1=rand();
if(temp1==0)
temp1=simulate(winsize);
i = temp1%winsize;
8
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
if(i==0)
return winsize;
else
return temp1%winsize;
}

9
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering

Course Name: COMPUTER Experiment No. 16


NETWORKS LAB

Course Code : PCC-CS692 Branch: Semester: 5


CSE

SLIDING WINDOW PROTOCOL (SELECTIVE REPEAT)

Aim: Study of flow control mechanism.

To write a program to perform simulation on sliding window protocol.

ALGORITHM:

Step 1: Start the program.


Step 2: Generate a random that gives the total number of frames to be transmitted.
Step 3: Set the size of the window.
Step 4: Generate a random number less than or equal to the size of the current window
and identify the number of frames to be transmitted at a given time.
Step 5: Transmit the frames and receive the acknowledgement for the frames sent.
Step 6: Find the remaining frames to be sent.
Step 7: Find the current window size.
Step 8: If an acknowledgement is not received for a particular frame retransmit that frame
alone again.
Step 9: Repeat the steps 4 to 8 till the number of remaining frames to be send becomes
zero.
Step 10: Stop the program.

CODING:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int temp1,temp2,temp3,temp4,temp5,i,winsize=8,noframes,moreframes;
char c;
int reciever(int);
int simulate(int);
int nack(int);
10
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
clrscr();
temp4=0,temp1=0,temp2=0,temp3=0,temp5=0;
for(i=0;i<200;i++)
rand();
noframes=rand()/200;
printf("\n number of frames is %d",noframes);
getch();
moreframes=noframes;
while(moreframes>=0)
{
temp1=simulate(winsize);
winsize-=temp1;
temp4+=temp1;
if(temp4 >noframes)
temp4 = noframes;
for(i=noframes-moreframes;i<=temp4;i++)
printf("\nsending frame %d",i);
getch();
temp2=reciever(temp1);
temp3+=temp2;
if(temp3 > noframes)
temp3 = noframes;
temp2 = nack(temp1);
temp5+=temp2;
if(temp5!=0)
{
printf("\nNo acknowledgement for the frame %d",temp5);
getch();
for(i=1;i<temp5;i++)
;
printf("\nRetransmitting frame %d",temp5);
getch();
}
moreframes-=temp1;
if(winsize<=0)
winsize=8;
}
printf("\nend of sliding window protocol Selective Reject");
getch();
}

int reciever(int temp1)


{
int i;
11
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
for(i=1;i<100;i++)
rand();
i=rand()%temp1;
return i;
}

int nack(int temp1)


{
int i;
for(i=1;i<100;i++)
rand();
i=rand()%temp1 ;
return i;
}
int simulate(int winsize)
{
int temp1,i;
for(i=1;i<50;i++)
temp1=rand();
if(temp1==0)
temp1=simulate(winsize);
i=temp1%winsize;
if(i==0)
return winsize;
else
return temp1%winsize;
}

12
Computer Networks Lab Manual PCC CS 692

You might also like