Você está na página 1de 11

Ex.No.

1B

SIMULATION OF SLIDING WINDOW PROTOCOL

AIM:
To write a C program for the simulation of Sliding Window Protocol.
ALGORITHM:
SERVER
1.

A TCP socket is created.

2.

An Internet socket address structure is filled in with the wildcard address (INADDR_ANY) and the servers
well-known port (PORT).

3.

The socket is converted into a listening socket by calling the listen function.

4.

The server blocks in the call to accept, waiting for the client connection to complete.

5.

When the connection is established, the server reads the source file name (which is to be transferred) from
the client using readn.

6.

Then the contents of the source file are transferred byte by byte (assuming the window size is 1) to the
client.

7.

Finally, the server closes the connected socket.

CLIENT
1.

A TCP socket is created.

2.

An Internet socket address structure is filled in with the servers IP address and the same port

3.

The connect function establishes the connection with the server.

4.

The client reads a source file name and the destination file name from the user, sends the source file name
to the server using written.

5.

It then receives the byte by byte content of the file from the server and displays it.

PROGRAM:
SERVER:

number.

#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<sys/types.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define PORT 8086
int main(int argc,char **argv)
{
int sfd,cfd,slen,clen,i,ch,len;
FILE *fp;
char buff[MAX],filename[MAX];
struct sockaddr_in servaddr,cliaddr;
if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("Socket not created\n");
exit(0);
}
slen=sizeof(servaddr);
memset(&servaddr,slen,0);
servaddr.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_port=htons(PORT);
servaddr.sin_family=AF_INET;
if(bind(sfd,(struct sockaddr *)&servaddr,slen)<0)
{
printf("Bind failed\n");
close(sfd);
exit(1);
}
printf("SERVER SOCKET BINDED\n");
listen(sfd,5);
printf("SERVER SOCKET listened\n");
len=sizeof(cliaddr);
if((cfd=accept(sfd,(struct sockaddr *)&cliaddr,&len))<0)
{
printf("Accept failed\n");
close(sfd);
exit(1);

}
printf("SERVER Socket accepted connection with a Client\n");
bzero(filename,MAX);
if(recv(cfd,filename,sizeof(filename),0)<0)
{
printf("Did not receive the filename");
close(sfd);
close(cfd);
exit(1);
}
for(i=0;i<MAX;i++)
if(filename[i]=='\n'||filename[i]=='\r')
{
filename[i]='\0';
break;
}
printf("File requested: %s\n",filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("File could not be opened\n");
close(sfd);
close(cfd);
exit(1);
}
if((ch=fgetc(fp))==EOF)
{
close(sfd);
close(cfd);
fclose(fp);
exit(1);
exit(1);
}
bzero(buff,sizeof(buff));
buff[0]=ch;
buff[1]='\0';
send(cfd,buff,2,0);
printf("Sending:%s",buff);
while(!feof(fp))
{
bzero(buff,sizeof(buff));

do
{
recv(cfd,buff,MAX,0);
}
while(strcmp(buff,"ACK")!=0);
printf("Received ACK...\n");
if((ch=fgetc(fp))==EOF)
break;
bzero(buff,sizeof(buff));
buff[0]=ch;
buff[1]='\0';
send(cfd,buff,2,0);
printf("Sending:%s",buff);
}
send(cfd,"OVER",5,0);
printf("Sending: OVER");
close(sfd);
close(cfd);
close(fp);
return 0;
}
CLIENT
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define PORT 8086
int main(int argc,char *argv[])
{
int ct,sockfd;
int sfd,slen,clen,bufsize;
char buff[MAX];
FILE *fp;
struct sockaddr_in serv,cliaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<1)
{

perror("Socket error");
exit(0);
}
printf("CLIENT SOCKED CREATED");
bzero(&serv,sizeof(serv));
serv.sin_family=AF_INET;
serv.sin_port=htons(PORT);
serv.sin_addr.s_addr=inet_addr(argv[1]);
if(connect(sockfd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("ERROR IN CONNECT");
exit(1);
}
printf("CLIENT SOCKET CONNECTED");
printf("\nEnter filename");
scanf("%s",buff);
send(sockfd,buff,sizeof(buff),0);
do
{
bzero(buff,sizeof(buff));
recv(sockfd,buff,MAX,0);
if(strncmp(buff,"OVER",3)==0)
break;
printf("\nReceived:%s, sending ACK...\n",buff);
send(sockfd,"ACK",4,0);
}while(1);
close(sockfd);
printf("\nReceived and signal(OVER) terminating\n");
return 0;
}

Ex.No.3
AIM:

SIMULATING ARP/RARP PROTOCOLS

To write a C program to implement Address Resolution Protocol/Reverse Address Resolution Protocol.


ALGORITHM:
SERVER:
1. Include header files.
2. Create the socket address structure.
3. IP address, port number and family is initialized with servers socket address structure
4. Socket address is manipulated using the byte manipulation function bzero().
5. Socket created using socket() function.
6. Using bind () function, socket is bound with the servers well known port.
7. Enter number of client, accept those connection with those client with specified port.
8. The IP address is received from the client and the corresponding MAC address is sent to the client.
9. Socket is closed.
CLIENT
1. Include header files.
2. Socket address structure is created.
3. Servers socket address structure is initialized with IP address, port number and family.
4. Server and the clients address are manipulated using the byte manipulation function bzero().
5. Socket is created using socket () function.
6. Socket is bound with the servers well known port.
7. The IP address is sent to the server.
8. Corresponding MAC address is received from the server and it is printed.
9. Socket is closed.

PROGRAM:
SERVER:
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<string.h>

main()
{
int shmid, a, i;
char *ptr, *shmptr;
shmid=shmget(3000,10,IPC_CREAT | 0666);
shmptr=shmat(shmid,NULL,0);
ptr=shmptr;
for(i=0;i<3;i++)
{
puts("Enter the Mac address");
scanf("%s",ptr);
a=strlen(ptr);
printf("\nstring length:%d",a);
ptr[a]= ' ' ;
puts("\nEnter IP address");
ptr=ptr+a+1;
scanf("%s",ptr);
ptr[a]='\n' ;
ptr= ptr+a+1;
}
ptr[strlen(ptr)]= '\0';
printf("\n ARP table at serverside is\n%s", shmptr);
shmdt(shmptr);
}
CLIENT:
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/shm.h>
main()
{
int shmid,a;
char *ptr, *shmptr;
char ptr2[51], ip[12], mac[26];
shmid=shmget(3000,10,0666);
shmptr=shmat(shmid,NULL,0);
puts("the arp table is");
printf("%s",shmptr);
printf("\n1.ARP\n 2.RARP\n 3.EXIT\n");

printf(\n Enter your choice:\n);


scanf("%d",&a);
switch(a)
{
case 1:
puts("Enter IP address");
scanf("%s",ip);
ptr=strstr(shmptr, ip);
ptr-=8;
sscanf(ptr,"%s%*s",ptr2);
printf("MAC addr is %s",ptr2);
break;
case 2:
puts("Enter MAC addr");
scanf("%s",mac);
ptr=strstr(shmptr, mac);
sscanf(ptr,"%*s%s",ptr2);
printf("%s",ptr2);
break;
case 3:
exit(1);
}
}
Ex.No.4A

SIMULATING PING COMMAND

AIM:
To write a C program to implement ping command.
ALGORITHM:
SERVER:
1. Include necessary header file.
2. Corresponding structure for client address and server address is created.
3. Socket address structure is initialised with IP address, port number and family.
4. Socket is created using socket() function.
5. Using bind() function, servers port is bound to the socket.
6. Client's request is accepted using accept() function.

7. From the client message is received and reply is sent.


8. The socket is closed and the connection is terminated.
CLIENT.
1. Include necessary header file.
2. Structure for timeval tp,timezone p ,servaddr,cliaddr is created.
3. IP address,port number and familyof server & client initialized.
4. Socket is created using socket() function.
5. Connection is established with the server using connect() function.
6. Sequence number of packet ,time are displayed.
7. Socket is closed.

PROGRAM.
SERVER:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
int main()
{
char x[15],y[15];
int n,sd,size,sersock,i;
//creation of socket address structure for client and the server.
struct sockaddr_in cliaddr,servaddr;
/*initialising the socket address structure for client with port number,address
and family*/
cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(7800);
cliaddr.sin_addr.s_addr=INADDR_ANY;
/*initialising the socket address structure for server with port number,address and family*/
servaddr.sin_family=AF_INET;

servaddr.sin_port=htons(8000);
servaddr.sin_addr.s_addr=INADDR_ANY;
//manipulation of addresses of client and server.
bzero(&(cliaddr.sin_zero),8);
bzero(&(servaddr.sin_zero),8);
//creation of socket.
//AF_INET is the family for internet IP addresses.
//SOCK_DGRAM represents udp socket.
sd=socket(AF_INET,SOCK_DGRAM,0);
//binding the socket with the server's well known port.
bind(sd,(struct sockaddr *)&servaddr,sizeof(servaddr));
size=sizeof(cliaddr);
for(i=1;i<=4;i++)
{
//receiving the data from the client.
recvfrom(sd,x,sizeof(x),0,&cliaddr,size);
//printing the data.
printf("\n Received data : %s\n",x);
//sending data to the client.
sendto(sd,x,sizeof(x),0,&cliaddr,size);
}
//closing the created socket.
close(sd);
}
CLIENT:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/time.h>
#include<string.h>
int main()
{
char x[15],y[15];
int n,sd,size,sersock,i;
long sec,msec;
struct timeval tp;
struct timezone p;
struct sockaddr_in cliaddr,servaddr;

cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(7800);
cliaddr.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8000);
servaddr.sin_addr.s_addr=INADDR_ANY;
bzero(&(cliaddr.sin_zero),8);
bzero(&(servaddr.sin_zero),8);
sd=socket(AF_INET,SOCK_DGRAM,0);
bind(sd,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
size=sizeof(servaddr);
printf("\n Enter the data :\n");
scanf("%s",x);
for(i=0;i<=4;i++)
{
gettimeofday(&tp,&p);
sec=tp.tv_sec;
msec=tp.tv_usec;
sendto(sd,x,sizeof(x),0,&servaddr,size);
recvfrom(sd,y,sizeof(y),0,&servaddr,size);
gettimeofday(&tp,&p);
sec=tp.tv_sec-sec;
msec=tp.tv_usec-msec;
msec=msec+(sec*1000000);
if((strcmp(x,y))==0)
{
printf("\n 64 Bytes from 10.6.2.6 icmp_seq_no=%d time=%ldms\n",i,msec);
}
}
close(sd);
}

Você também pode gostar