Get the information you want to send via keyboard entry.
Package the send and receive separately into two threads.
Package Cn.hncu.UDP;
<span style= "Font-size:32px;color: #ff0000;" >//Sender </span>import java.net.datagrampacket;import java.net.datagramsocket;import java.net.InetAddress; Import Java.net.socketexception;public class Senddemo {public static void main (string[] args) {try {datagramsocket ds = NE W Datagramsocket (9999); String info= "Hunan City College, socket communication information!"; byte buf[] = Info.getbytes (); In the default Code table//datagrampacket class, there is an IP address that is used to create the datagrampacket DP = new Datagrampacket that sends the packet ( BUF, Buf.length, Inetaddress.getbyname ("192.168.1.5"), 10000);//Note that both the IP and the port are the receiver's Ds.send (DP);d s.close ();} catch (Exception e) {e.printstacktrace ();}}}
<span style= "Font-size:32px;color: #ff0000;" >//Receiving Party </span>
<pre name= "code" class= "java" ><span style= "font-size:10px;" ><span style= "color: #ff0000;" >package Cn.hncu.udp;</span>import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.socketexception;public class Receivedemo {public static void main (string[] args) {try {datagramsocket ds = new Da Tagramsocket (10000);//Receive data into DP in byte buf[] = new Byte[1024];//datagrampacket class, no IP address is constructed to create a datagrampacket that receives the packet DP = new Datagrampacket (buf, Buf.length);d s.receive (DP);//After receiving, all the data in the DP//from the DP to resolve the information we want//get ipstring IP = Dp.getaddress (). gethostaddress (); int port = Dp.getport ();//port byte data[] = Dp.getdata ();//data sent by string info = new String (data); System.out.println (ip+ ":" +port+ "----" +info);} catch (Exception e) {e.printstacktrace ();}}} </span><span style= "FONT-SIZE:32PX;" ></span>
<span style= "Font-size:32px;color:rgb (255, 0, 0); >//running and putting 1</span>
<span style= "Font-size:32px;color:rgb (255, 0, 0); ></span>
<span style= "font-size:10px;" >package Cn.hncu.udp;//11111import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;import Java.net.unknownhostexception;public class Udpchat {public static void main (string[] args) {try {datagramsocket send = new Datagramsocket (10001);D atagramsocket receive = new Datagramsocket (10002); new Thread (New S End (send)). Start (); New Thread (new receive). Start (); catch (Exception e) {e.printstacktrace ();}}} Class Send implements Runnable {private Datagramsocket ds;public send (Datagramsocket send) {this.ds = Send;} @Overridepublic void Run () {try {bufferedreader br = new BufferedReader (new InputStreamReader (system.in)); String line = null;while (line = Br.readline ()) = null) {byte buf[] = Line.getbytes ();//To use the receiver's IP and receive port Datagrampacket DP = new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("192.168.1.5"), 10004);d S.send (DP); ("Over". Equals (line)) {break;}} Ds.close ();} catch (Unknownhostexception e) {e.printstacktrace ()} catch (IOException e) {e.printstacktrace ();} catch (Exception e) {E . Printstacktrace ();}}} Class Receive implements Runnable {private Datagramsocket Ds;public receive (Datagramsocket receive) {This.ds = receive;} @Overridepublic void Run () {try {byte buf[] = new byte[1024];//size enough to store one row (true) {//Receive data Datagrampacket DP = new Dat Agrampacket (buf, Buf.length);d s.receive (DP);//parse data string IP = dp.getaddress (). gethostaddress (); String info = new String (Dp.getdata (), 0, Dp.getlength ()); SYSTEM.OUT.PRINTLN (IP + "say" + info "), if (" over ". Equals (Info)) {System.out.println (IP +" leave the chat room .... "); Break;}}} catch (IOException e) {e.printstacktrace ();} catch (Exception e) {e.printstacktrace ()}}} </span>
//Operating Party 2
<pre name= "code" class= "java" ><span style= "font-size:10px;" >package Cn.hncu.udp.chat2;//22222import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;import Java.net.socketexception;import Java.net.unknownhostexception;public class UdpChat { public static void Main (string[] args) {try {datagramsocket send = new Datagramsocket (10003);D atagramsocket receive = new Datagramsocket (10004); new thread (new Send). Start (); New thread (new receive). Start (); catch (Exception e) {e.printstacktrace ();}}} Class Send implements Runnable{private datagramsocket ds;public send (Datagramsocket Send) {this.ds = send;} @Overridepublic void Run () {try {bufferedreader br = new BufferedReader (new InputStreamReader (system.in)); String line = Null;while ((Line=br.readline ())!=null) {byte buf[] = Line.getbytes ();//To use the receiver's IP and receive port datagrampacket DP = NE W Datagrampacket (buf, bUf.length, Inetaddress.getbyname ("192.168.1.5"), 10002);d S.send (DP), if ("over". equals [line)] {break;}} Ds.close ();} catch (Unknownhostexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} catch (Exception e) {e.printstacktrace ();}}} Class Receive implements Runnable{private Datagramsocket Ds;public receive (Datagramsocket receive) {This.ds = receive;} @Overridepublic void Run () {try {byte buf[] = new byte[1024];//size enough to store one row can be while (true) {//Receive data Datagrampacket DP = new Datagra Mpacket (buf, Buf.length);d s.receive (DP);//parse data string IP = dp.getaddress (). gethostaddress (); String info= new String (Dp.getdata (), 0,dp.getlength ()); SYSTEM.OUT.PRINTLN (ip+ "said:" +info), if ("over". Equals (Info)) {System.out.println (ip+ "Out of chat room ....."); catch (IOException e) {e.printstacktrace ();} catch (Exception e) {e.printstacktrace ();}}} </span>
UDP Chat program------java