1.I mpl ementt hedat al i nkl ay erf r ami ngmet hodssuchas char act erst uf f i ngandbi tst uf f i ng. Cpr ogr am t oi mpl ementchar act erst uf f i ng. Pr ogr am: #i ncl ude<st di o. h> #i ncl ude #i ncl ude<st r i ng. h> v oi dmai n( ) { i nti =0, j =0, n, pos; chara[ 20] , b[ 50] , ch; pr i nt f ( " \ nent erst r i ng: " ) ; scanf ( " %s" , &a) ; n=st r l en( a) ; b[ 0] =' D' ; b[ 1] =' L' ; b[ 2] =' E' ; b[ 3] =' S' ; b[ 4] =' T' ; b[ 5] =' X' ; j =6; whi l e( i
b[ j ] =' D' ; b[ j +1] =' L' ; b[ j +2] =' E' ; j =j +3; b[ j ] =a[ i ] ; i ++; j ++; } el se { b[ j ] =a[ i ] ; i ++; j ++; } } b[ j ] =' D' ; b[ j +1] =' L' ; b[ j +2] =' E' ; b[ j +3] =' E' ; b[ j +4] =' T' ; b[ j +5] =' X' ; b[ j +6] =' \ 0' ; pr i nt f ( " \ nf r ameaf t erst uf f i ng: " ) ; pr i nt f ( " %s" , b) ; }
OUTPUT:
Cpr ogr am f orbi tst uf f i ng Pr ogr am: #i ncl ude<st di o. h> #i ncl ude #i ncl ude<st r i ng. h> v oi dmai n( ) { i nta[ 20] , b[ 30] , i , j , k, count , n; pr i nt f ( " \ nent erf r amesi ze: " ) ; scanf ( " %d" , &n) ; pr i nt f ( " \ nent erf r amei nt hef or m of0and1: " ) ; f or ( i =0; i
i f ( a[ i ] ==1) { b[ j ] =a[ i ] ; f or ( k=i +1; a[ k] ==1&k
pr i nt f ( " %d" , b[ i ] ) ; pr i nt f ( " 01111110" ) ; get ch( ) ; }
OUTPUT:
2. I mpl ementonadat asetofchar act er st het hr eeCRC pol y nomi al s— CRC12, CRC16and CRCCCI P. Pr ogr am:
#i ncl ude<st di o. h> #i ncl ude #i ncl ude<st r i ng. h> v oi dmai n( ){ i nti , j , key l en, msgl en; chari nput [ 100] , key [ 30] , t emp[ 30] , quot [ 100] , r em[ 30] , key 1[ 30] ; cl r scr ( ) ; pr i nt f ( " Ent erDat a: " ) ; get s( i nput ) ; pr i nt f ( " Ent erKey : " ) ;
get s( key ) ; key l en=st r l en( key ) ; msgl en=st r l en( i nput ) ; st r cpy ( key 1, key ) ; f or( i =0; i 0; j ){ i f ( t emp[ j ] ==key [ j ] ) r em[ j 1] =' 0' ; el se r em[ j 1] =' 1' ; } r em[ key l en1] =i nput [ i +key l en] ; st r cpy ( t emp, r em) ; } st r cpy ( r em, t emp) ; pr i nt f ( " \ nQuot i enti s" ) ;
f or( i =0; i <msgl en; i ++) pr i nt f ( " %c" , quot [ i ] ) ; pr i nt f ( " \ nRemai nderi s" ) ; f or( i =0; i
Out put :
3. I mpl ementDi j kst r asal gor i t hm t ocomput et heShor t est pat ht hr oughagr aph. pr ogr am: #i ncl ude<st di o. h> #i ncl ude #def i neI NFI NI TY9999 #def i neMAX10 v oi ddi j kst r a( i ntG[ MAX] [ MAX] , i ntn, i ntst ar t node) ; i ntmai n( )
{
i ntG[ MAX] [ MAX] , i , j , n, u; pr i nt f ( " Ent erno.ofv er t i ces: " ) ; scanf ( " %d" , &n) ; pr i nt f ( " \ nEnt ert headj acencymat r i x: \ n" ) ; f or ( i =0; i
scanf ( " %d" , &G[ i ] [ j ] ) ;
pr i nt f ( " \ nEnt ert hest ar t i ngnode: " ) ; scanf ( " %d" , &u) ; di j kst r a( G, n, u) ; }
r et ur n0;
v oi ddi j kst r a( i ntG[ MAX] [ MAX] , i ntn, i ntst ar t node) { i ntcost [ MAX] [ MAX] , di st ance[ MAX] , pr ed[ MAX] ; i ntv i si t ed[ MAX] , count , mi ndi st ance, next node, i , j ; / / pr ed[ ]st or est hepr edecessorofeachnode / / countgi v est henumberofnodesseensof ar / / cr eat et hecostmat r i x f or ( i =0; i
f or ( i =0; i
i f ( di st ance[ i ] <mi ndi st ance&&! v i si t ed[ i ] ) { mi ndi st ance=di st ance[ i ] ; next node=i ; } / / checki fabet t erpat hexi st st hr oughnext node v i si t ed[ next node] =1; f or ( i =0; i
di st ance[ i ] =mi ndi st ance+cost [ next node] [ i ] ; count ++; }
}
pr ed[ i ] =next node;
/ / pr i ntt hepat handdi st anceofeachnode f or ( i =0; i
}
}
out put :
j =i ; do { j =pr ed[ j ] ; pr i nt f ( " <%d" , j ) ; } whi l e( j ! =st ar t node) ;
4.Takeanexampl esubnetgr aphwi t hwei ght si ndi cat i ng del aybet weennodes.Now obt ai nRout i ngt abl ear teach nodeusi ngdi st ancev ect orr out i ngal gor i t hm. Pr ogr am:
#i ncl ude #i ncl ude<st di o. h> st r uctnode{ i ntdi st [ 20] ; i ntf r om[ 20] ; }r out e[ 10] ;
i ntmai n( ) { i ntdm[ 20] [ 20] , no, i , j , k, f l ag; cl r scr ( ) ;
pr i nt f (" \ nEnt ernoofnodes: " ) ; scanf ( " %d" , &no) ; pr i nt f ( " \ nEnt ert hedi st ancemat r i x : " ) ; f or( i =0; i <no; i ++){ f or( j =0; j <no; j ++){ scanf ( " %d" , &dm[ i ] [ j ] ) ; dm[ i ] [ i ]=0; r out e[ i ] . di st [ j ]=dm[ i ] [ j ] ; r out e[ i ] . f r om[ j ]=j ; } }
do{ f l ag=0; f or(i =0; i <no; i ++){ f or( j =0; j <no; j ++){ f or(k=0; k<no; k++){ i f( ( r out e[ i ] . di st [ j ] )>( r out e[ i ] . di st [ k]+r out e[ k] . di st [ j ] ) ){ r out e[ i ] . di st [ j ]=r out e[ i ] . di st [ k]+r out e[ k] . di st [ j ] ; r out e[ i ] . f r om[ j ]=k; f l ag=1; } } } }
}whi l e( f l ag) ;
f or( i =0; i <no; i ++){ pr i nt f ( " \ nRout i ngt abl eofr out er%d: " , i +1) ; pr i nt f ( " \ nTo\ t Next \ t Cost " ) ; f or( j =0; j <no; j ++) i f ( i ! =j ) pr i nt f ( " \ n%d\ t %d\ t \ t %d\ n" , j +1, r out e[ i ] . f r om[ j ] +1, r out e[ i ] . di st [ j ] ) ; } r et ur n0; }
OUTPUT:
5. Takeanexampl esubnetofhost s.Obt ai nbr oadcastt r ee f ori t . Pr ogr am:
#i ncl ude<st di o. h> #i ncl ude v oi dadj ( i nt ) ; i nta[ 10] [ 10] , n; v oi dmai n( ) { i nti , j , r oot ; cl r scr ( ) ; pr i nt f ( " ent ernoofnodes: " ) ; scanf ( " %d" , &n) ; pr i nt f ( " \ nent eradj acencymat r i x : " ) ; f or ( i =1; i <=n; i ++) f or ( j =1; j <=n; j ++) scanf ( " %d" , &a[ i ] [ j ] ) ; pr i nt f ( " \ nent err oot : " ) ; scanf ( " %d" , &r oot ) ; adj ( r oot ) ; get ch( ) ;
} v oi dadj ( i ntk) { i nti , j ; pr i nt f ( " \ nBr oadcastt r ee: " ) ; pr i nt f ( " \ n\ t %d\ n" , k) ; f or ( j =1; j <=n; j ++) { i f ( ( a[ k] [ j ] ==1) | | ( a[ j ] [ k] ==1) ) pr i nt f ( " %d\ t " , j ) ; } pr i nt f ( " \ n" ) ; f or ( i =1; i <=n; i ++) i f ( ( a[ k] [ i ] ==0) &&( a[ i ] [ k] ==0) &&( i ! =k) ) pr i nt f ( " %d\ t " , i ) ; }
Out put :
6. Pr ogr amst oi mpl ementer r orcor r ect i onanddet ect i on. Pr ogr am: #i ncl ude<st di o. h> #i ncl ude<mat h. h> #i ncl ude i nti nput [ 32] ; i ntdat a[ 32] , dat ar ec[ 32] ; i ntham_ cal c( i nt , i nt ) ; v oi dmai n( )
{ i ntn, i , p_ n=0, c_ l , j , k, c, er r or _ pos; cl r scr ( ) ; pr i nt f ( " Ent ert hel engt hoft heDat abi t : " ) ; scanf ( " %d" , &n) ; pr i nt f ( " Ent ert heDat abi t : \ n" ) ; f or ( i =0; i ( i nt ) pow( 2, i ) ( i +1) ) { p_ n++; i ++; } c_ l =p_ n+n; j =k=0; f or ( i =0; i
pr i nt f ( " Ent ert her ecei v eddat abi t : \ n" ) ; f or ( i =0; i
Out put :
7. Pr ogr amsf orI Paddr essconv er si onf unct i on pr ogr am: #i ncl ude<st di o. h> #i ncl ude<sy s/ socket . h> #i ncl ude<st dl i b. h> #i ncl ude<ar pa/ i net . h> #def i neSERV_ PORT9002 i ntmai n( ) { charst r [ I NET_ ADDRSTRLEN] , * st r 1, * st r 2; st r uctsockaddr _ i nser v er ;
pr i nt f ( " Test i ngi net _ pt onandi neti net _ nt op\ n" ) ;
i net _ pt on( AF_ I NET, " 192. 0. 2. 33" , &( ser v er . si n_ addr ) ) ; i net _ nt op( AF_ I NET, &( ser v er . si n_ addr ) , st r , I NET_ ADDRSTRLEN) ; pr i nt f ( " %s\ n" , st r ) ; / /pr i nt s" 192. 0. 2. 33"
bzer o( &st r 1, si zeof ( st r 1) ) ; bzer o( &ser v er , si zeof ( ser v er ) ) ;
pr i nt f ( " Test i ngi net _ nt oaandi net _ at on\ n" ) ; i net _ at on( " 192. 168. 1. 1" , &( ser v er . si n_ addr ) ) ; st r 1=i net _ nt oa( ser v er . si n_ addr ) ; pr i nt f ( " %s\ n" , st r 1) ;
bzer o( &st r 1, si zeof ( st r 1) ) ; bzer o( &ser v er , si zeof ( ser v er ) ) ;
pr i nt f ( " Test i ngi net _ addr \ n" ) ; ser v er . si n_ addr . s_ addr =i net _ addr ( " 192. 2. 2. 2" ) ; st r 2=i net _ nt oa( ser v er . si n_ addr ) ; pr i nt f ( " %s\ n" , st r 2) ; } out put : sh4. 1$v i i pco. c sh4. 1$ccoi pcoi pco. c sh4. 1$. / i pco Test i ngi net _ pt onandi neti net _ nt op 192. 0. 2. 33
Test i ngi net _ nt oaandi net _ at on 192. 168. 1. 1 Test i ngi net _ addr 192. 2. 2. 2
8. Cl i entser v erappl i cat i onsusi ngi nt erpr ocess communi cat i onandsy nchr onousmechani sms:( a) FI FO ( b) Messagequeues ( c) Shar edmemor y ( a) Fi f o ser v er : #i ncl ude<st di o. h> #i ncl ude #i ncl ude<sy s/ t y pes. h> #i ncl ude<sy s/ st at . h> #i ncl ude #i ncl ude<st dl i b. h> #i ncl ude<st r i ng. h> mai n( ) { i ntwr f d, r df d, n, d, r et _ v al , count ; charbuf [ 50] ; / * cr eat et hef i r stnamedpi pe* / r et _ v al =mkf i f o( " np1" , 0666) ; / * cr eat et hesecondnamedpi pe* / r et _ v al =mkf i f o( " np2" , 0666) ; / * opent hef i r stnamedpi pef orr eadi ng* / r df d=open( " np1" , O_ RDONLY) ; / * opent hesecondnamedpi pef or wr i t i ng* /
wr f d=open( " np2" , O_ WRONLY) ; / * r eadf r om t hef i r stpi pe* / n=r ead( r df d, buf , 50) ; buf [ n] =' \ 0' ; / / endofl i ne pr i nt f ( " f ul l dupl exser v er : r eadf r om t hepi pe: %s\ n" , buf ) ; / * conv er tt hest r i ngt oupper cl ass* / count =0; whi l e( count #i ncl ude<sy s/ t y pes. h> #i ncl ude<sy s/ st at . h> #i ncl ude #i ncl ude<st dl i b. h> #i ncl ude<st r i ng. h> #i ncl ude mai n( ) { i ntwr f d, r df d, n; charbuf [ 50] , l i ne[ 50] ; / * opent hef i r stnamedpi pef orwr i t i ng* / wr f d=open( " np1" , O_ WRONLY) ; / * cr eat et hesecondnamedpi pef orr eadi ng * /
r df d=open( " np2" , O_ RDONLY) ; / * wr i t et ot hepi pe* / pr i nt f ( " ent erl i neoft ext " ) ; get s( l i ne) ; wr i t e( wr f d, l i ne, st r l en( l i ne) ) ; / * r eadf r om t hepi pe* / n=r ead( r df d, buf , 50) ; buf [ n] =' \ 0' ; / / endofl i ne pr i nt f ( " f ul l dupl excl i ent : r eadf r om t hepi pe: %s\ n" , buf ) ; } out put : ser v er : sh4. 1$. / msgsnd f ul l dupl exser v er : r eadf r om t hepi pe: hel l o cl i ent : sh4. 1$. / msgr cv ent erl i neoft ext : hel l o f ul l dupl excl i ent : r eadf r om t hepi pe: HELLO
( b) MessageQueue ser v er : #i ncl ude<sy s/ t y pes. h> #i ncl ude<sy s/ msg. h> #i ncl ude<sy s/ i pc. h> #i ncl ude<st r i ng. h> #i ncl ude<st di o. h> i ntmai n( ) {
key _ ti pckey ; i ntmq_ i d; st r uct { l ongt y pe; chart ext [ 100] ; }my msg; i pckey =f t ok( " / t mp/ f oo" , 42) ; pr i nt f ( " Mykeyi s%d\ n" , i pckey ) ; mq_ i d=msgget ( i pckey , I PC_ CREAT| 0666) ; pr i nt f ( " Messagei dent i f i eri s%d\ n" , mq_ i d) ; memset ( my msg. t ext , 0, 100) ; st r cpy ( my msg. t ext , " Hel l owor l d! " ) ; my msg. t y pe=1; msgsnd( mq_ i d, &my msg, si zeof ( my msg) , 0) ; } cl i ent : #i ncl ude<sy s/ t y pes. h> #i ncl ude<sy s/ msg. h> #i ncl ude<sy s/ i pc. h> #i ncl ude<st r i ng. h> #i ncl ude<st di o. h> i ntmai n( ) { key _ ti pckey ; i ntmq_ i d;
st r uct { l ongt y pe; chart ext [ 100] ; } my msg; i ntr ecei v ed; i pckey =f t ok( " / t mp/ f oo" , 42) ; pr i nt f ( " Mykeyi s%d\ n" , i pckey ) ; mq_ i d=msgget ( i pckey , 0) ; pr i nt f ( " Messagei dent i f i eri s%d\ n" , mq_ i d) ; r ecei v ed=msgr cv ( mq_ i d, &my msg, si zeof ( my msg) , 0, 0) ; pr i nt f ( " %s( %d) \ n" , my msg. t ext , r ecei v ed) ; } out put : sh4. 1$ccomsgsermsgser v er . c sh4. 1$ccomsgcl i msgcl i ent . c sh4. 1$. / msgser Mykeyi s1 Messagei dent i f i eri s98304 sh4. 1$. / msgcl i Mykeyi s1 Messagei dent i f i eri s98304 Hel l owor l d!( 112)
( c) Shar edMemor y ser v er :
#i ncl ude<sy s/ t y pes. h> #i ncl ude<sy s/ i pc. h> #i ncl ude<sy s/ shm. h> #i ncl ude<st di o. h> #i ncl ude<st dl i b. h> #def i neMAXSI ZE27 v oi ddi e( char* s) { per r or ( s) ; exi t ( 1) ; } i ntmai n( ) { charc; i ntshmi d; key _ tkey ; char* shm, * s; key =5678; i f ( ( shmi d=shmget ( key , MAXSI ZE, I PC_ CREAT| 0666) ) <0) di e( " shmget " ) ; i f ( ( shm=shmat ( shmi d, NULL, 0) ) ==( char* ) 1) di e( " shmat " ) ; s=shm; f or ( c=' a' ; c<=' z' ; c++) * s++=c; whi l e( * shm! =' * ' )
sl eep( 1) ; exi t ( 0) ; } cl i ent : #i ncl ude<sy s/ t y pes. h> #i ncl ude<sy s/ i pc. h> #i ncl ude<sy s/ shm. h> #i ncl ude<st di o. h> #i ncl ude<st dl i b. h> #def i neMAXSI ZE27 v oi ddi e( char* s) { per r or ( s) ; exi t ( 1) ; } i ntmai n( ) { i ntshmi d; key _ tkey ; char* shm, * s; key =5678; i f ( ( shmi d=shmget ( key , MAXSI ZE, 0666) ) <0) di e( " shmget " ) ; i f ( ( shm=shmat ( shmi d, NULL, 0) ) ==( char* ) 1) di e( " shmat " ) ; f or ( s=shm; * s! =' \ 0' ; s++)
put char ( * s) ; put char ( ' \ n' ) ; * shm=' * ' ; exi t ( 0) ; } out put : sh4. 1$ccossshmser v er . c sh4. 1$ccoscshmcl i ent . c sh4. 1$. / ss sh4. 1$. / sc abcdef ghi j kl mnopqr st uv wxy z
9. Connect i onor i ent edCl i entser v erappl i cat i onswi t hTCP ser v er : #i ncl ude<st di o. h> #i ncl ude #i ncl ude #i ncl ude<st dl i b. h> #i ncl ude<st r i ng. h> #i ncl ude<sy s/ socket . h> #i ncl ude<sy s/ t y pes. h> #def i neMAX80 #def i nePORT8080 #def i neSAst r uctsockaddr v oi df unc( i ntsockf d) { charbuf f [ MAX] ;
i ntn; f or( ; ; ){ bzer o( buf f , MAX) ; r ead( sockf d, buf f , si zeof ( buf f ) ) ; pr i nt f ( " Fr om cl i ent : %s\ tTocl i ent: " , buf f ) ; bzer o( buf f , MAX) ; n=0; whi l e( ( buf f [ n++]=get char ( ) )! =' \ n' ) ; wr i t e( sockf d, buf f , si zeof ( buf f ) ) ; i f( st r ncmp( " exi t " , buf f , 4)==0){ pr i nt f ( " Ser v erExi t . . . \ n" ) ; br eak; } } } i ntmai n( ) { i ntsockf d, connf d, l en; st r uctsockaddr _ i nser v addr , cl i ; sockf d=socket ( AF_ I NET, SOCK_ STREAM, 0) ; i f( sockf d==1){ pr i nt f ( " socketcr eat i onf ai l ed. . . \ n" ) ; exi t ( 0) ; } el se
pr i nt f ( " Socketsuccessf ul l ycr eat ed. . \ n" ) ; bzer o( &ser v addr , si zeof ( ser v addr ) ) ; ser v addr . si n_ f ami l y=AF_ I NET; ser v addr . si n_ addr . s_ addr=ht onl ( I NADDR_ ANY) ; ser v addr . si n_ por t=ht ons( PORT) ; i f( ( bi nd( sockf d, ( SA* ) &ser v addr , si zeof ( ser v addr ) ) )! =0){ pr i nt f ( " socketbi ndf ai l ed. . . \ n" ) ; exi t ( 0) ; } el se pr i nt f ( " Socketsuccessf ul l ybi nded. . \ n" ) ; i f( ( l i st en( sockf d, 5) )! =0){ pr i nt f ( " Li st enf ai l ed. . . \ n" ) ; exi t ( 0) ; } el se pr i nt f ( " Ser v erl i st eni ng. . \ n" ) ; l en=si zeof ( cl i ) ; connf d=accept ( sockf d, ( SA* ) &cl i , &l en) ; i f( connf d<0){ pr i nt f ( " ser v eraccceptf ai l ed. . . \ n" ) ; exi t ( 0) ; } el se pr i nt f ( " ser v eraccceptt hecl i ent . . . \ n" ) ; f unc( connf d) ;
cl ose( sockf d) ; }
cl i ent : #i ncl ude #i ncl ude<st di o. h> #i ncl ude<st dl i b. h> #i ncl ude<st r i ng. h> #i ncl ude<sy s/ socket . h> #def i neMAX80 #def i nePORT8080 #def i neSAst r uctsockaddr v oi df unc( i ntsockf d) { charbuf f [ MAX] ; i ntn; f or( ; ; ){ bzer o( buf f , si zeof ( buf f ) ) ; pr i nt f ( " Ent ert hest r i ng: " ) ; n=0; whi l e( ( buf f [ n++]=get char ( ) )! =' \ n' ) ; wr i t e( sockf d, buf f , si zeof ( buf f ) ) ; bzer o( buf f , si zeof ( buf f ) ) ; r ead( sockf d, buf f , si zeof ( buf f ) ) ; pr i nt f ( " Fr om Ser v er: %s" , buf f ) ;
i f( ( st r ncmp( buf f , " exi t " , 4) )==0){ pr i nt f ( " Cl i entExi t . . . \ n" ) ; br eak; } } }
i ntmai n( ) { i ntsockf d, connf d; st r uctsockaddr _ i nser v addr , cl i ; sockf d=socket ( AF_ I NET, SOCK_ STREAM, 0) ; i f( sockf d==1){ pr i nt f ( " socketcr eat i onf ai l ed. . . \ n" ) ; exi t ( 0) ; } el se pr i nt f ( " Socketsuccessf ul l ycr eat ed. . \ n" ) ; bzer o( &ser v addr , si zeof ( ser v addr ) ) ; ser v addr . si n_ f ami l y=AF_ I NET; ser v addr . si n_ addr . s_ addr=i net _ addr ( " 127. 0. 0. 1" ) ; ser v addr . si n_ por t=ht ons( PORT) ; i f( connect ( sockf d, ( SA* ) &ser v addr , si zeof ( ser v addr ) )! =0){ pr i nt f ( " connect i onwi t ht heser v erf ai l ed. . . \ n" ) ; exi t ( 0) ; }
el se pr i nt f ( " connect edt ot heser v er . . \ n" ) ; f unc( sockf d) ; cl ose( sockf d) ; }
out put :
10. Connect i onl essCl i entser v erappl i cat i onswi t hUDP. Pr ogr am:
Ser v er : #i ncl ude<st di o. h> #i ncl ude<st dl i b. h> #i ncl ude #i ncl ude<st r i ng. h> #i ncl ude<sy s/ t y pes. h> #i ncl ude<sy s/ socket . h> #i ncl ude<ar pa/ i net . h> #i ncl ude #def i nePORT 8080 #def i neMAXLI NE1024 i ntmai n( ){ i ntsockf d; charbuf f er [ MAXLI NE] ; char* hel l o=" Hel l of r om ser v er " ; st r uctsockaddr _ i nser v addr , cl i addr ; i f(( sockf d=socket ( AF_ I NET, SOCK_ DGRAM, 0) )<0){ per r or ( " socketcr eat i onf ai l ed" ) ; exi t ( EXI T_ FAI LURE) ; } memset ( &ser v addr , 0, si zeof ( ser v addr ) ) ; memset ( &cl i addr , 0, si zeof ( cl i addr ) ) ; ser v addr . si n_ f ami l y =AF_ I NET; / /I Pv 4 ser v addr . si n_ addr . s_ addr=I NADDR_ ANY; ser v addr . si n_ por t=ht ons( PORT) i f(bi nd( sockf d, ( constst r uctsockaddr* ) &ser v addr , si zeof ( ser v addr ) )<0) { per r or ( " bi ndf ai l ed" ) ; exi t ( EXI T_ FAI LURE) ;
} i ntl en, n; n=r ecv f r om( sockf d, ( char* ) buf f er , MAXLI NE, MSG_ WAI TALL, (st r uctsockaddr* )&cl i addr , &l en) ; buf f er [ n]=' \ 0' ; pr i nt f ( " Cl i ent: %s\ n" , buf f er ) ; sendt o( sockf d, ( constchar* ) hel l o, st r l en( hel l o) , MSG_ CONFI RM, ( constst r uctsockaddr* )&cl i addr , l en) ; pr i nt f ( " Hel l omessagesent . \ n" ) ; }
r et ur n0;
Cl i ent :
#i ncl ude<st di o. h> #i ncl ude<st dl i b. h> #i ncl ude #i ncl ude<st r i ng. h> #i ncl ude<sy s/ t y pes. h> #i ncl ude<sy s/ socket . h> #i ncl ude<ar pa/ i net . h> #i ncl ude #def i nePORT 8080 #def i neMAXLI NE1024 i ntmai n( ){ i ntsockf d, l en, n; charbuf f er [ MAXLI NE] ; char* hel l o=" Hel l of r om cl i ent " ; st r uctsockaddr _ i n ser v addr ; i f(( sockf d=socket ( AF_ I NET, SOCK_ DGRAM, 0) )<0){ per r or ( " socketcr eat i onf ai l ed" ) ; exi t ( EXI T_ FAI LURE) ; } memset ( &ser v addr , 0, si zeof ( ser v addr ) ) ; ser v addr . si n_ f ami l y=AF_ I NET; ser v addr . si n_ por t=ht ons( PORT) ; ser v addr . si n_ addr . s_ addr=I NADDR_ ANY; sendt o( sockf d, ( constchar* ) hel l o, st r l en( hel l o) , MSG_ CONFI RM, ( constst r uctsockaddr* )&ser v addr , si zeof ( ser v addr ) ) ; pr i nt f ( " Hel l omessagesent . \ n" ) ; n=r ecv f r om( sockf d, ( char* ) buf f er , MAXLI NE, MSG_ WAI TALL, ( st r uctsockaddr* )&ser v addr , &l en) ;
}
buf f er [ n]=' \ 0' ; pr i nt f ( " Ser v er: %s\ n" , buf f er ) ; cl ose( sockf d) ; r et ur n0;
Out put :