0% found this document useful (0 votes)
57 views5 pages

Simulation of Data Transfer Using TCP With Loss

This document describes a C program that simulates data transfer using TCP with packet loss. It includes code for a server and client. The server accepts data from the user, calculates its length, and transmits it to the client in packets with increasing size based on the congestion window until a specified iteration, when the window is reset. The client receives packets and sends acknowledgements back to the server. The output shows the data being transmitted packet-by-packet from server to client and acknowledgements returned.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views5 pages

Simulation of Data Transfer Using TCP With Loss

This document describes a C program that simulates data transfer using TCP with packet loss. It includes code for a server and client. The server accepts data from the user, calculates its length, and transmits it to the client in packets with increasing size based on the congestion window until a specified iteration, when the window is reset. The client receives packets and sends acknowledgements back to the server. The output shows the data being transmitted packet-by-packet from server to client and acknowledgements returned.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

b October 9 , 2012

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

Description
Initially,the TCP connection is established between the client and the server.The data to be sent to the client is got from the user and the length of the data is calculated.The iteration from which the loss has to occur is also got from the user.The data is transmitted with respect to the increase in size of the congestion window till the particular iteration.From that iteration,the size of the congestion window is again initialised to the intial congestion window size and the data is transmitted.

Figure 1: TCP data transfer [with loss]

Code
Server
/ Program Name : c o n t e n t i o n l o s s 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 / t y p e s . h> #i n c l u d e <s y s / s o c k e t . 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 <arpa / i n e t . h> #i n c l u d e <s t d i o . 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 c l u d e <s t r i n g . h> i n t main ( ) { i n t sock , connected , b y t e s r e c i e v e d , t r u e = 1 ; char send data [ 1 0 2 4 ] , recv data [ 1 0 2 4 ] ; char s t r [ 2 0 0 ] ; i n t l e n g t h , i =0, j , cw=2, i t e r a t i o n , count =1; 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 ( 5 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); } p r i n t f ( \ n S e r v e r Waiting f o r c l i e n t on p o r t 5 0 0 0 ) ; 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 ( send data ) ; f f l u s h ( stdout ) ; l e n g t h=s t r l e n ( s e n d d a t a ) ; p r i n t f ( \ nLength o f t h e data :%d , l e n g t h ) ; send data [ length ]= \0 ; p r i n t f ( \ nEnter t h e i t e r a t i o n : ) ; s c a n f (%d,& i t e r a t i o n ) ; w h i l e ( i <l e n g t h ) { strcpy ( str , ) ; i f ( i t e r a t i o n==count ) cw=cw / 2 ; f o r ( j =0; j <cw ; j ++) { s t r [ j ]= s e n d d a t a [ i ] ; i ++; } cw++; str [ j ]= \0 ; send ( connected , s t r , s t r l e n ( s t r ) , 0 ) ; b y t e s r e c i e v e d = r e c v ( connected , r e c v d a t a , 1 0 2 4 , 0 ) ; recv data [ bytes recieved ] = \0 ; p r i n t f ( \ n r e c e i v e d from c l i e n t :% s , r e c v d a t a ) ; count++; } c l o s e ( connected ) ; } c l o s e ( sock ) ; return 0; }

Client
/ Program Name : c o n t e n t i o n l o s s c l i . 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 i e v e d , count =0; char send data [ 1 0 2 4 ] , recv data [ 1 0 2 4 ] , s t r [ 2 0 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); 3

} 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 ( 5 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) { f f l u s h ( stdout ) ; b y t e s r e c i e v e d=r e c v ( sock , r e c v d a t a , 1 0 2 4 , 0 ) ; recv data [ bytes recieved ] = \0 ; i f ( strcmp ( r e c v d a t a , ) ! = 0 ) { s t r c p y ( s t r , acknowledgement f o r p a c k e t ) ; p r i n t f ( \ nPacket r e c e i v e d :% s , r e c v d a t a ) ; strcat ( str , recv data ) ; p r i n t f ( \ n%s s e n t , s t r ) ; send ( sock , s t r , s t r l e n ( s t r ) , 0 ) ; } } return 0; }

Output
Server
pglab1@pglab1-HCL-Desktop: /Desktop/ranjani$ cc contentionloss ser.c -o ser pglab1@pglab1-HCL-Desktop: /Desktop/ranjani$ ./ser Server Waiting for client on port 5000 Enter the string:hello how are you Length of the data:17 Enter the iteration:3 received from client:acknowledgement for packet he received from client:acknowledgement for packet llo received from client:acknowledgement for packet h received from client:acknowledgement for packet ow received from client:acknowledgement for packet are received from client:acknowledgement for packet you

Client
pglab1@pglab1-HCL-Desktop: /Desktop/ranjani$ cc contentionloss cli.c -o cli pglab1@pglab1-HCL-Desktop: /Desktop/ranjani$ ./cli Packet received:he acknowledgement for packet he sent Packet received:llo acknowledgement for packet llo sent Packet received: h acknowledgement for packet h sent Packet received:ow acknowledgement for packet ow sent Packet received:are acknowledgement for packet are sent

Packet received:you acknowledgement for packet you sent

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

You might also like