Java uses the socket for network Chat (2) Group Chat Edition

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.

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 ();// The data is packaged and sent to the 9527 port of the Datagrampacket DP = new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("192.168.1.254"), 9527);//Send data Ds.send (DP);} Close resource 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.

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];//defines a packet datagrampacket DP = new Datagrampacket (buf, buf.length);//Accept Packet 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:

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 End class send implements Runnable{private Datagramsocket ds;public Send (datagramsocket s) {ds = S;} public void Run () {try {//from keyboard input 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 5656 port//LAN broadcast address of the machine: 192.168.1.255DatagramPacket 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");}}} Receive 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];//defines a packet datagrampacket DP = new Datagrampacket (buf, buf. length);//Accept Packet 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 ();D atagramsocket 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 uses the socket for network Chat (2) Group Chat Edition

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.