0
我有用Java編寫的簡單Web客戶端,我必須計算它發送和接收的字節數。然後我必須將結果與netstat -s命令進行比較。另外,我如何測量我發送和接收的數據包的平均大小。 這裏是WebClient.java:計算Java Web客戶端發送和接收的字節數
package javaapplication1;
/**
*
* @author
*/
import java.net.*;
import java.io.*;
import java.util.*;
public class WebClient {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter host name (e.g., www.ouhk.edu.hk): ");
String host = scanner.nextLine();
System.out.print("Enter page (e.g., /index.html): ");
String page = scanner.nextLine();
final String CRLF = "\r\n"; // newline
final int PORT = 80; // default port for HTTP
try {
Socket socket = new Socket(host, PORT);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
PrintWriter writer = new PrintWriter(os);
writer.print("GET " + page + " HTTP/1.1" + CRLF);
writer.print("Host: " + host + CRLF);
writer.print(CRLF);
writer.flush(); // flush any buffer
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null){
System.out.println(line);
}
System.out.println("Recieved bytes:");
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
u能提供線索,以實現它=)) – Nate
這裏是鏈接到這個類的Java文檔:http://commons.apache.org/ io/api-release/org/apache/commons/io/input/CountingInputStream.html這是雅加達公共區域的一部分 – AlexR
pacakge不存在包org.apache.commons.io.input; – Nate