Java UDP Group chat version with socket for Web chat (2)

Source: Internet
Author: User
Tags ip number

Author: Ching

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

This article shows that the use of the socket network chat group chat, the implementation of the client to the "LAN" network segment inside the machine to send a broadcast, when the "886" is sent, 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.

3) client + server version, to implement a multi-threaded chat program that can send data and accept data.

Some points to note are: 1) Here is a broadcast to the LAN. 2) LAN broadcast address: 192.168.1.255 3) If the popup exception in thread "main" java.net.BindException:Address the already in use:cannot bind problem, please Change a port number (the port number has not been released).

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 ("192.168.1.254"),9527);
  21. //Send data
  22. Ds.send (DP);
  23. }
  24. //Close Resources
  25. Ds.close ();
  26. }
  27. }

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. }

Third, synthesize the above, write a multi-line Cheng Chat program that can be accepted and can be sent:

[Java]View Plaincopyprint?
  1. Package UDP.CHAT.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. Send Side
  8. Class Send implements Runnable
  9. {
  10. Private Datagramsocket ds;
  11. Public Send (datagramsocket s)
  12. {
  13. DS = S;
  14. }
  15. public Void Run () {
  16. try {
  17. //input from keyboard
  18. BufferedReader bufr = new BufferedReader (new InputStreamReader (system.in));
  19. String line = null;
  20. While ((Line=bufr.readline ()) =null)
  21. {
  22. if ("886". Equals (line))
  23. Break ;
  24. byte[] buf = Line.getbytes ();
  25. //package data, send to 5656 ports//LAN broadcast address: 192.168.1.255
  26. Datagrampacket DP = new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("10.100.56.210"),5656);
  27. //Send data
  28. Ds.send (DP);
  29. }
  30. } catch (Exception e) {
  31. throw New RuntimeException ("Send side failed");
  32. }
  33. }
  34. }
  35. Receiving end
  36. Class Rece implements Runnable
  37. {
  38. Private Datagramsocket ds;
  39. Public rece (datagramsocket s)
  40. {
  41. DS = S;
  42. }
  43. public Void Run () {
  44. try {
  45. While (true)
  46. {
  47. byte[] buf = new byte[1024];
  48. //define a packet
  49. Datagrampacket DP = new Datagrampacket (buf, buf.length);
  50. //Accept data Packets
  51. Ds.receive (DP);
  52. //Extract Data
  53. String IP = dp.getaddress (). gethostaddress ();
  54. String data = new String (Dp.getdata (),0,dp.getlength ());
  55. System.out.println (ip+":" +data);
  56. }
  57. } catch (Exception e) {
  58. throw New RuntimeException ("receiver failed");
  59. }
  60. }
  61. }
  62. Public class Cudpchat {
  63. public static void Main (string[] args) throws exception{
  64. //Send side can not specify a port, the system randomly assigns a
  65. Datagramsocket sendsocket = new Datagramsocket ();
  66. Datagramsocket recesocket = new Datagramsocket (5656);
  67. //Turn on multithreading
  68. New Thread (new Send (Sendsocket)). Start ();
  69. New Thread (new Rece (Recesocket)). Start ();
  70. }
  71. }

Display interface:


References: Java video Bi Xiangdong presenter

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

Java UDP Group chat version with socket for Web chat (2)

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.