exp 14 to 16 of computer networking
exp 14 to 16 of computer networking
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.
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
1
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
3 protocol
4 hostname
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.
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
1 s.bind()
2 s.listen()
3 s.accept()
This passively accept TCP client connection, waiting until connection arrives
(blocking).
1 s.connect()
1 s.recv()
2 s.send()
3
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
3 s.recvfrom()
4 s.sendto()
5 s.close()
6 socket.gethostname()
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.
4
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
while True:
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 −
s.connect((host, port))
print s.recv(1024)
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
$ python client.py
Data=”hello client”
6
Computer Networks Lab Manual PCC CS 692
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
ALGORITHM:
#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
ALGORITHM:
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();
}
12
Computer Networks Lab Manual PCC CS 692