Professional Documents
Culture Documents
All Exercises of CN
All Exercises of CN
CONTENTS: 1.SOCKET CREATION 2.BINDING THE SOCKET 3.LISTEN SYSTEM CALL 4.ACCEPT SYSTEM CALL 5.CLIENT/SERVER CHAT APPLICATION USING TCP 6. TCP ECHO CLIENT/SERVER 7. CLIENT/SERVER CHAT APPLICATION USING UDP . UDP ECHO CLIENT/SERVER !.BIT STU""ING AND DE#BIT STU""ING 1$.CYCLIC REDUNANCY CHECK 11.SELECTIVE REPEAT PROTOCOL 12.GO BACK#N PROTOCOL 13.STOP AND %AIT PROTOCOL 14.DNS PROTOCOL 15.BORDER GATE%AY PROTOCOL 16.OSP" PROTOCOL 17.DISTANCE VECTOR ROUTING 1 .MULTICASTING 1!.DATE AND TIME 2$.REMOTE COMMAND E&ECUTION 21.ARP PROTOCOL
3
OUTPUT:
9cseb332:&nsat;( <=> cc socket.c 9cseb332:&nsat;( <=> ./a.out socket1 is created and socket1 file descriptor 6alue is 3 socket2 is created and socket2 file descriptor 6alue is ?
#include<arpa/inet.h> #include<netinet/in.h> #define @+.)'+ ;AA0 int main() { int sockfd i"@+.)'+! struct sockaddr%in myaddr! sockfd"socket(#$%&'() *+,-%*).(#/ 0)! if(sockfd""31) { printf(4socket creation error4)! 5 else { printf(4socket is created8n4)! 5 //bBero(Cmyaddr siBeof (myaddr))! myaddr.sin%family"#$%&'()! myaddr.sin%port"htons(@+.)'+)! myaddr.sin%addr.s%addr"&'#11.%#'D! memset(C(myaddr.sin%Bero) E80E F)! if(bind(sockfd (struct sockaddr G)Cmyaddr siBeof(struct sockaddr))H"31) { printf(4socket is binded at point 7d8n4 i)! 5 else { printf(4bindinI error4)! 5 5
OUTPUT:
9cseb332:&nsat;( <=> cc neJ.c 9cseb332:&nsat;( <=> ./a.out socket is created socket is binded at point ;AA0
#include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #define @+.) 3KA0 #define L#,-M+2 12 main() { int fd! struct sockaddr%in ser6er! struct sockaddr%in client! int sin%siBe! int N! if((fd"socket(#$%&'() *+,-%*).(#/ 0))""31) { printf(4socket error8n4)! eNit(31)! 5 ser6er.sin%family"#$%&'()! ser6er.sin%port"htons(@+.))! ser6er.sin%addr.s%addr"&'#11.%#'D! bBero(C(ser6er.sin%Bero) F)! if(bind(fd (struct sockaddrG)Cser6er siBeof(struct sockaddr))""31) { printf(4bind error 8n4)! eNit(31)! 5 N"listen(fd L#,-M+2)! if(N""31) { printf(4listen error4)! eNit(31)! 5 else { printf(4ser6er is in listeninI mode8n4)! 5 close(fd)! 5
OUTPUT:
9cseb332:&nsat;( <=> cc listen.c 9cseb332:&nsat;( <=> ./a.out ser6er is in listeninI mode
PROGRAM:
#include<arpa/inet.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #define @+.) ?K2O #define L#,-M+2 F main() { int fd fd2! struct sockaddr%in ser6er! struct sockaddr%in client! int sin%siBe! if((fd"socket(#$%&'() *+,-%*).(#/ 0))""31) { printf(4socket() error8n4)! //eNit(31)! 5 ser6er.sin%family"#$%&'()! ser6er.sin%port"htons(@+.))! ser6er.sin%addr.s%addr"&'#11.%#'D! bBero(C(ser6er.sin%Bero) F)! if(bind(fd (struct sockaddrG)Cser6er siBeof(struct sockaddr))H"31) { printf(4bind()error8n4)! //eNit(31)! 5 else if(listen(fd L#,-M+2)H"31) { printf(4listen()error8n4)! //eNit(31)! 5 printf(4ser6er is in accept mode 8n 4)! //Jhile(1) //{ sin%siBe"siBeof( struct sockaddr)! if((fd2 " accept(fd (struct sockaddr G)Cclient Csin%siBe))H"31){ printf(4accept() error8n4)! //eNit(31)! 5 else { printf(4 *er6er is in accept mode4)! printf(4 Dou Iot a connection from 7s8n4 inet%ntoa(client.sin%addr))! //close(fd2)! 5 //close(fd2)!
OUTPUT:
9cseb332:&nsat;( <=> cc accccept.c 9cseb332:&nsat;( <=> ./a.out bind()error ser6er is in accept mode *er6er is in accept mode Dou Iot a connection from 20.1?K.1FK.0
{ printf(48n8t8terrror in listen4)! //return 0! 5 si"siBeof(cli)! if((sdl"accept(sd (struct sockaddrG)Ccli Csi))<0) { printf(48n8t8t errore in accept4)! //return 0! 5 do { rec6(sdl CmsI ;0 0)! printf(48n8t client messaIe recei6edR7s8n4 msI)! printf(48n8t enter the ser6er strinI4)! scanf(47s4 buf)! send(sdl buf ;0 0)! //rec6(sdl CmsI ;0 0)! //printf(48n8t client messaIe recei6edR7s8n4 msI)! 5Jhile(strcmp(buf 4eNit4)H"0)! close(sd)! 5
CLIENT PROGRAM:
#include<stdio.h> #include<netdb.h> #include<strinI.h> #include<sys/types.h> #include<netinet/in.h> #include<unistd.h> #include<stdlib.h> #include<sys/socket.h> #include<arpa/inet.h> #define /#P1#)#*&Q( 100 main() { int sd i"0 len! struct sockaddr%in ser! char msI9;0= rmsI9;0=! char buf9/#P1#)#*&Q(=! if((sd"socket(#$%&'() *+,-%*).(#/ 0))<0) { printf(48n8terror in socket4)! return 0! 5 A
bBero(Cser siBeof(ser))! ser.sin%family"#$%&'()! ser.sin%port"htons(210O)! ser.sin%addr.s%addr"htonl(&'#11.%#'D)! if(connect(sd (struct sockaddr G) Cser siBeof(ser))<0) { printf(48n8t error in connect4)! //eNit(31)! 5 do { printf(48n8t enter the strinIR8n4)! scanf(47s4 buf)! send(sd buf ;0 0)! rec6(sd rmsI ;0 0)! printf(48n8t8t ser6er messaIeR7s4 rmsI)! 5Jhile(strcmp(buf 4eNit4)H"0)! close(sd)! 5
OUTPUT: SERVER:
9cseb332:&nsat;( <=> cc tcpser.c 9cseb332:&nsat;( <=> ./a.out client messaIe recei6edRhi enter the ser6er strinIRhelllooo client messaIe recei6edRIudmorn enter the ser6er strinIRyes IudmrnI client messaIe recei6edRbye enter the ser6er strinIRbye client messaIe recei6edReNit enter the ser6er strinIReNit
CLIENT:
9cseb332:&nsat;( <=> ./a.out enter the strinIRhi 10
ser6er messaIeRhelllooo enter the strinIRIudmorn ser6er messaIeRyes IudmrnI enter the strinIRbye ser6er messaIeRbye enter the strinIReNit ser6er messaIeReNit
//return 0! 5 si"siBeof(cli)! if((sdl"accept(sd (struct sockaddrG)Ccli Csi))<0) { printf(48n8t8t errore in accept4)! //return 0! 5 do { rec6(sdl msI ;0 0)! //printf(48n8t client messaIe recei6edR7s8n4 msI)! //printf(48n8t enter the ser6er strinI4)! //scanf(47s4 buf)! send(sdl msI ;0 0)! //rec6(sdl CmsI ;0 0)! //printf(48n8t client messaIe recei6edR7s8n4 msI)! 5Jhile(strcmp(buf 4eNit4)H"0)! close(sd)! 5
CLIENT PROGRAM:
#include<strinI.h> #include<sys/types.h> #include<netinet/in.h> #include<unistd.h> #include<stdlib.h> #include<sys/socket.h> #include<arpa/inet.h> #define /#P1#)#*&Q( 100 main() { int sd i"0 len! struct sockaddr%in ser! char msI9;0= rmsI9;0=! char buf9/#P1#)#*&Q(=! if((sd"socket(#$%&'() *+,-%*).(#/ 0))<0) { printf(48n8terror in socket4)! return 0! 5 bBero(Cser siBeof(ser))! ser.sin%family"#$%&'()! 12
ser.sin%port"htons(2;?A)! ser.sin%addr.s%addr"htonl(&'#11.%#'D)! if(connect(sd (struct sockaddr G) Cser siBeof(ser))<0) { printf(48n8t error in connect4)! //eNit(31)! 5 do { printf(48n8t enter the strinIR8n4)! scanf(47s4 buf)! send(sd buf ;0 0)! rec6(sd rmsI ;0 0)! printf(48n8t8t ser6er messaIe recei6ed from client beforeR7s4 rmsI)! 5Jhile(strcmp(buf 4eNit4)H"0)! close(sd)! 5
OUTPUT: SERVER:
9cseb332:&nsat;( <=> cc ssseeerrr.c 9cseb332:&nsat;( <=> ./a.out 9cseb332:&nsat;( <=>
CLIENT:
9cseb332:&nsat;( <=> cc cccllliii.c 9cseb332:&nsat;( <=> ./a.out enter the strinIRhi ser6er messaIe recei6ed from client beforeRhi enter the strinIRhello ser6er messaIe recei6ed from client beforeRhello enter the strinIRiEmfine ser6er messaIe recei6ed from client beforeRiEm fine enter the strinIReNit ser6er messaIe recei6ed from client beforeReNit 9cseb332:&nsat;(<=>
13
CLIENT PROGRAM:
#include<stdio.h> #include<strinI.h> #include<sys/stat.h> #include<sys/types.h> 1?
#include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> main() { struct sockaddr%in ser6er client! int s n! char b19100= b29100=! s"socket(#$%&'() *+,-%12.#/ 0)! ser6er.sin%family"#$%&'()! ser6er.sin%port"htons(3FO?)! ser6er.sin%addr.s%addr"htonl(&'#11.%#'D)! printf(48n,lient ready....8n4)! n"siBeof(ser6er)! do { printf(48nenter the strinIR4)! scanf(47s4 b2)! sendto(s b2 siBeof(b2) 0 (struct sockaddr G) Cser6er n)! //if(strcmp(b2 4end4)""0) // break! rec6from(s b1 siBeof(b1) 0 'SMM 'SMM)! printf(48n*er6er messaIe recei6edR7s4 b1)! 5Jhile(strcmp(b1 4eNit4)H"0)! 5
OUTPUT: SERVER:
9cseb332:&nsat;( <=> cc udpchatser.c 9cseb332:&nsat;( <=> ./a.out (cho *er6er ready JaitinI for client.... client messaIe recei6ed isRhello enter the ser6er strinIRhi client messaIe recei6ed isReNit enter the ser6er strinIReNit 9cseb332:&nsat;( <=>
CLIENT:
9cseb332:&nsat;( <=> cc udpchatcliii.c 9cseb332:&nsat;( <=> ./a.out ,lient ready....
1;
enter the strinIRhello *er6er messaIe recei6edRhi enter the strinIReNit *er6er messaIe recei6edReNit 9cseb332:&nsat;( <=>
CLIENT PROGRAM:
#include<stdio.h> #include<strinI.h> 1K
#include<sys/stat.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> main() { struct sockaddr%in ser6er client! int s n! char b19100= b29100=! s"socket(#$%&'() *+,-%12.#/ 0)! ser6er.sin%family"#$%&'()! ser6er.sin%port"htons(;K20)! ser6er.sin%addr.s%addr"htonl(&'#11.%#'D)! printf(48n,lient ready....8n4)! n"siBeof(ser6er)! do { printf(48n,lientR4)! Iets(b2)! sendto(s b2 siBeof(b2) 0 (struct sockaddr G) Cser6er n)! rec6from(s b1 siBeof(b1) 0 'SMM 'SMM)! printf(48n*er6erR7s4 b1)! 5Jhile(strcmp(b2 TendT)H"0)! 5
OUTPUT: SERVER:
9cseb332:&nsat;( <=> cc udpechoser.c 9cseb332:&nsat;( <=> ./a.out (cho *er6er ready JaitinI for client....
CLIENT:
9cseb332:&nsat;( <=> cc udpechocli.c 9cseb332:&nsat;( <=> ./a.out ,lient ready.... ,lientRhi *er6erRhi 1O
1F
OUTPUT:
9cseb332:&nsat;( <=> cat>> file1.dat 1101111111110001111111100011111111 9cseb332:&nsat;( <=> cc bitstuffinI.c 9cseb332:&nsat;( <=> ./a.out 9cseb332:&nsat;( <=> cat file1.dat 1101111111110001111111100011111111 9cseb332:&nsat;( <=> cat file2.dat 1101111101111000111110111000111110111
DE#BITSTU""ING: PROGRAM:
#include<stdio.h> int main() { $&M( Gf1 Gf2! char c! int count! count"0! f1"fopen(4file3.dat4 4J4)! f2"fopen(4file2.dat4 4r4)! Jhile(Hfeof(f2)) { c"Ietc(f2)! if(c""E1E) countUU! else count"0! if(count"";) { fputc(c f1)! c"fIetc(f2)! count"0! 5 else fputc(c f1)! 5 fcloseall()! return 0! 5
1A
OUTPUT:
9cseb332:&nsat;( <=> cc debitstuffinI.c 9cseb332:&nsat;( <=> ./a.out 9cseb332:&nsat;( <=> cat file2.dat 1101111101111000111110111000111110111 9cseb332:&nsat;( <=> cat file3.dat 1101111111110001111111100011111111
for(i"0!i< n!UUi) W9i=" arr9i=! for(i"0!i< n!UUi) { if(arr9i=""0) { for(V"i!V< IUi!UUV) arr9V= " arr9V=X0! 5 else { arr9i= " arr9i=XIen90=! arr9iU1="arr9iU1=XIen91=! arr9iU2="arr9iU2=XIen92=! arr9iU3="arr9iU3=XIen93=! 5 5 printf(48n8t)he ,., is R4)! for(i"n!i < a!UUi) printf(47d4 arr9i=)! s"nUa! for(i"n!i< s!iUU) W9i="arr9i=! printf(48n4)! for(i"0!i< a!iUU) printf(47d4 W9i=)! 5
OUTPUT:
9cseb332:&nsat;( <=> cc crc?.c 9cseb332:&nsat;( <=> ./a.out )ransmitter sideR (nter no. of data bitsRF (nter dataR 1 0 1 0 1 0 1 0 (nter siBe of IeneratorR? (nter IeneratorR 21
1 1 0 1 )he Ienerator matriNR1101 )he appended matriN isR10101010000 )he ,., is R110 10101010110 9cseb332:&nsat;( <=> cc crc?.c 9cseb332:&nsat;( <=> ./a.out )ransmitter sideR (nter no. of data bitsR11 (nter dataR 1 0 1 0 1 0 1 0 1 1 0 (nter siBe of IeneratorR? (nter IeneratorR 1 1 0 1 )he Ienerator matriNR1101 )he appended matriN isR10101010110000 )he ,., is R000 10101010110000
char str920= frame920= temp920= ack920=! struct sockaddr%in saddr caddr! printf(4(nter the port address4)! scanf(47d4 Csport)! std " socket(#$%&'() *+,-%*).(#/ 0)! if(std<0) perror(4(rror4)! bBero(Csaddr siBeof(saddr))! saddr.sin%family " #$%&'()! saddr.sin%addr.s%addr " htonl(&'#11.%#'D)! saddr.sin%port " htons(sport)! lfd " bind(std (struct sockaddr G)Csaddr siBeof(saddr))! if(lfd) perror(4Lind (rror4)! listen(std ;)! len " siBeof(Ccaddr)! lfd " accept(std (struct sockaddr G)Ccaddr Clen)! printf(4(nter the teNtR4)! scanf(47s4 str)!i " 0! Jhile(i<strlen(str)) { memset(frame 0 20)! strncpy(frame strUi *&Q()! printf(48n)ransmittinI framesR4)! len " strlen(frame)! for(V"0! V<len! VUU) { printf(47d4 iUV)! sprintf(temp 47d4 iUV)! strcat(frame temp)! 5 Jrite(lfd frame siBeof(frame))! read(lfd ack 20)! sscanf(ack 47d4 Cstatus)! if(status "" 31) printf(48n)ransmission successful4)! else { printf(4.ecei6ed error inR 7d4 status)! printf(48n.etransmittinI frame4)! frame90= " str9status=! frame91= " E80E!Jrite(lfd frame siBeof(frame))! 5 i " i U *&Q(! 5 Jrite(lfd 4(Nit4 siBeof(4(Nit4))! printf(48n(NittinIH8n4)! sleep(2)! close(lfd)! 23
close(std)! 5
CLIENT PROGRAM:
#include<stdio.h> #include<strinI.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<arpa/inet.h> main() { int std lfd len choice cport! char str920= str1920= err920=! struct sockaddr%in saddr caddr! printf(4(nter the port addressR4)! scanf(47d4 Ccport)! std " socket(#$%&'() *+,-%*).(#/ 0)! if(std<0) perror(4(rror4)! bBero(Csaddr siBeof(saddr))! saddr.sin%family " #$%&'()! inet%pton(#$%&'() 412O.0.0.14 Csaddr.sin%addr)! saddr.sin%port " htons(cport)! connect(std (struct sockaddr G)Csaddr siBeof(saddr))! for(!!) { read(std str 20)! if(strcmp(str 4(Nit4) "" 0) { printf(4(NittinIH8n4)! break! 5 printf(4.ecei6edR 7s8n(rrorY(1 3 D(* or 0 3 '+)R 4 str)! scanf(47d4 Cchoice)! if(choice "" 0)Jrite(std 4314 siBeof(4314))! else{printf(4(nter the seWuence no of the frame Jhere error has occured4)! scanf(47s4 err)! Jrite(std err siBeof(err))! read(std str 20)! printf(4.ecei6ed the transmitted frameR 7s8n4 str)! 5 5 close(std)! 2?
CLIENT SIDE:
9cseb332:&nsat;( <=> cc selcli.c 9cseb332:&nsat;( <=> ./a.out (nter the port addressR?;FA .ecei6edR hell0123 (rrorY(1 3 D(* or 0 3 '+)R 1 (nter the seWuence no of the frame Jhere error has occurredR2 .ecei6ed the transmitted frameR l .ecei6edR o? (rrorY(1 3 D(* or 0 3 '+)R 1 (nter the seWuence no of the frame Jhere error has occurredR0 .ecei6ed the transmitted frameR h (NittinIH
#define *&Q( ? int main() { int sfd lfd len i V status! char str920= frame920= temp920= ack920=! struct sockaddr%in saddr caddr! sfd"socket(#$%&'() *+,-%*).(#/ 0)! if(sfd<0) perror(4(rror4)! bBero(Csaddr siBeof(saddr))! saddr.sin%family"#$%&'()! saddr.sin%addr.s%addr"htonl(&'#11.%#'D)! saddr.sin%port"htons(;?K;)! if(bind(sfd (struct sockaddrG)Csaddr siBeof(saddr))<0) perror(4Lind (rror4)! listen(sfd ;)! len"siBeof(Ccaddr)! lfd"accept(sfd (struct sockaddrG)Ccaddr Clen)! printf(4 (nter the teNt R 8n4)! scanf(47s4 str)! i"0! Jhile(i<strlen(str)) { memset(frame 0 20)! strncpy(frame strUi *&Q()! printf(4 )ransmittinI $rames. 4)! len"strlen(frame)! for(V"0!V<len!VUU) { printf(47d4 iUV)! sprintf(temp 47d4 iUV)! strcat(frame temp)! 5 printf(48n4)! Jrite(lfd frame siBeof(frame))! read(lfd ack 20)! sscanf(ack 47d4 Cstatus)! if(status""31) printf(4 )ransmission is successful. 8n4)! else { printf(4 .ecei6ed error in 7d 8n8n4 status)! printf(48n8n .etransmittinI $rame. 4)! for(V"0!!) { frame9V="str9VUstatus=! printf(47d4 VUstatus)! VUU! 2K
if((VUstatus)7?""0) break! 5 printf(48n4)! frame9V="E80E! len"strlen(frame)! for(V"0!V<len!VUU) { sprintf(temp 47d4 VUstatus)! strcat(frame temp)! 5 Jrite(lfd frame siBeof(frame))! 5 iU"*&Q(! 5 Jrite(lfd 4eNit4 siBeof(4eNit4))! printf(4(NitinI8n4)! sleep(2)! close(lfd)! close(sfd)! 5
CLIENT PROGRAM:
#include<stdio.h> #include<strinI.h> #include<stdlib.h> #include<sys/socket.h> #include<sys/types.h> { int sfd lfd len choice! char str920= str1920= err920=! if(sfd<0) perror(4$d(rror4)! bBero(Csaddr siBeof(saddr))! saddr.sin%family"#$%&'()! saddr.sin%addr.s%addr"&'#11.%#'D! saddr.sin%port"htons(;?K;)! connect(sfd (struct sockaddrG)Csaddr siBeof(saddr))! for(!!) { read(sfd str 20)! if(Hstrcmp(str 4eNit4)) { printf(4(NitinI8n4)! break! 2O
5 printf(48n8n.ecei6ed7s8n8n1.1o u Jant to report an error(13Des 03 'o)4 str)! scanf(47d4 Cchoice)! if(Hchoice) Jrite(sfd 4314 siBeof(4314))! else { printf(4(nter the seWuence no of the frame Jhere error has occured8n4)! scanf(47s4 err)! Jrite(sfd err siBeof(err))! read(sfd str 20)! printf(48n8n.ecei6ed the re3transmitted frames7s8n8n4 str)! 5 5 5
OUTPUT:
SERVER OUTPUT:
9cseb332:&nsat;( <=> cc Ioser.c 9cseb332:&nsat;( <=> ./a.out (nter the teNt R Zellohai )ransmittinI $rames. 0123 .ecei6ed error in 1 .etransmittinI $rame. 123 )ransmittinI $rames. ?;KO .ecei6ed error in ; .etransmittinI $rame. ;KO (NitinIH
CLIENT OUTPUT:
9cseb332:&nsat;( <=> cc Iocli.c 9cseb332:&nsat;( <=> ./a.out .ecei6edhell0123
2F
1.1o u Jant to report an error(13Des 03'o)R1 (nter the seWuence no of the frame Jhere error has occurredR 1 .ecei6ed the re3transmitted framesRell123 .ecei6edRohai?;KO 1.1o u Jant to report an error(13Des 03'o)R1 (nter the seWuence no of the frame Jhere error has occurredR ; .ecei6ed the re3transmitted framesRhai;KO (NitinIH
return 0! 5 i"siBeof(cli)! listen(sd ;)! nsd " accept(sd ((struct sockaddr G)Ccli) Ci)! if(nsd""31) { printf(48ncheck the description parameter8n4)! return 0! 5 printf(48n,onnection accepted.4)! Jhile(count<;) { phR printf(48n*endinI $.#/( 7d to the .ecei6er...8n4 pre6%frame)! snprintf(buff siBeof(buff) 47d4 pre6%frame)! send(nsd buff 30 0)! r " rec6(nsd ack ; 0)! if(strcmp(ack 4ack4)""0 [[ strcmp(ack 4#,-4)""0) { countUU! if(pre6%frame""0) pre6%frame"1! else pre6%frame " 0! 5 else if(strcmp(ack 4nak4)""0 [[ strcmp(ack 4'#-4)""0) { printf(48n '#-R *o aIain sendinI the @re6ious frame...8n4)! Ioto ph! 5 5 printf(48n Lye..4)! send(nsd 4(+$4 ? 0)! close(sd)! close(nsd)! return 0! 5
CLIENT PROGRAM:
#include 4stdio.h4 #include 4sys/types.h4 #include 4netinet/in.h4 #include 4strinI.h4 #include 4sys/socket.h4 #include 4stdlib.h4 #include 4unistd.h4 int sd con port i! char content930= ack93=! struct sockaddr%in cli! 30
if((sd"socket(#$%&'() *+,-%*).(#/ &@@.+)+%),@))""31) { printf(48n *ocket problem4)! return 0! 5 bBero((charG)Ccli siBeof(cli))! cli.sin%family " #$%&'()! printf(4(')(. @+.) '+4)! scanf(47d4 Cport)! cli.sin%port"htons(port)! cli.sin%addr.s%addr"htonl(&'#11.%#'D)! con"connect(sd (struct sockaddrG)Ccli siBeof(cli))! if(con""31) { printf(48n connection error4)! return 0! 5 i"rec6(sd content 30 0)! Jhile(strcmp(content 4(+$4) H" 0) { printf(4.ecei6ed from *enderR $rame 7s 8n4 content)! phR printf(48n #cknoJledIement(#,-/'#-) R 4)! scanf(47s4 ack)! if(H(strcmp(ack 4ack4)""0 [[ strcmp(ack 4nak4)""0 [[ strcmp(ack 4#,-4)""0 [[ str cmp(ack 4'#-4)""0)) { printf(48n 'ot a 6alid #cknoJledIement..use #,- or '#-..8n 4)! Ioto ph! 5 send(sd ack ; 0)! i"rec6(sd content 30 0)! 5 printf(48n8n Lye...4)! close(sd)! return 0! 5
*ocket created (')(. @+.) '+R 2KOA @ort #ddress is 2KOA R ,onnection accepted. *endinI $.#/( 0 to the .ecei6er... *endinI $.#/( 1 to the .ecei6er... '#-R *o aIain sendinI the @re6ious frame... *endinI $.#/( 1 to the .ecei6er... '#-R *o aIain sendinI the @re6ious frame... *endinI $.#/( 1 to the .ecei6er... *endinI $.#/( 0 to the .ecei6er... '#-R *o aIain sendinI the @re6ious frame... *endinI $.#/( 0 to the .ecei6er... *endinI $.#/( 1 to the .ecei6er... *endinI $.#/( 0 to the .ecei6er... Lye..
CLIENT OUTPUT:
9cseb332:&nsat;( <=> cc stopcli.c 9cseb332:&nsat;( <=> ./a.out (')(. @+.) '+2KOA .ecei6ed from *enderR $rame 0 #cknoJledIement(#,-/'#-) R ack .ecei6ed from *enderR $rame 1 #cknoJledIement(#,-/'#-) R nak .ecei6ed from *enderR $rame 1 #cknoJledIement(#,-/'#-) R nak .ecei6ed from *enderR $rame 1 32
#cknoJledIement(#,-/'#-) R ack .ecei6ed from *enderR $rame 0 #cknoJledIement(#,-/'#-) R nak .ecei6ed from *enderR $rame 0 #cknoJledIement(#,-/'#-) R ack .ecei6ed from *enderR $rame 1 #cknoJledIement(#,-/'#-) R ack .ecei6ed from *enderR $rame 0 #cknoJledIement(#,-/'#-) R ack Lye...
if(bi""31) { printf(48n(..+. &n bind4)! return 0! 5 i"siBeof(cli)! listen(sd ;)! nsd"accept(sd (struct sockaddrG)Ccli Ci)! if(nsd""31) { printf(4,heck parameter8n4)! return 0! 5 printf(48n,onnection accepted8n4)! i"rec6(nsd e 10 0)! if(strcmp(e a)""0) printf(4)he &@ address of yahoo is 12O.0.0.18n4)! else if(strcmp(e b)""0) printf(4)he &@ address of IooIle is 12O.0.0.28n4)! else if(strcmp(e c)""0) printf(48n)he &@ address of rediff is 12O.0.0.38n4)! close(sd)! close(nsd)! return 0! 5
CLIENT PROGRAM:
#include<stdio.h> #include<netinet/in.h> #include<strinI.h> #include<sys/socket.h> int main() { int sd con port n! char a910="4yahoo4 b910="4IooIle4 c910="4rediff4 e910=! struct sockaddr%in cli! printf(4(nter the port no. R8n4)! scanf(47d4 Cport)! if((sd"socket(#$%&'() *+,-%*).(#/ 0))""31) { printf(4(..+. &' *+,-()4)! return 0! 5 bBero((char G)Ccli siBeof(cli))! cli.sin%family"#$%&'()! cli.sin%port"htons(port)! 3?
cli.sin%addr.s%addr"htonl(&'#11.%#'D)! con"connect(sd (struct sockaddrG)Ccli siBeof(cli))! if(con""31) { printf(4(..+. &' ,+''(,)8n4)! return 0! 5 printf(48nZost 'ameR8n4)! printf(48n1.Dahoo8n2.2ooIle8n3..ediiff8n4)! printf(4select hostR8n4)! scanf(47d4 Cn)! if(n""1) send(sd a 10 0)! else if(n""2) send(sd b 10 0)! else if(n""3) send(sd c 10 0)! else printf(4*elect from optionsR8n4)! close(sd)! return 0! 5
SERVER OUTPUT:
9cseb332:&nsat;( <=> cc dns.c 9cseb332:&nsat;( <=> ./a.out (nter port no.R?;K3 *ocket ,reated @ort #ddress is ?;K3 ,onnection accepted )he &@ address of IooIle is 12O.0.0.2
CLIENT OUTPUT:
9cseb332:&nsat;( <=> cc dnscli.c 9cseb332:&nsat;( <=> ./a.out (nter the port no. R ?;K3 Zost 'ameR 1.Dahoo 2.2ooIle 3..ediiff select hostR 3;
int i V k n num m"0 I"0 count"0! printf(4(nter the number of autonomus systems8n4)! scanf(47d4 Cn)! for(i"0!i<n!iUU) { printf(4(nter the name of autonomus system8n4)! scanf(47s4 Ca9i=.name)! printf(4(nter number of &@ address in system8n4)! scanf(47d4 Ca9i=.no)! V"a9i=.no! printf(4enter the address4)! for(k"0!k<V!kUU) scanf(47s4 Ca9i=.addr9k=)! 5 printf(4(nter the number of ser6ers4)! scanf(47d4 Cnum)! printf(4enter the name8n4)! for(i"0!i<num!iUU) scanf(47s4 sname9i=)! for(i"0!i<n!iUU) {printf(4\ho is ser6er of7s8n4 a9i=.name)! scanf(47s4 Csname19i=)! 5k"0! for(V"0!V<num!VUU) {if(strcmp(sname9V= sname19k=)""0) {for(m"0!m<a9V=.no!mUU) {strcpy(c.addr9I= a9V=.addr9m=)! kUU!IUU! 55 else {for(m"0!m<a9V=.no!mUU) {strcpy(d.addr9I= a9V=.addr9m=)! IUU! 555 printf(4the addr knoJn by the main ser6er are8n4)! printf(47d8n4 I)! for(i"0!i<I!iUU) { printf(47s4 c.addr9i=)! printf(48n4)! 55
OUTPUT:
9cseb332:&nsat;( <=> cc bIp.c 9cseb332:&nsat;( <=> ./a.out (nter the number of autonomus systems 3O
1 (nter the name of autonomus system #*1 (nter number of &@ address in system ? enter the address 1F0.100.102.; 1F0.100.102.? 1F0.100.102.3 1F0.100.102.1 (nter the number of ser6ers1 enter the name L2@ *(.](. \ho is ser6er of#*1 the addr knoJn by the main ser6er are ?
return! 5 for(i"1!i<"n!iUU) for(V"1!V<"n!VUU) fscanf(f1 47d4 CadVmat9i=9V=)! fclose(f1)! printf(48n)he adVacency matriN is8n8n4)! 6ieJadVmat()! Jhile(1) { scanf(47c4 Cs)! printf(48n(nter the source code8n4)! fflush(stdin)! scanf(47c4 Cs)! scanf(47c4 Cd)! printf(48n8n(nter the destination code8n4)! fflush(stdin)! scanf(47c4 Cd)! printf(47c 7c4 s d)! src"toupper(s)3K?! des"toupper(d)3K?! tot"searchpath(src des pathmat Cminlen)! printf(47d4 tot)! 6ieJpathmat(pathmat tot minlen)! printf(48n1o S Jant to continue(D/')4)! scanf(47c4 ch)! if(chH"EDE CC chH"EyE) break! 5 5 6oid 6ieJpathmat(int pm9/#P= int n int len) { int k! if(lenH"0) { printf(48nminlen isR7d8n8n shortest path isR4 len)! for(k"n!k>1!k33) printf(47c3>4 pm9k=UK?)! printf(47c8n4 pm9k=UK?)! printf(41istance is R 4)! for(k"n!k>1!k33) printf(47d 4 adVmat9pm9k==9pm9k31==)! 5 else printf(48n no path from source to destination node8n4)! 5 6oid 6ieJadVmat() { int i V! 3A
printf(48n8t4)! for(i"1!i<"n!iUU) printf(47?c4 iUK?)! printf(48n8n4)! for(i"1!i<"n!iUU printf(48n8n4)) for(V"1!V<"n!VUU) { if (V "" 1) printf(47?c4 iUK?)! printf(47?d4 adVmat9i=9V=)! 5 5 int searchpath(int src int des int pathmath9/#P= int Gminlen) { node Iraph9/#P=! int i k min tot"0 cur6erteN neJlen 6 u! Gminlen"0! for(i"1!i<"n!iUU) { Iraph9i=.pre6ious"0! Iraph9i=.len"&'$&'&)D! Iraph9i=.status"S']&*&)(1! // cur6erteN"src! 5 Iraph9src=.pre6ious"0! Iraph9src=.len"0! Iraph9src=.status"]&*&)(1! cur6erteN"src! Jhile(cur6erteNH"des) { for(k"1!k<"n!kUU) { if(adVmat9cur6erteN=9k=>0CCIraph9k=.status "" S']&*&)(1) { neJlen"Iraph9cur6erteN=.lenUadVmat9cur6erteN=9k=! if(neJlen<Iraph9k=.len) { Iraph9k=.pre6ious"cur6erteN! Iraph9k=.len"neJlen! 5 5 5 min"&'$&'&)D! cur6erteN"0! for(i"1!i<"n!iUU) if(Iraph9i=.status""S']&*&)(1 CC Iraph9i=.len<min) { min"Iraph9i=.len! cur6erteN"i! ?0
5 if(cur6erteN""0) return 0! Iraph9cur6erteN=.status"]&*&)(1! 5 Jhile(cur6erteNH"0) { pathmath9UUtot="cur6erteN! cur6erteN"Iraph9cur6erteN=.pre6ious! 5 for(i"tot!i>1!i33) { u"pathmath9i=! 6"pathmath9i31=! Gminlen"GminlenUadVmat9u=96=! 5 return tot! 5
(nter the source code # (nter the destination code 1 # 13 minlen isR2 shortest path isR#3>L3>1 1istance is R 1 1 1o S Jant to continue(D/')R'
for(V"0!V<n!VUU) for(k"0!k<n!kUU) if(rt9i=.dist9V=>dmat9i=9k=Urt9k=.dist9V=) { rt9i=.dist9V="rt9i=.dist9k=Urt9k=.dist9V=! rt9i=.from9V="k! countUU! 5 5Jhile(countH"0)! for(i"0!i<n!iUU) { printf(48n8n*tate 6alue for router 7d is 8n4 iU1)! for(V"0!V<n!VUU) { printf(48t8nnode 7d 6ia 7d 1istance7d4 VU1 rt9i=.from9V= U1 rt9i=.dist9V=)! 5 5 printf(48n8n4)! 5
OUTPUT:
9cseb332:&nsat;( <=> cc dst.c 9cseb332:&nsat;( <=> ./a.out (nter the number of nodes R ? (nter the cost matriN R 0 3 ; AA 3 0 AA 1 ;?02 AA 1 2 0 *tate 6alue for router 1 is node 1 6ia 1 1istance0 node 2 6ia 2 1istance3 node 3 6ia 3 1istance; node ? 6ia 2 1istance? *tate 6alue for router 2 is
?3
node 1 6ia 1 1istance3 node 2 6ia 2 1istance0 node 3 6ia ? 1istance3 node ? 6ia ? 1istance1 *tate 6alue for router 3 is node 1 6ia 1 1istance; node 2 6ia ? 1istance3 node 3 6ia 3 1istance0 node ? 6ia ? 1istance2 *tate 6alue for router ? is node 1 6ia 2 1istance? node 2 6ia 2 1istance1 node 3 6ia 3 1istance2 node ? 6ia ? 1istance0
perror(4socket4)! eNit(1)! 5 /G set up destination address G/ memset(Caddr 0 siBeof(addr))! addr.sin%family"#$%&'()! addr.sin%addr.s%addr"inet%addr(Z(MM+%2.+S@)! addr.sin%port"htons(Z(MM+%@+.))! /G noJ Vust sendto() our destinationH G/ Jhile (1) { if (sendto(fd messaIe siBeof(messaIe) 0 (struct sockaddr G) Caddr siBeof(addr)) < 0) { perror(4sendto4)! eNit(1)! 5 sleep(1)! 5 5
LISTENER PROGRAM:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <time.h> #include <strinI.h> #include <stdio.h> #define Z(MM+%@+.) 123?; #define Z(MM+%2.+S@ 422;.0.0.3O4 #define /*2LS$*&Q( 2;K main(int arIc char GarI69=) { struct sockaddr%in addr! int fd nbytes addrlen! struct ip%mreW mreW! char msIbuf9/*2LS$*&Q(=! u%int yes"1! /GGG /+1&$&,#)&+' )+ +.&2&'#M G/ /G create Jhat looks like an ordinary S1@ socket G/ if ((fd"socket(#$%&'() *+,-%12.#/ 0)) < 0) { perror(4socket4)! eNit(1)! 5 /GGGG /+1&$&,#)&+' )+ +.&2&'#M G/ ?;
/G alloJ multiple sockets to use the same @+.) number G/ if (setsockopt(fd *+M%*+,-() *+%.(S*(#11. Cyes siBeof(yes)) < 0) { perror(4.eusinI #11. failed4)! eNit(1)! 5 /GGG ('1 +$ /+1&$&,#)&+' )+ +.&2&'#M G/ /G set up destination address G/ memset(Caddr 0 siBeof(addr))! addr.sin%family"#$%&'()! addr.sin%addr.s%addr"htonl(&'#11.%#'D)! /G '.L.R differs from sender G/ addr.sin%port"htons(Z(MM+%@+.))! /G bind to recei6e address G/ if (bind(fd (struct sockaddr G) Caddr siBeof(addr)) < 0) { perror(4bind4)! eNit(1)! 5 /G use setsockopt() to reWuest that the kernel Voin a multicast Iroup G/ mreW.imr%multiaddr.s%addr"inet%addr(Z(MM+%2.+S@)! mreW.imr%interface.s%addr"htonl(&'#11.%#'D)! if (setsockopt(fd &@@.+)+%&@ &@%#11%/(/L(.*Z&@ CmreW siBeof(mreW)) < 0) { perror(4setsockopt4)! eNit(1)! 5 /G noJ Vust enter a read3print loop G/ Jhile (1) { addrlen"siBeof(addr)! if ((nbytes"rec6from(fd msIbuf /*2LS$*&Q( 0 (struct sockaddr G) Caddr Caddrlen)) < 0) { perror(4rec6from4)! eNit(1)! 5 puts(msIbuf)! 5 5
?K
CLIENT1 OUTPUT:
9cseb332:&nsat;( <=> cc mullis.c 9cseb332:&nsat;( <=> ./a.out Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai 93=U *topped ./a.out
CLIENT2 OUTPUT:
9cseb332:&nsat;( <=> cc mullis.c 9cseb332:&nsat;( <=> ./a.out Zai Zai Zai Zai Zai Zai ?O
Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai 93=U *topped ./a.out
CLIENT3 OUTPUT:
9cseb332:&nsat;( <=> cc mullis.c 9cseb332:&nsat;( <=> ./a.out Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai Zai ?F
CLIENT PROGRAM:
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<sys/types.h> #include<netinet/in.h> #include<strinI.h> #include<sys/socket.h> #include<arpa/inet.h> main() { int csd cport len! char re6msI9100=! struct sockaddr%in ser6addr! printf(4(nter the port8n4)! scanf(47d4 Ccport)! csd"socket(#$%&'() *+,-%*).(#/ 0)! if(csd<0) printf(4,anEt ,reate8n4)! else printf(4*ocket is ,reated8n4)! ser6addr.sin%family"#$%&'()! ser6addr.sin%addr.s%addr"htonl(&'#11.%#'D)! ser6addr.sin%port"htons(cport)! if(connect(csd (struct sockaddrG)Cser6addr siBeof(ser6addr))<0) printf(4,onnection error8n4)! rec6(csd re6msI 100 0)! printf(47s8n4 re6msI)! 5
CLIENT OUTPUT:
9cseb332:&nsat;( <=> cc datecli.c 9cseb332:&nsat;( <=> ./a.out (nter the port 12KF *ocket is ,reated *at +ct 20 1KR33R;A 2012
nsd"accept(sd (struct sockaddrG)Ccli Ci)! if(nsd""31) { printf(48n(rrorR,lient #ccepted @roblem4)! return 0! 5 printf(48n,lient #ccepted4)! i"0! Jhile(1) { printf(49)he ,ommand #=4)! rec6(nsd Ccontent F0 0)! printf(47s8n4 content)! if(Hstrcmp(content 4eNit4)) { printf(4client C ser6er eNited8n4)! return 0! 5 system(content)! 5 close(sd)! close(nsd)! return 0! 5
CLIENT PROGRAM:
#include<stdio.h> #include<sys/types.h> #include<netinet/in.h> #include<unistd.h> #include<strinI.h> #include<sys/socket.h> int main() { int sd i"0 port"3?KF! char content9F0="4804! struct sockaddr%in ser! if((sd"socket(#$%&'() *+,-%*).(#/ &@@.+)+%),@))""31) { printf(4socket creation problem8n4)! return 0! 5 bBero((char G)Cser siBeof(ser))! printf(48n@ort address is 7d4 port)! ser.sin%family"#$%&'()! ser.sin%port"htons(port)! ;2
ser.sin%addr.s%addr"htonl(&'#11.%#'D)! if(connect(sd (struct sockaddr G)Cser siBeof(ser))""31) { printf(4,onnectinI problem4)! return 0! 5 printf(48n,lient module8n4)! printf(4GGGGGGGGGGGG8n4)! Jhile(1) { Jhile(i<OA) { scanf(47c4 Ccontent9i=)! if(content9iUU=""E8nE) break! 5 if(iH"OA) i33! content9i="E80E! i"0! send(sd content F0 0)! if(Hstrcmp(content 4eNit4)) { printf(4(P&)%S canEt eNecute in ser6er8n4)! break! 5 5 close(sd)! return 0! 5
OUTPUT: SERVER:
9cseb332:&nsat;( <=> cc rceser.c 9cseb332:&nsat;( <=> ./a.out )he port address is 3?KF *er6er /odule ,lient #ccepted9)he ,ommand #= 9)he ,ommand #= 9)he ,ommand #=ls ;3
8 dist6ect.c msI%ser6er serrr.c accccept.c dns.c msI%ser6er.c ser6er.c accept.c dnscli.c msI%s6c.c simpclient.c acceptt.c dnsser.c msI.N simp.h a.out dst.c mul.c simpser6ice.c arp.c dup.c mullis.c simpser6ice.cJW arpcli.c eof.c mulser.c simp.N arpp.c eN1.c multicli slidecli.c arppp.c fifo multicli.c slideser.c arpser.c file1.dat multiser slideJindoJcli.c bIp.c file2.dat multiser.c slideJindoJser.c bindinI.c file3.dat namedpipe.c slidinIJindoJ.c bitstuffinI.c fname neJ.c slidiser.c
CLIENT:
9cseb332:&nsat;( <=> cc rcecli.c 9cseb332:&nsat;( <=> ./a.out @ort address is 3?KF ,lient module GGGGGGGGGGGG ls
412.32.?;.0.0.O4 412.32.?;.0.0.F4 412.32.?;.0.0.A4 412.32.?;.0.0.1045! sd " socket(#$%&'() *+,-%12.#/ 0)! printf(4$irst @ort8n4)! scanf(47d4 Cport1)! s1.sin%port"htons(port1)! s1.sin%family"s2.sin%family"s3.sin%family"#$%&'()! s1.sin%addr.s%addr"inet%addr(41F0.100.102.;4)! s2.sin%addr.s%addr"s3.sin%addr.s%addr"inet%addr(41F0.100.102.;4)! printf(4*econd @ort8n4)! scanf(47d4 Cport2)! s2.sin%port"htons(port2)! printf(4)hird @ort8n4)! scanf(47d4 Cport3)! s3.sin%port"htons(port3)! if(bind(sd (struct sockaddr G)Cs1 siBeof(s1))<0) { printf(4Lind (rror4)! return(0)! 5 sB"siBeof(s2)! if(fork()""0) Jhile(1) { fcntl(sd $%*()$M +%'+'LM+,-)! rec6from(sd Cstr 30 0 (struct sockaddr G)Cs2 CsB)! if(strcmp(str 4804)) { k"2! Ioto s! 5 sB"siBeof(s3)! rec6from(sd Cstr 30 0 (struct sockaddr G)Cs3 CsB)! k"3! sR if(strcmp(str 4804)) { printf(47s8n4 str)! for(i"0!i<10!iUU) { if(Hstrcmp(str ip9i=)) { strcpy(strinI 4.eply from system 1 4)! strcat(strinI eth9i=)! if(k""2) sendto(sd CstrinI 30 0 (struct sockaddr G)Cs2 siBeof(s2))! else sendto(sd CstrinI 30 0 (struct sockaddr G)Cs3 siBeof(s3))! ;;
break! 5 if(Hstrcmp(str eth9i=)) { strcpy(strinI 4.eply from system 1 4)! strcat(strinI ip9i=)! if(k""2) sendto(sd CstrinI 30 0 (struct sockaddr G)Cs2 siBeof(s2))! break! 5 5 5 strcpy(str 4804)! k"0! 5 else Jhile(1) { int flI"0! scanf(47s4 str)! for(i"0!i<10!iUU) { if(Hstrcmp(str ip9i=)) { printf(4.ecords found 8n4)! printf(4(thernet #ddress R 7s8n4 et flI"1! 5 if(Hstrcmp(str eth9i=)) { printf(4.ecords found 8n4)! printf(4&@ #ddress R 7s8n4 ip9i=)! flI"1! 5 5 if(flI) continue! sendto(sd Cstr 30 0 (struct sockaddr G)Cs2 siBeof(s2))! sendto(sd Cstr 30 0 (struct sockaddr G)Cs3 siBeof(s3))! 5 close(sd)! 5
;K
OUTPUT:
9cseb30A:&nsat;( <=> cc arp.c 9cseb30A:&nsat;( <=> ./a.out $irst @ort FFFF *econd @ort KKKK )hird @ort ;;;; 12O.0.0.; .ecords found (thernet #ddress R 12.32.?;.0.0.; 12.32.?;.0.0.K .ecords found &@ #ddress R 12O.0.0.K
;O