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

Simulation of data transfer using TCP without loss Program No.6.

a October 9 , 2012

Aim
To write a C program for simulation of data transfer using TCP without loss..

Description
Initially,the TCP connection is established between the client and the server.For the given string,the length of the string is calculated and the packets are sent with respect to the increase in size of the congestion window.Here,the packets are sent between the client and the server without any loss in any of the iteration.

Figure 1: TCP data transfer [without loss]

Code
Server
/ Program Name : c o n t e n t i o n w i n d o w s e r . c Author : S i v a r a n j a n i .A R o l l No :13 / #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e <s y s / t y p e s . h> <s y s / s o c k e t . h> <n e t i n e t / i n . h> <arpa / i n e t . h> <s t d i o . h> 1

#i n c l u d e < s t d l i b . h> #i n c l u d e <u n i s t d . h> #i n c l u d e <e r r n o . h> #i n c l u d e <s t r i n g . h> #d e f i n e d e l i m i n t main ( i n t argc , c h a r argv [ ] ) { i n t sock , connected , b y t e s r e c e i v e d , t r u e = 1 , l e n =0, i =0, j =0,n , c =0; char send data [ 1 0 2 4 ] , recv data [ 1 0 2 4 ] , s t r [ 1 0 ] , s t r 1 [ 1 0 ] ; struct sockaddr in server addr , client addr ; int sin size ; i f ( ( s o c k = s o c k e t (AF INET , SOCK STREAM, 0 ) ) == 1) { perror ( Socket ) ; exit (1); } i f ( s e t s o c k o p t ( sock , SOL SOCKET,SO REUSEADDR,& t r u e , s i z e o f ( i n t ) ) == 1) { perror ( Setsockopt ) ; exit (1); } s e r v e r a d d r . s i n f a m i l y = AF INET ; s e r v e r a d d r . s i n p o r t = htons ( 6 0 0 0 ) ; s e r v e r a d d r . s i n a d d r . s a d d r = INADDR ANY; b z e r o (&( s e r v e r a d d r . s i n z e r o ) , 8 ) ; i f ( bind ( sock , ( s t r u c t s o c k a d d r )& s e r v e r a d d r , s i z e o f ( s t r u c t s o c k a d d r ) ) == 1) { p e r r o r ( Unable t o bind ) ; exit (1); } i f ( l i s t e n ( sock , 5 ) == 1) { perror ( Listen ) ; exit (1); } f f l u s h ( stdout ) ; while (1) { s i n s i z e = s i z e o f ( struct sockaddr in ) ; c o n n e c t e d = a c c e p t ( sock , ( s t r u c t s o c k a d d r )& c l i e n t a d d r ,& s i n s i z e ) ; p r i n t f ( \ nEnter t h e s t r i n g : ) ; gets ( str ) ; n=a t o i ( argv [ 1 ] ) ; f f l u s h ( stdout ) ; l e n=s t r l e n ( s t r ) ; str [ len ]= \0 ; p r i n t f ( \ n The l e n g t h o f t h e s t r i n g i s %d , l e n ) ; w h i l e ( n<l e n ) { w h i l e ( i <l e n ) { strcpy ( str1 , ) ; f o r ( j =0; j <n ; j ++) { s t r 1 [ j ]= s t r [ i ] ; i ++; } str1 [ j ]= \0 ;

} }

send ( connected , s t r 1 , s t r l e n ( s t r 1 ) , 0 ) ; b y t e s r e c e i v e d=r e c v ( connected , r e c v d a t a , 1 0 2 4 , 0 ) ; recv data [ bytes received ]= \0 ; p r i n t f ( \ nReceived from c l i e n t :% s , r e c v d a t a ) ; n++; p r i n t f ( \ n %d , n ) ;

c l o s e ( connected ) ; } f f l u s h ( stdout ) ; c l o s e ( sock ) ; return 0; }

Client
/ Program Name : c o n t e n t i o n w i n d o w s e r . c Author : S i v a r a n j a n i .A R o l l No :13 / #i n c l u d e <s y s / s o c k e t . h> #i n c l u d e <s y s / t y p e s . h> #i n c l u d e <n e t i n e t / i n . h> #i n c l u d e <netdb . h> #i n c l u d e <s t d i o . h> #i n c l u d e <s t r i n g . h> #i n c l u d e < s t d l i b . h> #i n c l u d e <u n i s t d . h> #i n c l u d e <e r r n o . h> i n t main ( ) { i n t sock , b y t e s r e c e i v e d , p=0; c h a r s e n d d a t a [ 1 0 2 4 ] , r e c v d a t a [ 1 0 2 4 ] , ch [ 5 0 ] , s t r [ 5 0 ] ; s t r u c t hostent host ; struct sockaddr in server addr ; h o s t = gethostbyname ( 1 2 7 . 0 . 0 . 1 ) ; i f ( ( s o c k = s o c k e t (AF INET , SOCK STREAM, 0 ) ) == 1) { perror ( Socket ) ; exit (1); } s e r v e r a d d r . s i n f a m i l y = AF INET ; s e r v e r a d d r . s i n p o r t = htons ( 6 0 0 0 ) ; s e r v e r a d d r . s i n a d d r = ( ( s t r u c t i n a d d r ) host>h addr ) ; b z e r o (&( s e r v e r a d d r . s i n z e r o ) , 8 ) ; i f ( c o n n e c t ( sock , ( s t r u c t s o c k a d d r )& s e r v e r a d d r , s i z e o f ( s t r u c t s o c k a d d r ) ) == 1) { p e r r o r ( Connect ) ; exit (1); } while (1) {

p++; b y t e s r e c e i v e d=r e c v ( sock , r e c v d a t a , 1 0 2 4 , 0 ) ; recv data [ bytes received ]= \0 ; p r i n t f ( \ n Packet %d r e c e i v e d and t h e data i s %s , p , r e c v d a t a ) ; s t r c p y ( ch , Ack f o r p a c k e t : ) ; strcpy ( str , recv data ) ; s t r c a t ( ch , s t r ) ; p r i n t f ( \ n Ack f o r p a c k e t %d s e n t , p ) ; send ( sock , ch , s t r l e n ( ch ) , 0 ) ; } c l o s e ( sock ) ; return 0; }

Output
Server
pglab1@pglab1-HCL-Desktop: /Desktop/ranjani$ cc contentionwindow client.c -o contentionclient pglab1@pglab1-HCL-Desktop: /Desktop/ranjani$ ./contentionclient Packet 1 received and the data is wel Ack for packet 1 sent Packet 2 received and the data is come Ack for packet 2 sent Packet 3 received and the data is to Ack for packet 3 sent

Client
pglab1@pglab1-HCL-Desktop: /Desktop/ranjani$ cc contentionwindow server.c -o contentionserver pglab1@pglab1-HCL-Desktop: /Desktop/ranjani$ ./contentionserver 3 Enter the string:welcome to The length of the string is 10 Received from client:Ack for packet:wel Received from client:Ack for packet:come Received from client:Ack for packet: to

Result
Thus the c program for simulating data transfer using TCP without loss was done and the output was veried.

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