Rdt3.0 sender and receiver Program (C implementation)

Source: Internet
Author: User
Tags ack

This is something I used in my network theory experiment. I shared it with everyone. ^_^ (it seems that some of them are not right. If they are useful, please pay attention to it)

Sender:

# Include <stdio. h>
Void sendata0 ();
Void start ();
Void waitack ();
Void retrans1 ();
Void retrans0 ();
Void sendata1 ();
Void main ()
{
Int;
Int B;
Int C;
Int D;
Start (); // start the program
Return;
}
Void start (){
Int;
While (! = 1 &! = 2)
{
Printf ("******************************/N "); // initial Page
Printf ("Welcome to RDT 3.0 sender demo/N ");
Printf ("1: send data 2: Exit demo/N ");
Printf ("Enter your choice :");
Scanf ("% d", & );
Printf ("************************************* * *******************/N ");
}
If (A = 1)
Sendata0 (); // send data with a serial number of 0
If (A = 2)
Exit (0); // exit the program
}

//************************************** *****************************
Void sendata0 () // send a packet whose serial number is 0
{
Int B;
While (B! = 1 & B! = 2)
{
Printf ("Send a packet with the serial number 0 now. Are you sure you want? /N ");
Printf ("1: OK 2: Exit ");
Scanf ("% d", & B );
Printf ("************************************* * *******************/N ");
}
If (B = 1)
Waitack (); // wait for the ACK message to be received
If (B = 2)
Start (); // return the Start Program

}

Void waitack (){
Int C;
While (C! = 1 & C! = 2 & C! = 3 & C! = 4 ){
Printf ("packets with serial number 0 have been sent !! The timer has been started:-)/n ");
Printf ("select event/N ");
Printf ("1: ack0 received 2: ack1 received 3: ACK 4 received error: Timeout !! /N ");

Scanf ("% d", & C );
Printf ("************************************* * *******************/N ");
}
If (C = 2 | C = 3 | C = 4)
Retrans0 (); // resend the data packet with the serial number 0;
If (C = 1)
Sendata1 (); // send a data packet with the serial number of 1;

}

Void retrans0 (){
Printf ("packets with serial numbers 0 will be ressent/N ");
Waitack (); // wait for ACK
}

Void sendata1 (){
Int D;
Printf ("the packet whose serial number is to be sent now/N ");
While (D! = 1 & D! = 2 & D! = 3 & D! = 4)
{
Printf ("packets with serial number 1 have been sent and are waiting for a response/N ");
Printf ("1: ack1 received 2: ack0 received 3: timeout! 4: receive error message/N ");
Printf ("select event ");
Scanf ("% d", & D );
Printf ("************************************* * *******************/N ");
}
If (D = 2 | D = 3 | D = 4)
Retrans1 (); // re-transmits the first packet
If (D = 1)
Sendata0 (); // return to the data packet No. 0 to be sent

}
Void retrans1 (){
Printf ("packets whose serial number is to be ressent/N ");
Sendata1 (); // resend the data packet No. 1
}

Aggreger:

# Include <stdio. h>
Void start ();
Void recv0 ();
Void recall0 ();
Void sendack1 ();
Void recall1 ();
Void sendack0 ();
Void recv1 ();

Void main (){
Int;
Int B;
Int C;
Int D;
Int E;
Start (); // Start Screen
Return;
}
Void start (){
Int;
While (! = 1 &! = 2 ){
Printf ("*******************************/N ");
Printf ("welcome to the RDT 3.0 receiver demo !!! /N ");
Printf ("1: Enter the received DEMO 2: Exit/N ");
Printf ("Enter your choice ");
Scanf ("% d", & );
Printf ("*********************************/N" );
}
If (A = 1)
Recv0 (); // enters the status of receiving packet 0
If (A = 2)
Exit (0); // exit the program

}
Void recv0 (){
Int B;
While (B! = 1 & B! = 2 & B! = 3 ){
Printf ("the sender has sent a packet with the serial number 0/N ");
Printf ("select event:/N ");
Printf ("1: receive the correct data 2: receive the wrong data 3: timeout/N ");
Scanf ("% d", & B );
Printf ("*******************************/N ");
}
If (B = 1)
Sendack1 (); // connect to the received data packet for confirmation
If (B = 2 | B = 3)
Recall0 (); // send the message so that the sender retransmits it.
}

Void sendack1 (){
Int C;
While (C! = 1 & C! = 2 ){
Printf ("packets with serial number 0 have been correctly received !! /N ");
Printf ("select an event! /N ");
Printf ("1: Send ack0 2: Quit/N ");
Scanf ("% d", & C );
Printf ("*****************************/N ");
}
If (C = 1)
Recv1 (); // changes to the status of waiting for receiving data packets whose serial number is 1
If (C = 2)
Start (); // return the start status
}
Void recall0 (){
Printf ("ack1 needs to be sent to the sender !! /N ");
Printf ("ack1 has been sent !!! /N ");
Printf ("****************************/N ");
Recv0 ();
}

Void recv1 (){
Int D;
While (D! = 1 & D! = 2 & D! = 3 ){
Printf ("the sender has sent packet 1 !! /N ");
Printf ("select event/N ");
Printf ("1: receive the correct data 2: receive the wrong data 3: timeout/N ");
Scanf ("% d", & D );
Printf ("*************************/N ");
}
If (D = 1)
Sendack0 ();
If (D = 2 | D = 3)
Recall1 ();
}

Void sendack0 (){
Int E;
While (E! = 1 & E! = 2 ){
Printf ("packets with serial number 1 have been received !! /N ");
Printf ("select event/N ");
Printf ("1: Send ack1 2: Quit ");
Scanf ("% d", & E );
Printf ("*************************/N ");
}
If (E = 1)
Recv0 ();
If (E = 2)
Start ();
}

Void recall1 (){
Printf ("ack0 needs to be sent for sender to re-transmit !! /N ");
Printf ("ack0 has been sent !!! /N ");
Printf ("************************/N ");
Recv1 ();
}
The Code comes from reference materials on hand ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.