Java TCP uses sockets for network communication (3)

Source: Internet
Author: User

Author: Ching

Original address: http://blog.csdn.net/qingdujun/article/details/39322051

This article demonstrates that TCP uses the socket for network communication, establishes a simple client, and uses the client to send a message to the server, which is then printed on the console after the service has received it.

1) The client sends a message to the server.

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

Client, the code is as follows:

[Java]View Plaincopyprint?
  1. Package TCP.CLINET.QDJ;
  2. Import Java.io.OutputStream;
  3. Import Java.net.Socket;
  4. Client of TCP Communication
  5. Public class Ctcpclient {
  6. public static void Main (string[] args) throws Exception {
  7. //Create Client Socket service, specify destination host and port.
  8. Socket s = new socket ("127.0.0.1",6877);
  9. SYSTEM.OUT.PRINTLN ("Client has established a link ...");
  10. //In order to send data, you should get the output stream in the socket stream.
  11. OutputStream out = S.getoutputstream ();
  12. Out.write ("TCP data Comes ...".)  GetBytes ());
  13. //Close Resources
  14. S.close ();
  15. }
  16. }

Service side, the code is as follows:

[Java]View Plaincopyprint?
  1. Package TCP.SERVER.QDJ;
  2. Import Java.io.InputStream;
  3. Import Java.net.ServerSocket;
  4. Import Java.net.Socket;
  5. Service side of TCP communication
  6. Public class Cserver {
  7. public static void Main (string[] args) throws exception{
  8. //Set up the server socket service and listen to a port.
  9. ServerSocket ss = New ServerSocket (6877);
  10. //Get the client object from the link through the Accept method
  11. SYSTEM.OUT.PRINTLN ("Server is started, listening on port 6876, waiting for data ...");
  12. Socket s = ss.accept ();
  13. //Get an IP address
  14. String IP = s.getinetaddress (). gethostaddress ();
  15. System.out.println (ip+".... Connected");
  16. //Get the data sent by the client, then use the read stream of the client's object to read the data .
  17. InputStream in = S.getinputstream ();
  18. byte[] buf = new byte[1024];
  19. int len = In.read (BUF);
  20. System.out.println (new String (buf,0,len));
  21. //Close client
  22. S.close ();
  23. //Close the service side (optional)
  24. Ss.close ();
  25. }
  26. }

It is important to note that TCP is a connection-oriented data transfer, so you need to start the server side before you open the client to successfully connect ....

In addition, I write the IP address as my actual network IP address, the occurrence of what data are not accepted. Change to 127.0.0.1 is no problem, to be solved.

Effect Show:

1) Start the service side

2) Start the client and send the data

3) server side, receipt after acceptance ...

References: Java video Bi Xiangdong presenter

Original address: http://blog.csdn.net/qingdujun/article/details/39322051

Java TCP uses sockets for network communication (3)

Related Article

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.