Java------Socket Series (iii)

Source: Internet
Author: User

Exercise: UDP Chat Program

Get the information you want to send via keyboard entry.
Package the send and receive separately into two threads.


Chat Side One:

<span style= "FONT-SIZE:14PX;" >package cn.hncu.url.udp;import java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;import Java.net.socketexception;import Com.sun.imageio.plugins.common.InputStreamAdapter; public class Udpchat {public static void main (string[] args) {try {datagramsocket send=new datagramsocket (10001);D Atagram Socket receive=new Datagramsocket (10002), new thread (new Send), start (), new thread (new receive). Start () ;} catch (SocketException e) {e.printstacktrace ();}}} Class Send implements Runnable{private datagramsocket ds=null;public Send (datagramsocket ds) {this.ds=ds;} @Overridepublic void Run () {BufferedReader br=new BufferedReader (New InputStreamReader (system.in)); String Line=null;try {while ((Line=br.readline ())!=null) {byte buf[]=line.getbytes ();// The following IP address I said with xxx.xxx.xxx.xxx, here to use your own IP address, the runtime remember to change DatagrampacketDp=new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("</span><span style=" font-family:arial, Helvetica, Sans-serif; " ><span style= "FONT-SIZE:14PX;" >xxx.xxx.xxx.xxx</span></span><span style= "font-family:arial, Helvetica, Sans-serif;" ><span style= "FONT-SIZE:14PX;" > "), 10004); </span></span><span style=" FONT-SIZE:14PX; " >ds.send (DP), if (Line.equalsignorecase ("over")) {break;}} Ds.close ();} catch (IOException e) {e.printstacktrace ();}}} Class Receive implements Runnable{private datagramsocket ds=null;public Receive (datagramsocket ds) {this.ds=ds;} @Overridepublic void Run () {byte buf[]=new byte[1024];try {while (true) {Datagrampacket dp=new datagrampacket (buf, Buf.length);d S.receive (DP), Byte Data[]=dp.getdata (); String ip=dp.getaddress (). gethostaddress (); String Info=new string (data); SYSTEM.OUT.PRINTLN (ip+ "says" +info), if (Info.equalsignorecase ("over")) {System.out.println (ip+ "have left the chatting ... ");}} catch (ioexceptIon e) {e.printstacktrace ();}}} </span>
Chat Side Two:

Package Cn.hncu.url.udp.chat2;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import Java.io.inputstreamreader;import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;import Java.net.socketexception;import Com.sun.imageio.plugins.common.InputStreamAdapter; public class UDPChat2 {public static void main (string[] args) {try {datagramsocket send=new datagramsocket (10003);D Atagra Msocket receive=new Datagramsocket (10004), new thread (new Send), start (), new thread (new receive). Start ( );} catch (SocketException e) {e.printstacktrace ();}}} Class Send implements Runnable{private datagramsocket ds=null;public Send (datagramsocket ds) {this.ds=ds;} @Overridepublic void Run () {BufferedReader br=new BufferedReader (New InputStreamReader (system.in)); String Line=null;try {while ((Line=br.readline ())!=null) {byte buf[]=line.getbytes ();// The following IP address I use xxx.xxx.xxx.xxx to say, here to use your own IP address, the runtime remember to change over Datagrampacket dp=new datagrampacket (buf, BUF.length,inetaddress.getbyname ("<span style=" font-family:arial, Helvetica, Sans-serif; " >xxx.xxx.xxx.xxx</span><span style= "font-family:arial, Helvetica, Sans-serif;" > "), 10002), </span>ds.send (DP), if (Line.equalsignorecase (" over ")) {break;}} Ds.close ();} catch (IOException e) {e.printstacktrace ();}}} Class Receive implements Runnable{private datagramsocket ds=null;public Receive (datagramsocket ds) {this.ds=ds;} @Overridepublic void Run () {byte buf[]=new byte[1024];try {while (true) {Datagrampacket dp=new datagrampacket (buf, Buf.length);d S.receive (DP), Byte Data[]=dp.getdata (); String ip=dp.getaddress (). gethostaddress (); String Info=new string (data); SYSTEM.OUT.PRINTLN (ip+ "says" +info), if (Info.equalsignorecase ("over")) {System.out.println (ip+ "have left the chatting ... ");}} catch (IOException e) {e.printstacktrace ();}}}


Java------Socket Series (iii)

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.