Java Network Programming (4)--UDP implementation Chat

Source: Internet
Author: User

UDP can realize the online chat function, I here is a simple simulation:
Send side:

 Packagecom.seven.udp;ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress; Public classCharbyupdsendImplementsRunnable {PrivateDatagramsocket ds;  PublicCharbyupdsend (datagramsocket ds) { This. ds =ds; } @Override Public voidrun () {Try {            //inputBufferedReader BUFR =NewBufferedReader (NewInputStreamReader (system.in)); String Line=NULL;  while(line = Bufr.readline ())! =NULL) {Datagrampacket DP=NewDatagrampacket (Line.getbytes (), Line.length (), Inetaddress.getbyname ("127.0.0.1"),                        10001);            Ds.send (DP); }        } Catch(Exception e) {e.printstacktrace (); }    }}

Receiving end:

 Packagecom.seven.udp;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket; Public classCharbyupdreceiveImplementsrunnable{PrivateDatagramsocket ds;  Publiccharbyupdreceive (datagramsocket ds) { This. ds =ds; } @Override Public voidrun () {Try{             while(true){                byte[] buf =New byte[1024]; Datagrampacket DP=NewDatagrampacket (buf, buf.length);                Ds.receive (DP); String Data=NewString (Dp.getdata (), 0, Dp.getlength ()); System.out.println (dp.getaddress ()+ "-+" +data); }                    }Catch(Exception e) {e.printstacktrace (); Throw NewRuntimeException (e); }    }}

Main program:

 Packagecom.seven.udp;ImportJava.net.DatagramSocket; Public classChardemo { Public Static voidMain (string[] args)throwsException {datagramsocket send=NewDatagramsocket (); Datagramsocket Receive=NewDatagramsocket (10001); NewThread (Newcharbyupdsend (send)). Start (); NewThread (Newcharbyupdreceive (receive)). Start (); }}

Start the receiver first, and then start the sender, so that you can achieve a small chat effect, although this is on the machine, as long as the address and port pairs, so that you can achieve online chat!

Java Network Programming (4)--UDP implementation Chat

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.