Design and implementation of computer network-reliable data transmission protocol-shutdown Protocol

Source: Internet
Author: User
Tags ack

I. Introduction to the implementation of the stop-and-wait protocol

I designed the program to implement the rdt3.0 version of the stop-and-wait protocol, the sender sends packets in the order of 0, 1 alternating, and when the 0 packets begin to be timed, only receive the ACK0 to continue sending 1 packets, if it has timed out without receiving the ack0, This requires retransmission of packet 0;

The receiver returns the corresponding ACK according to the number of packets received, and when the last one receives packet 0, the next only packet 1 can be received into the receiving file, and if the next or packet 0 is discarded.

Second, the grouping format

Datagram Format:

Third, the characteristics of UDP programming

UDP protocol is a connectionless data transmission format, so you don't have to set up a special socket connection like TCP. The specific host transmits the data, but after each packet is packaged, the host and port number of the destination hosts are added to the packet, and no remaining connections are provided.

Therefore, the use of UDP programming is in the form of packets in the Datagramsocket transmission and acceptance, in Java, several packets can be datagrampacket () plus the specified host and port, and then by Datagramsocket () sent out on the line, The corresponding reception is also in the Datagramsocket (* *) to listen to an interface to receive packets, in accordance with the format of their own data packets to extract different parts of the useful information can be.

Iv. the main classes and their functions of experiment realization

Main class:

Server () class: Implement the function of servers, accept packets, parse the effective information output to receive files, return the corresponding packet ack;

Client () class: To implement the Clients function, read the information from the sending file, according to the definition of the datagram combined into a datagram, send packets to the server at the same time, and control timeout, return ACK is not wanted and so on the corresponding treatment of various situations.

In experiments, both the server and the client are started as threads, and their acceptance and sending packets are implemented in their own run () methods.

V. Results of experimental verification

Impersonation loses ACK, there will be a timeout retransmission:

Data transfer completed: Read file for send, receive as receiving file




Package Rdt;
Import java.net.*;

Import java.io.*;
	public class Server extends thread{public static final int max_length = 1024;
	public static datagramsocket socket;	public static int last;
	Number of the last received package public static byte[] receive = new Byte[max_length];
	public static byte[] send = new byte[max_length];
	public static OutputStream WriteFile;
	public static inetaddress inetaddress;
	
	public static int port;
			Public Server () {try {socket = new Datagramsocket (8888);	last = 1; 
			Because the first need is number No. 0 packet WriteFile = new FileOutputStream ("Receive.txt");
		Receive[0] = 1;
		catch (SocketException e) {e.printstacktrace ();
		catch (FileNotFoundException e) {e.printstacktrace (); The public void Run () {int timeOut = 0;//simulate the maximum number of timeouts, then restore while (true) {try{Datagrampacket packet1 = new Data
		Grampacket (receive,receive.length);
		Socket.receive (PACKET1); 
		byte order = receive[0];
		byte need = (byte) ((last==0)? 1:0); SYSTEM.OUT.PRINTLN ("Received packet is:" +order+)Need is: "+need";
			Received is the required packet, then write the file, return ACK if (need = order) {Writefile.write (Receive, 1, packet1.getlength ()-1);
			SEND[0] = need;
			last = order;  
			SYSTEM.OUT.PRINTLN ("Return ack is:" +need);
			InetAddress = Packet1.getaddress ();
			Port = Packet1.getport ();
			SYSTEM.OUT.PRINTLN ("Host name:" +inetaddress.gethostname () + "port:" +port);
			Datagrampacket Packet2 = new Datagrampacket (send,send.length,inetaddress,port); 
			if (timeout++ > 1) {socket.send (Packet2); 
			} else{Send[0] = order; SYSTEM.OUT.PRINTLN ("Sent back the bag is not wanted, discarded.")
			");
			SYSTEM.OUT.PRINTLN ("Return ack is:" +need);
			InetAddress = Packet1.getaddress ();
			Port = Packet1.getport ();
			SYSTEM.OUT.PRINTLN ("Host name:" +inetaddress.gethostname () + "port:" +port);
			Datagrampacket Packet2 = new Datagrampacket (send,send.length,inetaddress,port);
		Socket.send (Packet2);
		} catch (IOException e) {e.printstacktrace (); }
	}
	}
}



Package Rdt;
Import java.net.*;

Import java.io.*; = 3000;
	Set timeout public static byte[] receive = new Byte[max_length];
	public static datagramsocket socket; public static InputStream inputfile = null;
	Read the transfer data from this public static byte order;
	public static inetaddress inetaddress;
	 
	public static int port;
			 Public Client () {try {socket = new datagramsocket ();
			Socket.setsotimeout (TIMEOUT);
			Inputfile = new FileInputStream ("Read.txt");	order = 0;
			Just beginning to send is 0 packet inetaddress = inetaddress.getbyname ("localhost");
		Port = 8888; catch (SocketException |
		FileNotFoundException e) {e.printstacktrace ();
		catch (Unknownhostexception e) {e.printstacktrace (); 
		 } public void Run () {int count = 0;
			Create packet while (true) {int len;
				try {byte[] Sendata = new Byte[max_length]; 
				Sendata[0] = order; Len = Inputfile.read (sendata,1,sendata.length-1);
				count++;
				System.out.println (len);
				if (len = = 1)//file has been sent to the break;
					while (Len!=-1) {try{Datagrampacket packet = new Datagrampacket (sendata,len,inetaddress,port); 
					Socket.send (packet);
					SYSTEM.OUT.PRINTLN ("Send the number of" +count+ "reported");
					Datagrampacket Packet2 = new Datagrampacket (receive,receive.length);
					Socket.receive (Packet2);
					BYTE ack = receive[0];
					SYSTEM.OUT.PRINTLN ("Outgoing package is:" +order+ "sent back the ACK is:" +ack);
						if (ack = = order) {order = (byte) (order==0) 1:0);	Break
					Go to Next turn}//otherwise retransmission}catch (Sockettimeoutexception e) {//timeout, need to retransmit System.out.println ("timeout, retransmission");
			A catch (IOException e) {e.printstacktrace ()); 
 } 
			}
		}
	}


Related Article

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.