This is a creation in
Article, where the information may have evolved or changed.
There is a computer network experiment, need to simulate the process of GBN, that is, under the local conditions simulated packet loss retransmission mechanism. Just saw this experiment was a bit overwhelmed, because do not know how to implement packet loss, in the local programming, even using unreliable data transmission UDP datagramsocket, because localhost to localhost is the loopback link, That is, from the client's application layer-the Transport layer-the network layer to the server's network layer-the Transport layer-the application layer, this must not be lost packets, but also with experiments to verify (such as the continuous transmission of 10,000 datagrampacket, found that each has been received).
So the question is, how can I drop the bag?
Through the previous narrative, it is impossible to lose packets on the link, that is, we can start from the server, that is, even if the server received a packet, and do not send an ACK to the client, then to the client set the timer, the client is naturally considered to be lost packets. So you can set a packet drop probability to the server, implemented with the Math.random () function, which returns a random number from 0 to 1, which can be used to set the "packet loss" probability. If you set the packet drop probability to 0.3:
if (Math.random () <=0.7) { //Receive successful, send ack;} else{ //hehe, do nothing}
"Packet loss" problem solved, the following is how to send a packet, with the previous learned TCP socket line, with UDP datagramsocket also line, this experiment using UDP programming bar, so, the next question is, how to use UDP programming.
Also need socket (do not understand what the socket is to see the previous articles), but this time is Datagramsocket, represents the UDP datagram socket. Datagramsocket to send out packets, that is, Datagrampacket. For example, a client can send data to the server and display it on the server.
The client code is as follows:
public class UdpClient {public static void Main (string[] args) throws Exception { //create UDP socket Datagramsocket socket = new Datagramsocket (); Obtain the IP address of the server via the domain name inetaddress serveraddress = inetaddress.getbyname ("localhost"); String str = "Hello Server, I am Client"; You need to convert the sent string into a byte array in order to transmit byte[] data = Str.getbytes () in the Internet; New packet, indicating the sending data content, the length of the send, the server address, the server port number datagrampacket packet = new Datagrampacket (data, Data.length, ServerAddress, 8899); Send packet socket.send (packet) using the socket;} }
The corresponding server-side code is as follows:
public class Udpserver {public static void Main (string[] args) throws Exception { //new server-side socket with simultaneous setting of the listening port Datagramsocket socket = new Datagramsocket (8899); while (true) { byte[] data = new byte[1024]; A new packet will be placed in the data byte array, with a maximum length of data.length datagrampacket packet = new Datagrampacket (data, data.length); This method is blocking (block method) until the packet is heard and is not executed down, otherwise it waits, just like the Serversocket.accept () method socket.receive (packet); Execute here to illustrate that the monitor hears the packet and converts the contents into string result = new String (Packet.getdata ()); Print System.out.println ("Receive result:" + result);}}}
Be sure to run the server side first and then run the client. The results of the operation are as follows:
If you want the server to send data to the client, you can do the same.
Use datagramsocket send datagrampacket problem solved, then the next problem, in the GBN protocol, after the client sends a data to set the timer, this timer how to set?
In Java there are two ways to set the timer, first of all, the first method, using a timer combined with ActionListener can be.
The code is as follows, it is worth noting that the timer to import java.swing in this package, not java.util the package, otherwise it will not run correctly.
Import java.awt.event.ActionEvent; Import Java.awt.event.actionlistener;import Javax.swing.timer;public class Timermain {public static void main ( String[] (args) { //Set the timer, the first parameter is the delay execution time, the unit is milliseconds, the second parameter is the action after the time, the type is ActionListener Timer timer = new Timer (3000, New Delayactionlistener ()); This must be written in order to make the timer effective timer.start (); In order not to let the program end, add a dead loop, or the timer timer has not yet arrived, the main program is over while (true) { } }}//Custom action class implementation ActionListener interfaces class Delayactionlistener implements actionlistener{ //This is a callback method that automatically executes the Actionperformed method when the time is up @Override public void actionperformed (ActionEvent e) { System.out.println ("Timer time to");} }
The results of the operation are as follows: every 3 seconds. If you need to cancel the timer, use the Timer.stop () method.
The second way to set the timer is to use a timer combined with TimerTask, which is very common in Android, which is actually Java stuff, the code is as follows:
Import Java.util.Timer; Import Java.util.timertask;public class Timertaskmain {public static void Main (string[] args) { //Use timertask< C2/>timertask timertask = new TimerTask () { @Override public void Run () { System.out.println (" TimerTask timer time to "); } ; Timer timer = new timer (); The first parameter is the target to be executed, the second parameter is the time of the first execution delay, the third parameter is the time interval for the loop execution, the units are in milliseconds timer.schedule (timertask, x, n);} }
The results of the operation are as follows: every 3 seconds. If you need to cancel the timer, use the Timertask.cancel () method.
With these things, and then combined with the principle of GBN and their own control of the logic operation, write GBN should be able to achieve, do not paste the complete code, their own implementation of the impression deeper engraved.
Update--------
Many people tell me that your GBN program has no source code .... Although there is no post-source, but I gave the GitHub link, go in and look for it, it is obvious that the GBN at the beginning of the warehouse is, must learn to use GitHub ah ... Help me to order a few ' star ', if it's convenient.
personal GitHub: http://github.com/icodeu
Personal website: www.icodeyou.com