Simple implementation of the JAVA,UDP protocol

Source: Internet
Author: User

simple implementation of the//UDP protocol-----Server
PackageUDP;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetSocketAddress;ImportJava.nio.ByteBuffer; Public classUdpserverImplementsRunnable {Private intPort; PublicUdpserver (intPort) { This. Port =Port; } Public voidrun () {Try { //listening at port for UDP requestDatagramsocket Server =NewDatagramsocket (Newinetsocketaddress (port)); byte[] bs =New byte[4]; Bytebuffer Bbuf=NULL; Datagrampacket Data=NewDatagrampacket (BS, bs.length); Server.setsotimeout (1000 * 10);//Set Timeout while(true) {//Keep listening .server.receive (data); Bbuf= Bytebuffer.wrap (BS, 0, 4); String s= "Receive" + Data.getdata (). length + "byte data,value:" +Bbuf.getint (); System.out.println (s); } } Catch(Exception e) {e.printstacktrace (); } } Public Static voidMain (string[] args) {NewThread (NewUdpserver (11111) . Start (); }}

Client implementation;

 PackageUDP; ImportJava.net.DatagramPacket; ImportJava.net.DatagramSocket; Importjava.net.InetSocketAddress;  Public classUdpClientImplementsRunnable {Private intPort;  PublicUdpClient (intPort) {             This. Port =Port; }         Public voidrun () {Try{Datagramsocket Client=NewDatagramsocket (); byte[] bs =New byte[4]; Inetsocketaddress Isa=NewInetsocketaddress ("localhost", Port); Datagrampacket Data=NewDatagrampacket (BS, bs.length);                Data.setsocketaddress (ISA);  for(inti = 0; I < 10; i++) {Data.setdata (Int2bytes (i));                Client.send (data); }            } Catch(Exception e) {e.printstacktrace (); }        }        Static byte[] Int2bytes (intnum) {            byte[] B =New byte[4];  for(inti = 0; I < 4; i++) {B[i]= (byte) (Num >>> (24-I * 8)); }            returnb; }    }

Test code;

 PackageUDP; Public classTest { Public Static voidMain (string[] args)throwsinterruptedexception {intPort = 11111; Try{            NewThread (NewUdpserver (port)). Start (); NewThread (NewUdpClient (port)). Start (); }Catch(Exception e) {//e.printstacktrace ();        }    }}

Simple implementation of the JAVA,UDP protocol

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.