下面是server端代碼:package ex3;<br />//200532580182 pengxu<br />import java.io.*;<br />import java.net.*;<br />public class TCPServer {<br />//the server listen socket<br />private ServerSocket serverSocket;<br />// the server connect socket<br />private Socket socket;<br />//listen port<br />private int port=456;<br />//data write to client<br />private DataOutputStream outToClient;<br />//data receive from the client<br />private BufferedReader inFromClient;<br />//the constructor<br />public TCPServer(){<br />try{<br />System.out.println("Server Start: ");<br />//ready to listen port<br />serverSocket=new ServerSocket(port);<br />}catch(Exception e){<br />e.printStackTrace();<br />}<br />}<br />public void service(){</p><p>try{<br />//accept tcp connection<br />socket=serverSocket.accept();</p><p> outToClient=new DataOutputStream(socket.getOutputStream());<br /> inFromClient=new BufferedReader(<br /> new InputStreamReader(socket.getInputStream()));<br /> char[] msg=new char[1];<br /> //read data from the client<br /> inFromClient.read(msg);<br /> //send data to client<br /> outToClient.writeBytes("e");<br /> //close the accept socket<br /> socket.close();</p><p>}catch(Exception e){<br />e.printStackTrace();<br />}<br />}<br />public static void main(String [] args) throws Exception{<br />TCPServer server=new TCPServer();<br />while(true){<br />server.service();<br />}<br />}</p><p>}<br />
下面是client端代碼package ex3;<br />//200532580182 pengxu<br />import java.net.*;<br />import java.io.*;<br />public class TCPClient {<br />//the client socket<br /> private Socket socket;<br /> //tcp connect port<br /> private int port=456;<br />//the round trip time<br /> private long[] rtt;<br /> //server<br /> private String remoteHost="localhost";<br /> //server ip<br /> private InetAddress remoteIP;<br /> //stream write to server<br /> private DataOutputStream outToServer;<br /> //stream receive from the server<br /> private BufferedReader inFromServer;<br /> //constructor<br /> public TCPClient(){<br /> rtt=new long[1000];<br /> try{<br /> //get the host ip address<br /> remoteIP=InetAddress.getByName(remoteHost);</p><p> }catch(UnknownHostException e){<br /> e.printStackTrace();<br /> }<br /> }<br /> public void talk(){<br /> try{<br /> System.out.println("Client Start: ");<br /> System.out.println("Establish TCP connections,please wait... ");<br /> //begin to set 1000 TCP connection and send one byte at each connection<br /> for(int i=0;i<1000;i++){<br /> //record the tcp connect start time<br /> long startTime=System.currentTimeMillis();<br /> //connect to the server<br /> socket=new Socket(remoteIP,port);</p><p> outToServer=new DataOutputStream(socket.getOutputStream());<br /> inFromServer=new BufferedReader(new InputStreamReader(socket.getInputStream()));<br /> String msg="s";<br /> //write data to the server<br /> outToServer.writeBytes(msg);<br /> char[] ch=new char[1];<br /> //receive data from the server<br /> inFromServer.read(ch);<br /> //record the tcp connection finish time<br /> long finishTime=System.currentTimeMillis();<br /> //the rtt time of this tcp connection<br /> rtt[i]=finishTime-startTime;<br /> //close this tco connection<br /> socket.close();<br /> }<br /> }catch(Exception e){<br /> e.printStackTrace();<br /> }<br /> double sum=0;<br /> //computer the average round trip time<br /> for(int j=0;j<1000;j++){<br /> sum+=rtt[j];<br /> }<br /> double rrtTime=sum/1000;<br /> //print the rrt time<br /> System.out.println("rrtTime: "+rrtTime+"ms");<br /> }<br /> public static void main(String [] args) throws Exception{<br />new TCPClient().talk();</p><p>}<br />}</p><p>