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 ");
}
}