?? tcpclient.java
字號:
import java.io.*;
import java.net.*;
import java.util.*;
public class TCPClient {
private Socket client;
private DataInputStream inFromServer;
private DataOutputStream outToServer;
private Date start;
private Date stop;
private byte[] out;
private byte[] in;
private int count;
private int hostPort;
private int lost;
TCPClient() {
hostPort = 8000;
count = 0;
in = new byte[1];
lost = 0;
out = new byte[1];
out[0] = 0;
}
void setUpConnection(String hostIP){
try {
Socket client = new Socket(hostIP, hostPort);
inFromServer = new DataInputStream(client.getInputStream());
outToServer = new DataOutputStream(client.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.println("Error setting up socket connection: unknown host at "
+ hostIP + ":" + hostPort);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error setting up socket connection: " + e);
}
}
void sendMessage(){
start = new Date();
while(count < 1000){
try {
outToServer.write(out);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IO write error!");
}
try {
inFromServer.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IO read error!");
}
if(out[0] == in[0])
count++;
else{
lost++;
System.out.println("the Page lost is : " + out[0]);
}
}
stop = new Date();
double time = stop.getTime() - start.getTime();
System.out.println("The average round trip time is: " + time/1000);
System.out.println("The number of lost packets is: " + lost);
}
void close(){
try {
client.close();
inFromServer.close();
outToServer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
if(args.length != 1){
System.out.println("Only need one argument: hostIP");
}
String hostIP = args[0];
TCPClient client = new TCPClient();
client.setUpConnection(hostIP);
client.sendMessage();
//client.close();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -