5th ex:-
PROGRAM:
SERVER:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define SIZE 4
main()
{
int sd,ld,len,i,j,st;
char str[20],frame[20],temp[20],ack[20];
struct sockaddr_in saddr,caddr;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
perror("error");
bzero(&saddr,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_addr.s_addr=htonl(INADDR_ANY);
saddr.sin_port=htons(6616);
if(bind(sd,(struct sockaddr *)&saddr,sizeof(saddr))<0)
perror("bind error");
listen(sd,5);
len=sizeof(&caddr);
ld=accept(sd,(struct sockaddr *)&caddr,&len);
printf("enter the text\n");
scanf("%s",str);
i=0;
while(i<strlen(str))
{
memset(frame,0,20);
strncpy(frame,str+1,SIZE);
printf("transmitting frames");
len=strlen(frame);
for(j=0;j<len;j++)
{
printf("%d",i+j);
sprintf(temp,"%d",i+j);
strcat(frame,temp);
}
printf("\n");
write(ld,frame,sizeof(frame));
read(ld,ack,20);
sscanf(ack,"%d",&st);
if(st==-1)
printf("transmission is successful\n");
{
printf("received error in %d\n\n",st);
printf("\nretransmitting frame");
for(j=0;;)
{
frame[j]=str[j+st];
j++;
printf("%d",j+st);
if((j+st)%4==0)
break;
}
printf("\n");
frame[j]='\0';
len=strlen(frame);
for(j=0;j<len;j++)
{
sprintf(temp,"%d",j+st);
strcat(frame,temp);
}
write(ld,frame,sizeof(frame));
}
i+=SIZE;
}
write(ld,"exit",sizeof("exit"));
printf("\nexiting\n");
sleep(2);
close(ld);
close(sd);
}
CLIENT:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
main()
{
int sd,len,op;
char str[20],err[20];
struct sockaddr_in saddr;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
perror("ERROR");
bzero(&saddr,sizeof(saddr));
saddr.sin_family=AF_INET;
inet_pton(AF_INET,"172.16.6.4",&saddr.sin_addr);
saddr.sin_port=htons(6616);
connect(sd,(struct sockaddr *)&saddr,sizeof(saddr));
for(;;)
{
read(sd,str,20);
if(!strcmp(str,"exit"))
{
printf("exiting\n");
break;
}
printf("\n\nreceived%s\n\n1.do u want to report an error(1-yes,0-no)",str);
scanf("%d",op);
if(op)
{
printf("enter the sequence no. of frame when error has occured\n");
scanf("%s",err);
write(sd,err,sizeof(err));
read(sd,str,20);
printf("\n\nreceived the retransmitted frames %s\n\n",str);
}
else
write(sd,"-1",sizeof("-1"));
}
}
OUTPUT:
SERVER:
[it86091@vecit ~]$ cc prog5b.c
[it86091@vecit ~]$ ./a.out
enter the text
hello
transmitting frames0123
received error in 3
retransmitting frame4
transmitting frames4567
received error in 1
retransmitting frame234
exiting
CLIENT:
[it86091@vecit ~]$ cc prog5a.c
[it86091@vecit ~]$ ./a.out
receivedello0123
1.do u want to report an error(1-yes,0-no)1
enter the sequence no. of frame when error has occured
3
received the retransmitted frames l3
receivedello4567
1.do u want to report an error(1-yes,0-no)
enter the sequence no. of frame when error has occured
1
received the retransmitted frames ell123
exiting
No comments:
Post a Comment