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

📄 datagramclient.txt

📁 Icarnegie university SSD8电子书和全部练习答案
💻 TXT
字号:
import java.io.*;
import java.net.*;
import java.util.*;

/**
* @author MABO
* @version 1.0
*/
class DatagramClient {
private static int port = 8080;

/** 
* The define of the ip
*/
private static InetAddress IpAddress;

/** 
* The total of the count number
*/
private static int totalCounter = 1000;

/** 
* The number of the lost packets
*/
private static int lostCounter = 0;

private static byte[] sendData = new byte[1];

private static byte[] recieveData = new byte[1];

/** 
* begin timer 
*/
private static Date begin;

/**
* end timer 
*/
private static Date end;

private static DatagramSocket clientSocket;

/**
* 
* @return the received number
* @throws Exception
*/
public static byte sendPacket(byte[] bytes) throws Exception {
DatagramPacket sendPacket = new DatagramPacket(bytes, bytes.length,
IpAddress, port);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(recieveData,
recieveData.length);
clientSocket.receive(receivePacket);
recieveData = receivePacket.getData();
return recieveData[0];
}

/**
* @throws Exception
*/
public static void main(String args[]) throws Exception {
PrintWriter screen = new PrintWriter(System.out, true);
/**
* check the number of the arguments 
*/
if (args.length != 1) {
screen.println("Please enter the port numbet!");
System.exit(0);
}
clientSocket = new DatagramSocket();
BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
port = Integer.parseInt(args[0]);
screen.println("Please input your IpAddress:");
String IP = "";
IP = keyboard.readLine();
IpAddress = InetAddress.getByName(IP);
int i = 0;
byte message = 0;
/**
* set the begin
*/
begin = new Date();
while (i < totalCounter) {
sendData[0] = message;
byte recieveByte = sendPacket(sendData);
if (recieveByte != message) {// if the packet is lost send again
lostCounter += 1;
recieveByte = sendPacket(sendData);
}
message++;
i++;
}
/** 
* set the end 
*/
end = new Date();
screen.println("The average time of the round rip time is : "
+ ((double) (end.getTime() - begin.getTime()) / 1000.0));
screen.println("The number of the lost and deplicated packets is : "
+ lostCounter);
}
}








⌨️ 快捷键说明

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