Java UDP uses the socket for network chat (1)

Source: Internet
Author: User
Tags ip number

Author: Ching

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

This article demonstrates that using a socket for a network chat, the implementation of the client to the server side has been sending messages, when sending "886", indicating that the client shutdown.

1) client, send data to the server, send "886" to shut down the client.

2) The server, has been listening to 9527 ports, the monitoring of the data printed on the console.

First, the client: the main use of the input from the keyboard, and when the "886" packet is sent, close the client.

[Java]View Plaincopyprint?
  1. Package UDP.SEND2.QDJ;
  2. Import Java.io.BufferedReader;
  3. Import Java.io.InputStreamReader;
  4. Import Java.net.DatagramPacket;
  5. Import Java.net.DatagramSocket;
  6. Import java.net.InetAddress;
  7. Public class CUdpSend2 {
  8. public static void Main (string[] args) throws Exception {
  9. //Send data from native 1234 Port
  10. Datagramsocket ds = new Datagramsocket (1234);
  11. //input from keyboard
  12. BufferedReader bufr = new BufferedReader (new InputStreamReader (system.in));
  13. String line = null;
  14. While ((Line=bufr.readline ()) =null)
  15. {
  16. if ("886". Equals (line))
  17. Break ;
  18. byte[] buf = Line.getbytes ();
  19. //Package The data and send it to the 9527 port on the machine
  20. Datagrampacket DP = new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("10.100.56.210"),9527);
  21. //Send data
  22. Ds.send (DP);
  23. }
  24. //Close Resources
  25. Ds.close ();
  26. }
  27. }

Client Display effect:

Second, the server side, the main is always listening to 9527 ports, and the monitoring of the data to print to the console.

1) The console can be dragged and released, if need to put in the "minimize" to the left of the bottom of the option is OK.

2) Switch the console, you can click on the "minimize" to the left of the 2nd option.

[Java]View Plaincopyprint?
  1. Package UDP.RECE2.QDJ;
  2. Import Java.net.DatagramPacket;
  3. Import Java.net.DatagramSocket;
  4. Public class CUdpRece2 {
  5. public static void Main (string[] args) throws exception{
  6. //Monitor 9527 Port
  7. Datagramsocket ds = new Datagramsocket (9527);
  8. While (true)
  9. {
  10. byte[] buf = new byte[1024];
  11. //define a packet
  12. Datagrampacket DP = new Datagrampacket (buf, buf.length);
  13. //Accept data Packets
  14. Ds.receive (DP);
  15. //Extract Data
  16. String IP = dp.getaddress (). gethostaddress ();
  17. String data = new String (Dp.getdata (),0,dp.getlength ());
  18. SYSTEM.OUT.PRINTLN ("IP number:" +ip+"\ n data:" +data);
  19. }
  20. }
  21. }

Server side, display effect:

References: Java video Bi Xiangdong presenter

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

Java UDP uses the socket for network chat (1)

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.