"Java Network Programming" UDP-based chat communication

Source: Internet
Author: User

Using the UDP protocol, write a command-line-based chat software, and the client and the server can send data to each other after the command line is started, respectively.

First, create Threads Sendthread and Receivethread

1  PackageCom.fhcq.chat;2 3 //Duplex Chat program4 //Can send data, but also can wait to receive data5 //need to use multithreading to implement6 7  Public classQicqdemo {8 9      Public Static voidMain (string[] args) {Ten         //TODO auto-generated method stub One  ASend send =NewSend (); -Receive receive =NewReceive (); -  theThread Sendthread =NewThread (send); -Thread Receivethread =NewThread (receive); -  -Sendthread.start (); +Receivethread.start (); -} +} A 
Java Code

Second, the use of thread sendthread to achieve the transmission of information

1  PackageCom.fhcq.chat;2 3 ImportJava.io.IOException;4 ImportJava.net.DatagramPacket;5 ImportJava.net.DatagramSocket;6 Importjava.net.InetAddress;7 ImportJava.net.SocketException;8 ImportJava.net.UnknownHostException;9 ImportJava.util.Scanner;Ten  One  Public classSendImplementsRunnable { A  -@Override -      Public voidRun () { the         //TODO auto-generated method stub -  -         Try{ -Datagramsocket Datagramsocket =NewDatagramsocket (); +InetAddress inetaddress = Inetaddress.getbyname ("Congkick"); -Scanner Scanner =NewScanner (system.in); +              while(true){ A                 //Blocking atString nextline = Scanner.nextline (); -               byte[] bytes = Nextline.getbytes (); -Datagrampacket Datagrampacket =NewDatagrampacket -(bytes,bytes.length,inetaddress,10019); -Datagramsocket.send (Datagrampacket); -                 if(Nextline.equals ("886")) { in                      Break; -} to}
32
33}Catch(SocketException e) { the             //TODO auto-generated catch block *E.printstacktrace (); $}Catch(Unknownhostexception e) {Panax Notoginseng             //TODO auto-generated catch block -E.printstacktrace (); the}Catch(IOException e) { +             //TODO auto-generated catch block AE.printstacktrace (); the} +} -}
45
Send Code

Third, using thread receivethread to realize the receiving of information

1  PackageCom.fhcq.chat;2 3 ImportJava.io.IOException;4 ImportJava.net.DatagramPacket;5 ImportJava.net.DatagramSocket;6 ImportJava.net.SocketException;7 8  Public classReceiveImplementsRunnable {9 Ten@Override One      Public voidRun () { A         //TODO auto-generated method stub -  -         //New Datagramsocket the         Try{ -Datagramsocket Datagramsocket =NewDatagramsocket (10010); -             byte[] buf =New byte[1024]; -             //Blocking method +Datagrampacket Datagrampacket =NewDatagrampacket (BUF, 1024); -              while(true){ +Datagramsocket.receive (Datagrampacket); A                 byte[] data = Datagrampacket.getdata (); at                 intLength = Datagrampacket.getlength (); -String string =NewString (data,0,length); -System.out.println (string); -                 if(String.Equals ("886")) { -                      Break; -} in}
30
*         Catch (SocketException e) { +             //TODO auto-generated catch block               E.printstacktrace ();  the         Catch (IOException e) {              //TODO auto-generated catch block  $             E.printstacktrace (); PNS         } --     }  *
40
Receive Code

"Java Network Programming" UDP-based chat communication

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.