Java TCP uses Socket for network communication (4) round-trip Transmission

Source: Internet
Author: User

Java TCP uses Socket for network communication (4) round-trip Transmission

 

This article demonstrates that TCP uses Socket for network communication to send data between the client and the server.

1) the client sends a message to the server and prints the received Server Message on the console.

2) The Server accepts the client message, prints it on the server's start console, and replies to the client to receive the message.

 

The client code is as follows:

 

Package tcp. client2.qdj; import java. io. inputStream; import java. io. outputStream; import java.net. socket; public class CTcpClient2 {public static void main (String [] args) throws Exception {// create the Socket service of the client, specifying the target host and port. Socket s = new Socket (127.0.0.1, 6823); System. out. println (the client has established a link...); // send data, and the output stream in the Socket stream should be obtained. OutputStream out = s. getOutputStream (); out. write (server, hello. getBytes (); // get the data sent from the server, first obtain the input stream InputStream in = s. getInputStream (); byte [] buf = new byte [1024]; // Note: read will block int len = in. read (buf); // print the amount of data sent from the server on the console System. out. println (new String (buf, 0, len); // close resource s. close ();}}
Server, the Code is as follows:

 

 

Package tcp. server2.qdj; import java. io. inputStream; import java. io. outputStream; import java.net. serverSocket; import java.net. socket; public class CTcpServer2 {public static void main (String [] args) throws Exception {// create a server socket service and listen to a port. ServerSocket ss = new ServerSocket (6823); // obtain the linked client Object System through the accept method. out. println (the server is started and listening to port 6823, waiting for data ...); socket s = ss. accept (); // obtain the ip address String ip address = s. getInetAddress (). getHostAddress (); System. out. println (ip + .... connected); // obtain the data sent from the client. Use the read stream of the client object to read the data. InputStream in = s. getInputStream (); byte [] buf = new byte [1024]; // Note: read will block int len = in. read (buf); System. out. println (new String (buf, 0, len); // sends a message to the client to obtain the output stream OutputStream out = s. getOutputStream (); out. write (the server has received it. getBytes (); // close the client s. close (); // closes the server (optional) ss. close ();}}
Note that blocking occurs in the text, which prevents data sending and receiving errors.

 

Running effect:

1) Start the server

2) Start the client

3) at this time, the server is shown as follows:

 

References: Lecture on Java video Bi Xiangdong

Address: http://blog.csdn.net/qingdujun/article/details/39322613

 

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.