Rdt3.0 sender Program (Java implementation)

Source: Internet
Author: User

This is a reference to the code pai_^ given by the teacher. It is much more friendly than the C implementation in the early morning .. And I personally feel clearer...

Import javax. Swing .*;
Import java. AWT. event .*;
Import javax. Swing. timer;

Public class sender
{
Public static void main (string [] ARGs)
{
String input;
Int counter = 0;
Int configure;

/* Enter the data to be sent */
Input = joptionpane. showinputdialog ("input the sending data! ");
/* Register the corresponding clock listener for the sent data packet */
Timer atimer = new timer (5000, new senderlistener (input, counter % 2 ));
/* Start the clock listener */
Atimer. Start ();

Do
{
/* Information about the recipient */
Configure = answertosender ();
While (true)
{
/* If the packet has been successfully received, terminate the corresponding clock listener.
/* Otherwise, the data packet resends and the clock listener continues to listen */
If (configure = counter % 2)
{
Atimer. Stop ();
Break;
}

Else configure = answertosender ();
}
Input = joptionpane. showinputdialog ("continue? (Y/n )");
If (input. Equals ("N") | input. Equals ("N") break;

Input = joptionpane. showinputdialog ("input the sending data! ");
Atimer = new timer (5000, new senderlistener (input, ++ counter % 2 ));
Atimer. Start ();
} While (true );

System. Exit (0 );
}
 
Public static int answertosender ()
{
String input = joptionpane. showinputdialog ("response to sender (0/1 )");
Return integer. parseint (input );
}
}

/* Implement the custom class of the actionlistener listener interface, which must be implemented
The actionreceivmed (actionevent) method in the actionlistener interface.
The method is automatically called when the time interval reaches your defined time interval */
Class senderlistener implements actionlistener
{
Private string series;
Private int sequencenumber;

/* Constructor, called during object initialization */
Public senderlistener (string series, int counter)
{
This. Series = series;
Sequencenumber = counter;
System. Out. println ("the datas: '" + series + "'have been sended! /N"
+ "" + "And its sequence number is:" + sequencenumber + "/N ");
}
 
Public void actionreceivmed (actionevent aevent)
{
System. Out. println ("the datas: '" + series + "'cieved failurely! ");
Sendagain ();
}
 
Public void sendagain ()
{
System. Out. println ("the datas: '" + series + "'have been sended again! /N ");
}
}

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.