Index S. No. Name of Programs Page No. Remarks/ Signature 1. Write a C program to implement the parity generatorcode from a give bit pattern. 1 2. Write a C program to implement the parity checkerfrom a give code (data). 3 3. Write a program for implementation of bit stuffing and destuffing. 5 4. Write a C program for charactercount generator. 8 5. Write a C program for characterstuffing. 10 6. Write a C program to implement CRC. 13 7. Write a C program to implement the LZW technique. 16 8. Write a C program for a simple stream- oriented serveruses TCP port number 3456. 19 9. Write a C program for a simple stream- oriented client uses TCP port number 3456. 21 10. Write a C program for a simple datagram- oriented serveruses UDP port number 3456. 23 11. Write a C program for a simple datagram- oriented client uses UDP port number 3456. 25
1 Program 1 Write a C program to implement the parity generatorcode from a give bit pattern. #include<stdio.h> #include<conio.h> void main() { int a[20],n,i,flag=0; clrscr(); printf("Enter No. Of Bits: n"); scanf("%d", &n); printf("Enter Data"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { if(a[i]) flag++; } //printf("%d",flag); if(flag%2) a[n]=1; else a[n]=0; for(i=0;i<=n;i++) printf("%d",a[i]); getch(); }
2 Output:-
3 Program 2 Write a C program to implement the parity checker from a give code (data). #include<stdio.h> #include<conio.h> void main() { int a[20],n,i,flag=0; clrscr(); printf("Enter No. Of Bits: n"); scanf("%d", &n); printf("Enter Data"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<(n-1);i++) { if(a[i]) flag++; } if((flag%2) && (a[n-1]==1)) printf("data received is correct"); else if((!(flag%2)) && (a[n-1]==0)) printf("data received is correct"); else printf("data received has an error"); getch(); }
4 Output:-
5 Program 3 Write a program for implementation of bit stuffing and destuffing. #include <stdio.h> #include <conio.h> void main() { char num[11], i, j, k, cnt=0; clrscr(); printf("Enter the sequence of 10 digit binary numbers: "); for(i=0;i<10;i++) scanf("%c", &num[i]); printf("The 10 digit binary number you have entered is: "); for(i=0;i<10;i++) printf("%c", num[i]); printf("nAfter stuffing: "); i=0; while (i<10) { if(num[i]=='1') { i++; cnt++; if(cnt==5) { for(k=11;k>i;k--) num[k]=num[k-1]; num[i]=0; num[i]='0'; } } else { i++; cnt=0; } } for(i=0;i<11;i++) printf("%c", num[i]); printf("nAfter destuffing: "); i=0;
6 while (i<10) { if(num[i]=='1') { i++; cnt++; if(cnt==5) { for(k=i;k<11;k++) num[k]=num[k+1]; } } else { i++; cnt=0; } } for(i=0;i<10;i++) printf("%d", num[i]-48); getch(); }
7 Output:-
8 Program 4 Write a C program for charactercount generator. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[10][10]; int x[50],i,n; clrscr(); printf("Enter The No. Of Frames You Want To Send: n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("n Enter The Frame No. %dn",i); scanf("%s",&s[i]); } for(i=0;i<n;i++) { x[i]=strlen(s[i]); } printf("n Generated Codeis:nn"); for(i=0;i<n;i++) { printf("%d%s",x[i],s[i]); } getch(); }
9 Output:-
10 Program 5 Write a C program for characterstuffing. #include<stdio.h> #include<conio.h> void main() { int i,n,j=0,k,flag=0; char a[40]; clrscr(); printf("Input No. Of Characters: n"); scanf("%d",&n); printf("Input Charactersn"); for (i=0;i<n;i++) scanf("%s",&a[i]); for(i=0;i<(n-2);i++) { if((a[i]=='d') && (a[i+1]=='l') && (a[i+2]=='e')) flag++; } k=n+(3*flag); while(flag) { if((a[j]=='d')&&(a[j+1]=='l')&&(a[j+2]=='e')) { for(i=(k-1);i>=(j+3);i--) a[i]=a[i-3]; flag--; j=j+3; } j++; } for(i=(k+2);i>=3;i--) a[i]=a[i-3]; a[0]='s'; a[1]='t'; a[2]='f'; a[k+3]='s'; a[k+4]='t'; a[k+5]='x'; for(i=0;i<(k+6);i++)
11 printf("%c",a[i]); getch(); }
12 Output:-
13 Program 6 Write a C program to implement CRC. #include<stdio.h> #include<string.h> #include<conio.h> char xor(char,char); void main() { int i, j, l, k; char code[10], temp[15], temp2[5], div[5]; clrscr(); printf("n Enter the Frame:"); scanf("%s", code); strcpy(temp2,"0000"); strcat(code,temp2); strcpy(temp, code); strcpy(div,"10011"); i=0; l=4; while(l>0) { for(j=0;j<5;j++) { temp[i] = xor(temp[i],div[j]); i++; } i=0; while(temp[i]=='0') i++; l--; } printf("n The codeto be transmitted is =%s", code); for(j=i;j<(i+4);j++) printf("%c", temp[j]); getch(); } char xor(char a, char b) { if(a==b) return('0');
14 else return('1'); }
15 Output:-
16 Program 7 Write a C program to implement the LZW technique. #include<stdio.h> #include<conio.h> #include<string.h> struct code { int in; char st[20]; }; void main() { char a[20]={""}, s[20]={""}, s1[20]={""}, c[20]={""}; struct codeop[20], d[20]; int i=1,j=0,k,m=0,n=0,flag=0; clrscr(); printf("Enter the String: "); scanf("%s", &a); //Loading the Dictionary with all possiblesymbols d[n].in=i++; strcpy(d[n++].st,"0"); d[n].in=i++; strcpy(d[n++].st,"1"); //s = First character from the input string s[0]=a[m++]; s[1]='0'; while(a[m]!='0') { c[0]=a[m++]; //c = Next Character from the input string c[1]='0'; strcpy(s1,s);//s1=s strcat(s1,c);//s1=s+c flag=0; //Assuming that s1 doesntexist in the dictionary //Checking if s1 is present in the Dictionary for(k=0;k<n;k++) if(strcmp(s1,d[k].st)==0) { flag = 1; //s1 alredy exists in the dictionary break; }
17 if(flag==1) strcat(s,c);//s=s+c;increasing the length of the entry else { //Finding the INDEX of s for(k=0;k<n;k++) if(strcmp(s,d[k].st)==0) break; //Adding the entry n its correspondingindex to b sent in output strct op[j].in = d[k].in; strcpy(op[j++].st,s1); //Adding the Entry to the Dictionary d[n].in=i++; strcpy(d[n++].st,s1); //copying the ending character of the entry for next entry strcpy(s,c); } } //finding index of the last character for(k=0;k<n;k++) if(strcmpi(s,d[k].st)==0) break; //Adding the entry n its correspondingindex to b sent in output strct op[j].in = d[k].in; strcpy(op[j].st,s); printf("nDictionary"); printf("nEntry Indexn"); for(i=0;i<n;i++) printf("n%4s %2d",d[i].st,d[i].in); printf("nnTransmission codes"); printf("nEntry CodeTransmittedn"); for(i=0;i<=j;i++) printf("n%4s %2d",op[i].st,op[i].in); getch(); }
18 Output:-
19 Program 8 Write a C program for a simple stream-orientedserveruses TCP port number 3456. #include <stdio>; #include <stdlib>; #include <errno>; #include <string>; #include <sys/types>; #include <netinet/in>; #include <sys/socket>; #include <sys/wait>; #define MYPORT 3456 /* the port users will be connecting to */ #define BACKLOG 10 /* number of pending connections */ main() { int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */ struct sockaddr_inmy_addr; /* my address information */ struct sockaddr_intheir_addr; /* client's address info */ int sin_size; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_port= htons(MYPORT); my_addr.sin_addr.s_addr= INADDR_ANY; /* auto-fill with my IP */ bzero(&(my_addr.sin_zero), 8); /* zero the rest */ if (bind(sockfd,(structsockaddr*)&my_addr, sizeof(struct sockaddr))== -1) { perror("bind"); exit(1); } if (listen(sockfd, BACKLOG) == -1) { perror("listen"); exit(1); } while(1) /* main accept() loop */ {
20 sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd,(structsockaddr*)&their_addr,&sin_size)) == -1) { perror("accept"); continue; } printf("server: got connection from %sn", inet_ntoa(their_addr.sin_addr)); if (!fork()) /* this is the child process */ { if (send(new_fd, "Hello, world!n", 14, 0) == -1) perror("send"); close(new_fd); exit(0); } close(new_fd); /* parent doesn'tneed this */ while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */ } }
21 Program 9 Write a C program for a simple stream-orientedclient uses TCP port number 3456. #include <stdio>; #include <stdlib>; #include <errno>; #include <string>; #include <netdb>; #include <sys/types>; #include <netinet/in>; #include <sys/socket>; #define PORT 3490 /* the port client will be connecting to */ #define MAXDATASIZE 100 /* max number of bytes we can get at once */ int main(int argc, char *argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he; struct sockaddr_intheir_addr; /* client's address information */ if (argc != 2) { fprintf(stderr,"usage: client hostnamen"); exit(1); } if ((he=gethostbyname(argv[1])) == NULL) /* get the host info */ { herror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; their_addr.sin_port = htons(PORT); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero), 8); if (connect(sockfd,(structsockaddr*)&their_addr, sizeof(struct sockaddr))==1) {
22 perror("connect"); exit(1); } if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '0'; printf("Received: %s",buf); close(sockfd); return 0; }
23 Program 10 Write a C program for a simple datagram-orientedserveruses UDP port number 3456. #include <stdio>; #include <stdlib>; #include <errno>; #include <string>; #include <sys/types>; #include <netinet/in>; #include <sys/socket>; #include <sys/wait>; #define MYPORT 4950 /* the port users will be sending to */ #define MAXBUFLEN 100 main() { int sockfd; struct sockaddr_inmy_addr; /* my address information */ struct sockaddr_intheir_addr; /* client's address information */ int addr_len, numbytes; char buf[MAXBUFLEN]; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_port= htons(MYPORT); my_addr.sin_addr.s_addr= INADDR_ANY; bzero(&(my_addr.sin_zero), 8); if (bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr))== -1) { perror("bind"); exit(1); } addr_len = sizeof(struct sockaddr); if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, (struct sockaddr*)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1);
24 } printf("got packet from %sn",inet_ntoa(their_addr.sin_addr)); printf("packet is %d bytes longn",numbytes); buf[numbytes] = '0'; printf("packet contains "%s"n",buf); close(sockfd); }
25 Program 11 Write a C program for a simple datagram-orientedclient uses UDP port number 3456. #include <stdio>; #include <stdlib>; #include <errno>; #include <string>; #include <sys/types>; #include <netinet/in>; #include <netdb>; #include <sys/socket>; #include <sys/wait>; #define MYPORT 4950 /* the port users will be sending to */ int main(int argc, char *argv[]) { int sockfd; struct sockaddr_intheir_addr; /* client's address information */ struct hostent *he; int numbytes; if (argc != 3) { fprintf(stderr,"usage: talker hostname messagen"); exit(1); } if ((he=gethostbyname(argv[1])) == NULL) /* get the host info */ { herror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; their_addr.sin_port = htons(MYPORT); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero), 8); if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr*)&their_addr, sizeof(struct sockaddr)))== -1)
26 { perror("sendto"); exit(1); } printf("sent %d bytes to %sn",numbytes,inet_ntoa(their_addr.sin_addr)); close(sockfd); return 0; }

Computer Networks Lab File

  • 1.
    Index S. No. Nameof Programs Page No. Remarks/ Signature 1. Write a C program to implement the parity generatorcode from a give bit pattern. 1 2. Write a C program to implement the parity checkerfrom a give code (data). 3 3. Write a program for implementation of bit stuffing and destuffing. 5 4. Write a C program for charactercount generator. 8 5. Write a C program for characterstuffing. 10 6. Write a C program to implement CRC. 13 7. Write a C program to implement the LZW technique. 16 8. Write a C program for a simple stream- oriented serveruses TCP port number 3456. 19 9. Write a C program for a simple stream- oriented client uses TCP port number 3456. 21 10. Write a C program for a simple datagram- oriented serveruses UDP port number 3456. 23 11. Write a C program for a simple datagram- oriented client uses UDP port number 3456. 25
  • 2.
    1 Program 1 Write aC program to implement the parity generatorcode from a give bit pattern. #include<stdio.h> #include<conio.h> void main() { int a[20],n,i,flag=0; clrscr(); printf("Enter No. Of Bits: n"); scanf("%d", &n); printf("Enter Data"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { if(a[i]) flag++; } //printf("%d",flag); if(flag%2) a[n]=1; else a[n]=0; for(i=0;i<=n;i++) printf("%d",a[i]); getch(); }
  • 3.
  • 4.
    3 Program 2 Write aC program to implement the parity checker from a give code (data). #include<stdio.h> #include<conio.h> void main() { int a[20],n,i,flag=0; clrscr(); printf("Enter No. Of Bits: n"); scanf("%d", &n); printf("Enter Data"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<(n-1);i++) { if(a[i]) flag++; } if((flag%2) && (a[n-1]==1)) printf("data received is correct"); else if((!(flag%2)) && (a[n-1]==0)) printf("data received is correct"); else printf("data received has an error"); getch(); }
  • 5.
  • 6.
    5 Program 3 Write aprogram for implementation of bit stuffing and destuffing. #include <stdio.h> #include <conio.h> void main() { char num[11], i, j, k, cnt=0; clrscr(); printf("Enter the sequence of 10 digit binary numbers: "); for(i=0;i<10;i++) scanf("%c", &num[i]); printf("The 10 digit binary number you have entered is: "); for(i=0;i<10;i++) printf("%c", num[i]); printf("nAfter stuffing: "); i=0; while (i<10) { if(num[i]=='1') { i++; cnt++; if(cnt==5) { for(k=11;k>i;k--) num[k]=num[k-1]; num[i]=0; num[i]='0'; } } else { i++; cnt=0; } } for(i=0;i<11;i++) printf("%c", num[i]); printf("nAfter destuffing: "); i=0;
  • 7.
  • 8.
  • 9.
    8 Program 4 Write aC program for charactercount generator. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[10][10]; int x[50],i,n; clrscr(); printf("Enter The No. Of Frames You Want To Send: n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("n Enter The Frame No. %dn",i); scanf("%s",&s[i]); } for(i=0;i<n;i++) { x[i]=strlen(s[i]); } printf("n Generated Codeis:nn"); for(i=0;i<n;i++) { printf("%d%s",x[i],s[i]); } getch(); }
  • 10.
  • 11.
    10 Program 5 Write aC program for characterstuffing. #include<stdio.h> #include<conio.h> void main() { int i,n,j=0,k,flag=0; char a[40]; clrscr(); printf("Input No. Of Characters: n"); scanf("%d",&n); printf("Input Charactersn"); for (i=0;i<n;i++) scanf("%s",&a[i]); for(i=0;i<(n-2);i++) { if((a[i]=='d') && (a[i+1]=='l') && (a[i+2]=='e')) flag++; } k=n+(3*flag); while(flag) { if((a[j]=='d')&&(a[j+1]=='l')&&(a[j+2]=='e')) { for(i=(k-1);i>=(j+3);i--) a[i]=a[i-3]; flag--; j=j+3; } j++; } for(i=(k+2);i>=3;i--) a[i]=a[i-3]; a[0]='s'; a[1]='t'; a[2]='f'; a[k+3]='s'; a[k+4]='t'; a[k+5]='x'; for(i=0;i<(k+6);i++)
  • 12.
  • 13.
  • 14.
    13 Program 6 Write aC program to implement CRC. #include<stdio.h> #include<string.h> #include<conio.h> char xor(char,char); void main() { int i, j, l, k; char code[10], temp[15], temp2[5], div[5]; clrscr(); printf("n Enter the Frame:"); scanf("%s", code); strcpy(temp2,"0000"); strcat(code,temp2); strcpy(temp, code); strcpy(div,"10011"); i=0; l=4; while(l>0) { for(j=0;j<5;j++) { temp[i] = xor(temp[i],div[j]); i++; } i=0; while(temp[i]=='0') i++; l--; } printf("n The codeto be transmitted is =%s", code); for(j=i;j<(i+4);j++) printf("%c", temp[j]); getch(); } char xor(char a, char b) { if(a==b) return('0');
  • 15.
  • 16.
  • 17.
    16 Program 7 Write aC program to implement the LZW technique. #include<stdio.h> #include<conio.h> #include<string.h> struct code { int in; char st[20]; }; void main() { char a[20]={""}, s[20]={""}, s1[20]={""}, c[20]={""}; struct codeop[20], d[20]; int i=1,j=0,k,m=0,n=0,flag=0; clrscr(); printf("Enter the String: "); scanf("%s", &a); //Loading the Dictionary with all possiblesymbols d[n].in=i++; strcpy(d[n++].st,"0"); d[n].in=i++; strcpy(d[n++].st,"1"); //s = First character from the input string s[0]=a[m++]; s[1]='0'; while(a[m]!='0') { c[0]=a[m++]; //c = Next Character from the input string c[1]='0'; strcpy(s1,s);//s1=s strcat(s1,c);//s1=s+c flag=0; //Assuming that s1 doesntexist in the dictionary //Checking if s1 is present in the Dictionary for(k=0;k<n;k++) if(strcmp(s1,d[k].st)==0) { flag = 1; //s1 alredy exists in the dictionary break; }
  • 18.
    17 if(flag==1) strcat(s,c);//s=s+c;increasing the lengthof the entry else { //Finding the INDEX of s for(k=0;k<n;k++) if(strcmp(s,d[k].st)==0) break; //Adding the entry n its correspondingindex to b sent in output strct op[j].in = d[k].in; strcpy(op[j++].st,s1); //Adding the Entry to the Dictionary d[n].in=i++; strcpy(d[n++].st,s1); //copying the ending character of the entry for next entry strcpy(s,c); } } //finding index of the last character for(k=0;k<n;k++) if(strcmpi(s,d[k].st)==0) break; //Adding the entry n its correspondingindex to b sent in output strct op[j].in = d[k].in; strcpy(op[j].st,s); printf("nDictionary"); printf("nEntry Indexn"); for(i=0;i<n;i++) printf("n%4s %2d",d[i].st,d[i].in); printf("nnTransmission codes"); printf("nEntry CodeTransmittedn"); for(i=0;i<=j;i++) printf("n%4s %2d",op[i].st,op[i].in); getch(); }
  • 19.
  • 20.
    19 Program 8 Write aC program for a simple stream-orientedserveruses TCP port number 3456. #include <stdio>; #include <stdlib>; #include <errno>; #include <string>; #include <sys/types>; #include <netinet/in>; #include <sys/socket>; #include <sys/wait>; #define MYPORT 3456 /* the port users will be connecting to */ #define BACKLOG 10 /* number of pending connections */ main() { int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */ struct sockaddr_inmy_addr; /* my address information */ struct sockaddr_intheir_addr; /* client's address info */ int sin_size; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_port= htons(MYPORT); my_addr.sin_addr.s_addr= INADDR_ANY; /* auto-fill with my IP */ bzero(&(my_addr.sin_zero), 8); /* zero the rest */ if (bind(sockfd,(structsockaddr*)&my_addr, sizeof(struct sockaddr))== -1) { perror("bind"); exit(1); } if (listen(sockfd, BACKLOG) == -1) { perror("listen"); exit(1); } while(1) /* main accept() loop */ {
  • 21.
    20 sin_size = sizeof(structsockaddr_in); if ((new_fd = accept(sockfd,(structsockaddr*)&their_addr,&sin_size)) == -1) { perror("accept"); continue; } printf("server: got connection from %sn", inet_ntoa(their_addr.sin_addr)); if (!fork()) /* this is the child process */ { if (send(new_fd, "Hello, world!n", 14, 0) == -1) perror("send"); close(new_fd); exit(0); } close(new_fd); /* parent doesn'tneed this */ while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */ } }
  • 22.
    21 Program 9 Write aC program for a simple stream-orientedclient uses TCP port number 3456. #include <stdio>; #include <stdlib>; #include <errno>; #include <string>; #include <netdb>; #include <sys/types>; #include <netinet/in>; #include <sys/socket>; #define PORT 3490 /* the port client will be connecting to */ #define MAXDATASIZE 100 /* max number of bytes we can get at once */ int main(int argc, char *argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he; struct sockaddr_intheir_addr; /* client's address information */ if (argc != 2) { fprintf(stderr,"usage: client hostnamen"); exit(1); } if ((he=gethostbyname(argv[1])) == NULL) /* get the host info */ { herror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; their_addr.sin_port = htons(PORT); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero), 8); if (connect(sockfd,(structsockaddr*)&their_addr, sizeof(struct sockaddr))==1) {
  • 23.
    22 perror("connect"); exit(1); } if ((numbytes=recv(sockfd, buf,MAXDATASIZE, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '0'; printf("Received: %s",buf); close(sockfd); return 0; }
  • 24.
    23 Program 10 Write aC program for a simple datagram-orientedserveruses UDP port number 3456. #include <stdio>; #include <stdlib>; #include <errno>; #include <string>; #include <sys/types>; #include <netinet/in>; #include <sys/socket>; #include <sys/wait>; #define MYPORT 4950 /* the port users will be sending to */ #define MAXBUFLEN 100 main() { int sockfd; struct sockaddr_inmy_addr; /* my address information */ struct sockaddr_intheir_addr; /* client's address information */ int addr_len, numbytes; char buf[MAXBUFLEN]; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_port= htons(MYPORT); my_addr.sin_addr.s_addr= INADDR_ANY; bzero(&(my_addr.sin_zero), 8); if (bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr))== -1) { perror("bind"); exit(1); } addr_len = sizeof(struct sockaddr); if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, (struct sockaddr*)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1);
  • 25.
    24 } printf("got packet from%sn",inet_ntoa(their_addr.sin_addr)); printf("packet is %d bytes longn",numbytes); buf[numbytes] = '0'; printf("packet contains "%s"n",buf); close(sockfd); }
  • 26.
    25 Program 11 Write aC program for a simple datagram-orientedclient uses UDP port number 3456. #include <stdio>; #include <stdlib>; #include <errno>; #include <string>; #include <sys/types>; #include <netinet/in>; #include <netdb>; #include <sys/socket>; #include <sys/wait>; #define MYPORT 4950 /* the port users will be sending to */ int main(int argc, char *argv[]) { int sockfd; struct sockaddr_intheir_addr; /* client's address information */ struct hostent *he; int numbytes; if (argc != 3) { fprintf(stderr,"usage: talker hostname messagen"); exit(1); } if ((he=gethostbyname(argv[1])) == NULL) /* get the host info */ { herror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; their_addr.sin_port = htons(MYPORT); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero), 8); if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr*)&their_addr, sizeof(struct sockaddr)))== -1)
  • 27.
    26 { perror("sendto"); exit(1); } printf("sent %d bytesto %sn",numbytes,inet_ntoa(their_addr.sin_addr)); close(sockfd); return 0; }