⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagramclient.java

📁 美国卡耐基。梅隆大学SSD8课程练习3的答案
💻 JAVA
字号:
/*
 * Created on 2007-5-26
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import java.io.*;
import java.net.*;
import java.util.*;
/**
 * @author Guest
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DatagramClient {
	
	/**
	 * the count of packet
	 */
	private int count;
	
	/**
	 * duplicate packet
	 */
	private int duplicate;
	
	/**
	 * lost count of packet
	 */
	private int lost;
	
	/**
	 * the time before send packet
	 */
	private Date start;
	
	/**
	 * the time while stop send packet
	 */
	private Date stop;
	
	/**
	 * datagramSocket 
	 */
	private DatagramSocket socket;
	
	/**
	 * datagramPacket
	 */
	private DatagramPacket outToServer;
	private DatagramPacket inFromServer;
	
	/**
	 * the byte packet
	 */
	private byte out[] = new byte[1];
	private byte in[] = new byte[1];
	
	/**
	 * byte number
	 */
	protected byte temp;
	
	/**
	 * DatagramClient constructor
	 * <p>initialize </p>
	 */
	DatagramClient(){
		count = 0;
		duplicate = 0;
		lost = 0;
		temp = 0;
		out[0] = temp;	
	}
	
	/**
	 * send packet to server
	 * @param host the hostIP of the server
	 */
	void sendPacket(InetAddress host){
		
		try {
			socket = new DatagramSocket();
		} catch (SocketException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		start = new Date();
		
		for(; count < 1000; out[0] = (byte)(count % 128)){			
			try {
				outToServer = new DatagramPacket(out, out.length, host, 8000);
				inFromServer = new DatagramPacket(in, in.length);
				socket.send(outToServer);
				socket.setSoTimeout(1000);				
				
				socket.receive(inFromServer);
				in = inFromServer.getData();
				temp = in[0];
				if(temp == out[0]){
					count++;
				}else{
					lost++;
				}				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println(e);
			}
		}
		stop = new Date();
	}
	
	/**
	 * 
	 * <p>Print out the average round trip time, 
	 * as well as the number of packets lost and duplicated.</p>
	 */
	void out(){
		double aveTime = (double)(stop.getTime() - start.getTime())/1000;
		System.out.println("The average round trip time is: " + aveTime);
		System.out.println("The number of lost packets is: " + lost);
		//System.out.println("The number of duplicate packets is: " + duplicate);
	}
	
	/**
	 * Close the DatagramSocket
	 *
	 */
	void close(){
		try {
			socket.close();
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		
		if(args.length != 1){
			System.out.println("Only need one argument!");
			return;
		}
		
		DatagramClient client = new DatagramClient();
		
		try {
			InetAddress host = InetAddress.getByName(args[0]);
			client.sendPacket(host);
			client.out();
			client.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -