java socket tcp/ip 簡單樣本

來源:互聯網
上載者:User

下面是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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.