Java basic knowledge of network programming ——-UDP protocol-based communication example

Source: Internet
Author: User

UDP is an unreliable protocol that establishes a socket on both ends of the communication, which does not establish a persistent communication connection and only sends data unilaterally to the other party without checking the results of the transmission.

UDP protocol-based communication in Java uses the receive and send methods of the Datagramsocket class, but the message needs to be encapsulated by a specific class (Datagrampacket)

The following is an example of a UDP protocol-based communication,

Server-side,

1  PackageUDP;2 3 Importjava.io.IOException;4 ImportJava.net.DatagramPacket;5 ImportJava.net.DatagramSocket;6 7  Public classServer {8     Private Static Final intPORT = 3000;9     //Datagram sizeTen     Private Static Final intData_len = 4096; One     //byte array that accepts network data A     byte[] Inbuff =New byte[Data_len]; -     //Receive Data -     PrivateDatagrampacket Inpacket =NewDatagrampacket (Inbuff, inbuff.length); the     //Send Data -     PrivateDatagrampacket Outpacket; -string[] Books =Newstring[] -     { +"Crazy English", -"Kangxi Dictionary", +"TCP Protocol", A"NiO non-blocking channel" at     }; -      Public voidInit ()throwsIOException { -         Try { -Datagramsocket socket =NewDatagramsocket (PORT); -              for(inti = 0; i < 1000; i++) { -                 //reading data from a socket in socket.receive (inpacket); -System.out.println (Inbuff = =inpacket.getdata ()); toSystem.out.println (NewString (Inbuff, 0, Inpacket.getlength ())); +                  -                 byte[] senddata = books[i% 4].getbytes (); theOutpacket =NewDatagrampacket (SendData, Senddata.length, Inpacket.getaddress (), PORT); * socket.send (outpacket); $             }Panax Notoginseng}Catch(IOException e) { - e.printstacktrace (); the         } +     } A      the      Public Static voidMain (string[] args)throwsIOException { +         NewServer (). Init (); -     } $}

Client

1  PackageUDP;2 3 Importjava.io.IOException;4 ImportJava.net.DatagramPacket;5 ImportJava.net.DatagramSocket;6 Importjava.net.InetAddress;7 ImportJava.util.Scanner;8 9  Public classClient {Ten     Private Static Final intPORT = 3000; One     //Datagram size A     Private Static Final intData_len = 4096; -     Private Static FinalString dest_ip = "127.0.0.1"; -     //byte array that accepts network data the     byte[] Inbuff =New byte[Data_len]; -     //Receive Data -     PrivateDatagrampacket Inpacket =NewDatagrampacket (Inbuff, inbuff.length); -     //Send Data +     PrivateDatagrampacket Outpacket; -      +      Public voidInit ()throwsIOException { A         Try { at             //create a socket using a random port -Datagramsocket socket =NewDatagramsocket (); -Outpacket =NewDatagrampacket (New byte[0], 0, Inetaddress.getbyname (DEST_IP), PORT); -Scanner Scann =NewScanner (system.in); -              while(Scann.hasnextline ()) { -                 byte[] Buff =scann.nextline (). GetBytes (); in outpacket.setdata (buff); - socket.send (outpacket); to socket.receive (inpacket); +System.out.println (NewString (Inbuff, 0, Inpacket.getlength ())); -             } the}Catch(IOException e) { * e.printstacktrace (); $         }Panax Notoginseng     } -      the      Public Static voidMain (string[] args)throwsIOException { +         NewClient (). Init (); A     } the}

Executes the result, starts a server side, starts a client sends a message, the server side outputs the information,

Java basic knowledge of network programming ——-UDP protocol-based communication example

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.