Java Socket TCP/IP simple example

Source: Internet
Author: User

Below is the server side Code :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; <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/> 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/> Public static void main (string [] ARGs) throws exception {<br/> tcpserver Server = new tcpserver (); <br/> while (true) {<br/> server. service (); <br/>}</P> <p >}< br/>

The following is the client codePackage EX3; <br/> // 200532580182 pengxu <br/> Import java.net. *; <br/> Import Java. io. *; <br/> public class tcpclient {<br/> // The client socket <br/> private 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/> 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.