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 classTestudpserver {/**Server-side code *@paramargs * for UDP and TCP port numbers, first we can set arbitrary, but must be guaranteed within 65536 * UDP and TCP port number is not associated, that is, UDP and TCP each have 65,536 ports*/     Public Static voidMain (string[] args)throwsexception{byte[] buf =New byte[1024];//set the character array that holds the dataDatagrampacket DP =NewDatagrampacket (buf, buf.length); Datagramsocket DS=NewDatagramsocket (5888);//set the port number         while(true) {ds.receive (DP);//receive request data from clientSystem.out.println (NewString (buf, 0, Dp.getlength ())); //new String (buf, 0, Dp.getlength ()): A string constructor that converts a byte array into a string class of data        }    }}

 Client

 Public classtestudpclient {/**Client Code Instance *@paramargs * socketaddress: Address on the network, including IP, including port number and so on * INETADDRESS:IP address of Super * INETSOCKETADDRESS:IP address + port number*/     Public Static voidMain (string[] args)throwsexception{byte[] buf =New byte[1024]; BUF= (NewString ("Hello")). GetBytes ();//converting a string type of data to a character arrayDatagrampacket DP =NewDatagrampacket (buf, Buf.length,NewInetsocketaddress ("127.0.0.1", 5888)); Datagramsocket DS=NewDatagramsocket (9999);//set the port number for the client to send dataDs.send (DP);//sending data to the serverDs.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 classTestUDPServer1 { Public Static voidMain (string[] args)throwsexception{byte[] buf =New byte[1024];//set the character array that holds the dataDatagrampacket DP =NewDatagrampacket (buf, buf.length); Datagramsocket DS=NewDatagramsocket (5888);//set the port number         while(true) {ds.receive (DP);//receive request data from clientBytearrayinputstream Bais =NewBytearrayinputstream (BUF); DataInputStream Dis=NewDataInputStream (Bais);        System.out.println (Dis.readlong ()); }    }}

  Client

 Public classTestUDPClient1 { Public Static voidMain (string[] args)throwsexception{Long L= 10000L; Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); DataOutputStream dos=NewDataOutputStream (BAOs);//DataOutputStream: You can write a long type of data directlyDos.writelong (L); byte[] buf =Baos.tobytearray (); Datagrampacket DP=NewDatagrampacket (buf, Buf.length,NewInetsocketaddress ("127.0.0.1", 5888)); Datagramsocket DS=NewDatagramsocket (9999);//set the port number for the client to send dataDs.send (DP);//sending data to the serverDs.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.

Statement: If the above content is inappropriate, please leave a message. Thank you

java--Network Programming (UDP)

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.