java--Network Programming (UDP)

Source: Internet
Author: User

This morning gives you a brief introduction of TCP network communication knowledge, now for everyone to complement the knowledge of complete network programming, about UDP communication knowledge.

UDP is an unreliable network protocol, so what is the use value or necessity? In some cases, the UDP protocol may become very useful. Because UDP has the speed advantage that TCP can not catch. Although a variety of security features are embedded in the TCP protocol, the actual execution of the system consumes a lot of overhead, which undoubtedly makes the speed seriously affected. In view of the elimination of the information reliable transmission mechanism, the security and sequencing functions are transferred to the upper application to complete, greatly reducing the execution time, so that the speed is guaranteed.

This kind of textual description, I will not repeat, the relevant aspects of the text introduced Baidu on more, I will directly to introduce you to the code.

One, based on the UDP network programming code:

Server-side

public class Testudpserver {    /** server-side code     * @param args     * for UDP and TCP port numbers, first we can set any, but must be guaranteed to be within 65536     * UDP and TCP port numbers are not associated, that is, UDP and TCP each have 65,536 ports     */public    static void Main (string[] args) throws exception{        byte [ ] buf = new byte[1024];//Sets the character array that holds the data        datagrampacket DP = new Datagrampacket (buf, buf.length);        Datagramsocket ds = new Datagramsocket (5888);//Set port number        while (true) {            ds.receive (DP);//Receive request data            sent by client System.out.println (New String (buf, 0, Dp.getlength ()));            New string (buf, 0, Dp.getlength ()): string constructor, converts a byte array to a string class of Data}}}    

Client

public class Testudpclient {    /** client code Instance     * @param args     * socketaddress: Address on the network, including IP, port number, etc.     * Inetaddress:ip Address's Super     * INETSOCKETADDRESS:IP address + port number     *    /public static void main (string[] args) throws exception{        byte [] buf = new byte[1024];        BUF = (New String ("Hello")). GetBytes ();//Converts data of type string to a character array        datagrampacket DP = new Datagrampacket (buf, Buf.length, New Inetsocketaddress ("127.0.0.1", 5888));        Datagramsocket ds = new Datagramsocket (9999);//Set the port number of the client to send the data        Ds.send (DP);//Send data to the server        ds.close ();}    }

About the details of the code, I have commented in it, if you need to learn, you can copy directly, the above code is complete, you can run directly.

Second, UDP-based data transmission:

Server-side

public class TestUDPServer1 {public    static void Main (string[] args) throws exception{        byte [] buf = new byte[1024 ];//sets the character array that holds the data        datagrampacket DP = new Datagrampacket (buf, buf.length);        Datagramsocket ds = new Datagramsocket (5888);//Set port number        while (true) {            ds.receive (DP);//Receive request data            sent by client Bytearrayinputstream Bais = new Bytearrayinputstream (BUF);            DataInputStream dis = new DataInputStream (Bais);            System.out.println (Dis.readlong ());}}}    

Client

public class TestUDPClient1 {public    static void Main (string[] args) throws exception{        Long L = 10000L;        Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();        DataOutputStream dos = new DataOutputStream (BAOs);//dataoutputstream: You can write a long type of data        Dos.writelong (L) directly;        byte [] buf = Baos.tobytearray ();        Datagrampacket DP = new Datagrampacket (buf, Buf.length, New Inetsocketaddress ("127.0.0.1", 5888));        Datagramsocket ds = new Datagramsocket (9999);//Set the port number of the client to send the data        Ds.send (DP);//Send data to the server        ds.close ();}    }

You must have such a question, what is the use of it? Well, let me briefly introduce you to this long type of data transmission is very useful in online games, why so? For example, you in need for speed, the system to your real-time in 3-dimensional space in the location of the information sent to another player system, because the game requires fast transmission speed, so UDP-based data transmission in the game development process the most useful.

The above is just some basic UDP programming code, just to give you a brief introduction, so that we have a little understanding.

java--Network Programming (UDP)

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.