Java TCP uses Socket for network communication (3)

Source: Internet
Author: User

Java TCP uses Socket for network communication (3)

 

This article demonstrates that TCP uses Socket for network communication, establishes a simple client, and sends a message to the server. After receiving the message, the server prints the data on the console.

1) the client sends a message to the server.

2) The server receives the message from the client and prints it on the console.

 

The client code is as follows:

 

Package tcp. clinet. qdj; import java. io. outputStream; import java.net. socket; // Tcp client public class CTcpClient {public static void main (String [] args) throws Exception {// create the Socket service of the client and specify the target host and port. Socket s = new Socket (127.0.0.1, 6877); System. out. println (the client has established a link...); // The output stream in the Socket stream should be obtained to send data. OutputStream out = s. getOutputStream (); out. write (Tcp data comes... getBytes (); // close resource s. close ();}}
Server, the Code is as follows:

 

 

Package tcp. server. qdj; import java. io. inputStream; import java.net. serverSocket; import java.net. socket; // Tcp Communication Server public class CServer {public static void main (String [] args) throws Exception {// establish the server socket service and listen to a port. ServerSocket ss = new ServerSocket (6877); // obtain the linked client Object System through the accept method. out. println (the server is started and listening to port 6876, 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]; int len = in. read (buf); System. out. println (new String (buf, 0, len); // close the client s. close (); // closes the server (optional) ss. close ();}}
In this article, you must note that Tcp is connection-oriented data transmission. Therefore, you must first start the server and then enable the client to successfully connect to the server .....

 

In addition, when writing, I enter the IP address as my actual network IP address, so that no data can be accepted. If it is changed to 127.0.0.1, no problem will be solved.

Effect display:

1) Start the server

2) Start the client and send data

3) on the server side, after receiving the receipt...

References: Lecture on Java video Bi Xiangdong

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

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.