Java scoket (UDP communication model) simple chat room

Source: Internet
Author: User

ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;/*** Receive thread * *@authorXiaoduc **/ Public classReceivethreadextendsThread {PrivateDatagramsocket ds;  PublicReceivethread (intPort) {        Super(); Try {             This. ds =NewDatagramsocket (port); } Catch(Exception e) {e.printstacktrace (); }} @Override Public voidrun () {Try {            //Create a byte array            byte[] buff=New byte[1024]; //Create a Packet objectDatagrampacket dp=NewDatagrampacket (Buff, 1024); //Perpetual Cycle             while(true) {ds.receive (DP);//Receiving MessagesString str=NewString (Dp.getdata (), 0,dp.getlength ());//Get the dataSystem.out.println ("Receiving:" +STR);//Print Messages            }        } Catch(Exception e) {e.printstacktrace (); }finally{ds.close (); }    }}

ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress;/*** Send Thread class *@authorXiaoduc **/ Public classSendthreadextendsthread{PrivateDatagramsocket ds; Private intSendPort;  PublicSendthread (intPortintSendPort) {        Super();  This. sendport=SendPort; Try {             This. ds=NewDatagramsocket (port); } Catch(Exception e) {e.printstacktrace (); }} @Override Public voidrun () {Try {            //loop receive user inputBufferedReader br=NewBufferedReader (NewInputStreamReader (system.in)); String Str=NULL;  while((Str=br.readline ())! =NULL){                //Create a Datagrampacket objectDatagrampacket dp=NewDatagrampacket (Str.getbytes (), 0,str.length (), Inetaddress.getbyname ("localhost"), SendPort); Ds.send (DP);//Send DataSystem.out.println ("Send:" +str);//Print            }        } Catch(Exception e) {e.printstacktrace (); }finally{ds.close (); }    }}

 Public class Chat {    publicstaticvoid  main (string[] args) {        // start send thread        New Sendthread (1091, 1099). Start ();            }}

 Public class Receivechat {    publicstaticvoid  main (string[] args) {        //  start the receive thread        new receivethread (1099). Start ();    }}

Java scoket (UDP communication model) simple chat room

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.