Вы находитесь на странице: 1из 4

PROGRAM SERVER

#include<stdio.h> #include<sys/types.h> #include<netinet/in.h> #include<string.h> #include<sys/socket.h> int main() { int sd,nsd,bi,port=9032; int i,a=0,b=0,m=0,n=0,e,client,str[30]; char data[30]="\n Ack received"; struct sockaddr_in ser,cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\n Socket problem"); return 0; } printf("\n Socket created"); bzero((char *)&ser,sizeof(ser)); printf("\n port address is %d",port); ser.sin_family=AF_INET; ser.sin_port=htons(port); ser.sin_addr.s_addr=htons(INADDR_ANY); if((bi=bind(sd,(struct sockaddr *)&ser,sizeof (ser))==-1)) { printf("Bind error port busy... change port in client and server"); return 0; } i=sizeof(cli); listen(sd,5); nsd=accept(sd,(struct sockaddr *)&cli,&i); if(nsd==-1) { printf("Check the descriptor parameter"); return 0; } printf("\n Connection accepted"); client=recv(nsd,&m,2,0); client=recv(nsd,&n,2,0); printf("\n window size is %d ",n); for(i=1;i<=30;i++) str[i]=0; a=n; e=0; b=1;

while(m>0) { client=recv(nsd,&str,30,0); e=n-1; for(i=b;i<=a;i++) { printf("\n Received data is %d",str[i]); printf("\n Window size is %d",e); e--; } send(nsd,data,20,0); printf("\n Acknowledgment is sent \n"); b=a+1; m=m-n; if(m>=n) a=a+n; else a=a+m; } send(nsd,"EOF",4,0); close(sd); close(nsd); return 0; }

CLIENT
#include<stdio.h> #include<sys/types.h> #include<netinet/in.h> #include<string.h> #include<sys/socket.h> int main() { int sd,nsd,bi,port=9032; int i,a,m,b,n,ser,c; int st[30],str[30]; char data[20]="\0"; struct sockaddr_in cli; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\n Socket problem"); return 0; } printf("\n Socket created"); bzero((char *)&cli,sizeof(cli)); printf("\n port address is %d",port); cli.sin_family=AF_INET;

cli.sin_port=htons(port); cli.sin_addr.s_addr=htonl(INADDR_ANY); if((c=connect(sd,( struct sockaddr*)&cli, sizeof(cli)))== -1) { printf("\n sorry connection failed"); return 0; } printf("\n Connection is established between server and the client"); printf("\n Enter the size of the data"); scanf("%d",&m); send(sd,&m,2,0); printf("\n Enter the data "); for(i=1;i<=m;i++) scanf("%s",&st[i]); printf("\n Enter the window size"); scanf("%d",&n); send(sd,&n,2,0); a=n; b=1; while(m>0) { for(i=b;i<=a;i++) str[i]=st[i]; printf("\n Ready to send"); send(sd,&str,30,0); ser=recv(sd,data,20,0); printf("%s \n",data); b=a+1; m=m-n; if(m>=n) a=a+n; else a=a+m; } send(sd,"exit",5,0); printf("\n BYE....."); close(sd); return 0; }

OUTPUT SERVER

Socket created port address is 9032 Connection accepted window size is 25715 Acknowledgment is sent Acknowledgment is sent [user@localhost ~]$ cc slideser.c [user@localhost ~]$ ./a.out Socket created port address is 9032 Connection accepted window size is 2 Acknowledgment is sent Acknowledgment is sent Acknowledgment is sent

CLIENT
Socket created port address is 9032 Connection is established between server and the client Enter the size of the data:3 Enter the data Hi Hw u Enter the window size:2 Ready to send Ack received Ready to send Ack received BYE.....

Вам также может понравиться