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?
- Package UDP.SEND2.QDJ;
- Import Java.io.BufferedReader;
- Import Java.io.InputStreamReader;
- Import Java.net.DatagramPacket;
- Import Java.net.DatagramSocket;
- Import java.net.InetAddress;
- Public class CUdpSend2 {
- public static void Main (string[] args) throws Exception {
- //Send data from native 1234 Port
- Datagramsocket ds = new Datagramsocket (1234);
- //input from keyboard
- BufferedReader bufr = new BufferedReader (new InputStreamReader (system.in));
- String line = null;
- While ((Line=bufr.readline ()) =null)
- {
- if ("886". Equals (line))
- Break ;
- byte[] buf = Line.getbytes ();
- //Package The data and send it to the 9527 port on the machine
- Datagrampacket DP = new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("192.168.1.254"),9527);
- //Send data
- Ds.send (DP);
- }
- //Close Resources
- Ds.close ();
- }
- }
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?
- Package UDP.RECE2.QDJ;
- Import Java.net.DatagramPacket;
- Import Java.net.DatagramSocket;
- Public class CUdpRece2 {
- public static void Main (string[] args) throws exception{
- //Monitor 9527 Port
- Datagramsocket ds = new Datagramsocket (9527);
- While (true)
- {
- byte[] buf = new byte[1024];
- //define a packet
- Datagrampacket DP = new Datagrampacket (buf, buf.length);
- //Accept data Packets
- Ds.receive (DP);
- //Extract Data
- String IP = dp.getaddress (). gethostaddress ();
- String data = new String (Dp.getdata (),0,dp.getlength ());
- SYSTEM.OUT.PRINTLN ("IP number:" +ip+"\ n data:" +data);
- }
- }
- }
Third, synthesize the above, write a multi-line Cheng Chat program that can be accepted and can be sent:
[Java]View Plaincopyprint?
- Package UDP.CHAT.QDJ;
- Import Java.io.BufferedReader;
- Import Java.io.InputStreamReader;
- Import Java.net.DatagramPacket;
- Import Java.net.DatagramSocket;
- Import java.net.InetAddress;
- Send Side
- Class Send implements Runnable
- {
- Private Datagramsocket ds;
- Public Send (datagramsocket s)
- {
- DS = S;
- }
- public Void Run () {
- try {
- //input from keyboard
- BufferedReader bufr = new BufferedReader (new InputStreamReader (system.in));
- String line = null;
- While ((Line=bufr.readline ()) =null)
- {
- if ("886". Equals (line))
- Break ;
- byte[] buf = Line.getbytes ();
- //package data, send to 5656 ports//LAN broadcast address: 192.168.1.255
- Datagrampacket DP = new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("10.100.56.210"),5656);
- //Send data
- Ds.send (DP);
- }
- } catch (Exception e) {
- throw New RuntimeException ("Send side failed");
- }
- }
- }
- Receiving end
- Class Rece implements Runnable
- {
- Private Datagramsocket ds;
- Public rece (datagramsocket s)
- {
- DS = S;
- }
- public Void Run () {
- try {
- While (true)
- {
- byte[] buf = new byte[1024];
- //define a packet
- Datagrampacket DP = new Datagrampacket (buf, buf.length);
- //Accept data Packets
- Ds.receive (DP);
- //Extract Data
- String IP = dp.getaddress (). gethostaddress ();
- String data = new String (Dp.getdata (),0,dp.getlength ());
- System.out.println (ip+":" +data);
- }
- } catch (Exception e) {
- throw New RuntimeException ("receiver failed");
- }
- }
- }
- Public class Cudpchat {
- public static void Main (string[] args) throws exception{
- //Send side can not specify a port, the system randomly assigns a
- Datagramsocket sendsocket = new Datagramsocket ();
- Datagramsocket recesocket = new Datagramsocket (5656);
- //Turn on multithreading
- New Thread (new Send (Sendsocket)). Start ();
- New Thread (new Rece (Recesocket)). Start ();
- }
- }
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)