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 −
[Link]. 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 [Link]() function available in socket module, which has the general
syntax −
s = [Link] (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
[Link]. Method & Description
1 [Link]()
This method binds address (hostname, port number pair) to socket.
2 [Link]()
This method sets up and start TCP listener.
3 [Link]()
This passively accept TCP client connection, waiting until connection arrives
(blocking).
Client Socket Methods
[Link]. Method & Description
1 [Link]()
This method actively initiates TCP server connection.
General Socket Methods
[Link]. Method & Description
1 [Link]()
This method receives TCP message
2 [Link]()
This method transmits TCP message
3
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
3 [Link]()
This method receives UDP message
4 [Link]()
This method transmits UDP message
5 [Link]()
This method closes socket
6 [Link]()
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 [Link] file
import socket # Import socket module
s = [Link]() # Create a socket object
host = [Link]() # Get local machine name
port = 12345 # Reserve a port for your service.
[Link]((host, port)) # Bind to the port
4
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
[Link](5) # Now wait for client connection.
while True:
c, addr = [Link]() # Establish connection with client.
print 'Got connection from', addr
[Link]('Thank you for connecting')
[Link]() # 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 [Link](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 [Link] file
import socket # Import socket module
s = [Link]() # Create a socket object
host = [Link]() # Get local machine name
port = 12345 # Reserve a port for your service.
[Link]((host, port))
print [Link](1024)
[Link] # Close the socket when done
Now run this [Link] in background and then run above [Link] 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 [Link] &
# Once server is started run client as follows:
$ python [Link]
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