UDP protocol interaction information for Java

Source: Internet
Author: User

Because to do the application of the UDP protocol interaction, so it is specifically learning the next, in fact, similar to the Java server and socket, the following is written a simple demo

Service side:

Package Com.test1;import java.io.ioexception;import java.net.datagrampacket;import java.net.DatagramSocket; Import Java.net.inetsocketaddress;import java.net.SocketException; Public classUpdserver {Private byte[] Buff =New byte[1024x768]; PrivateDatagrampacket DP; PrivateDatagramsocket ds; Privateinetsocketaddress socketaddress;  PublicUpdserver (String host,intPort) {socketaddress=Newinetsocketaddress (host, Port); Try{DS=NewDatagramsocket (socketaddress); } Catch(SocketException e) {System. out. println ("Connection failed:"+e.getlocalizedmessage ());        E.printstacktrace (); } System. out. println ("The server opens the monitor ..."); }        PrivateString receive (String host,intPort) {String Info=""; Try{DP=NewDatagrampacket (Buff,0, buff.length);            Ds.receive (DP); Info=NewString (Dp.getdata (),0, Dp.getlength ()); }Catch(Exception e) {e.printstacktrace (); }        returninfo; }         Public Static voidMain (string[] args) {String host="127.0.0.1"; intPort =8888; Updserver Server=NewUpdserver (host, Port); String Info=server.receive (host, Port); System. out. println (info); //send information to the customer service sideServer.send (NewString ("Hello, Sao year! ")); }    Private voidSend (Stringstring) {datagrampacket DPS=NewDatagrampacket (Buff,0, Buff.length, Dp.getaddress (), Dp.getport ()); Dps.setdata (string. GetBytes ()); Try{ds.send (DPS); } Catch(IOException e) {e.printstacktrace (); }    }}

Client:

Package Com.test1;import java.io.ioexception;import java.net.datagrampacket;import java.net.DatagramSocket; Import Java.net.inetaddress;import java.net.SocketException; Public classupdclient {Private byte[] Buff =New byte[1024x768]; PrivateDatagrampacket DP; PrivateDatagramsocket ds;  Publicupdclient () {Try{DS=NewDatagramsocket (); } Catch(SocketException e) {e.printstacktrace (); }    }         Public Static voidMain (string[] args) {updclient updclient=Newupdclient (); String Host="127.0.0.1"; intPort =8888; Updclient.send (host, Port,NewString ("Hello, Junior!"). GetBytes ()); //AcceptString info =updclient.receive (); System. out. println (info); }    PrivateString receive () {Datagrampacket DPR=Newdatagrampacket (Buff, buff.length); String Info=""; Try{ds.receive (DPR); Info=NewString (Dpr.getdata (),0, Dpr.getlength ()); } Catch(IOException e) {e.printstacktrace (); }        returninfo; }    Private voidSend (String host,intPortbyte[] b) {Try{DP=NewDatagrampacket (Buff,0, Buff.length, Inetaddress.getbyname (host), Port);            Dp.setdata (b);        Ds.send (DP); }Catch(Exception e) {e.printstacktrace (); }    }}

UDP protocol interaction information for Java

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.