Java programming Those things 105--network programming Technology 4

Source: Internet
Author: User
Tags port number server port

13.2.4 UDP Programming

Network communication In addition to the way the TCP, there is a way to implement the UDP way. UDP (user Datagram Protocol), the Chinese meaning is the Customer datagram protocol, similar to send short message, is a cheap way of communication, use this way without establishing a dedicated virtual connection, because no need to establish a dedicated connection, so the pressure on the server is much smaller than TCP , so it is also a common way of network programming. But the biggest disadvantage of using this approach is that transmission is unreliable, of course, is not to say that often lost, as we send short messages, the theoretical existence of the possibility of not receiving, this is likely to be 1%, anyway relatively small, but due to this possible existence, so usually we feel that the important thing is to make a phone call ( Similar to the TCP way), the general thing to send short messages (similar to the UDP method). Network programming is also the case, must require reliable transmission of information generally using TCP, the general data to use the UDP method to achieve.

The UDP network programming also obtains the good support in the Java language, because it does not need to establish the special connection and so on in the transmission data the process, therefore the implementation structure which in the Java API design and the TCP method is not very same. Of course, the class that needs to be used is included in the java.net package.

In the Java API, the implementation of UDP programming, including client network programming and server-side network programming, is mainly implemented by two classes, respectively:

L Datagramsocket

The Datagramsocket class implements network connections, including client network connections and server-side network connections. Although UDP network communication does not need to establish a dedicated network connection, but still need to send and receive data, Datagramsocket is the transmitter to send data, as well as the role of the listener when receiving data. Analogous to a network connection in TCP, this class can be used either to implement a client connection or to implement a server-side connection.

L Datagrampacket

The Datagrampacket class implements the encapsulation of data that is transmitted over the network, that is, the object of the class represents the data exchanged in the network. In UDP network programming, both the data to be sent and the data to be received must be processed into a Datagrampacket type object that contains the address sent to, the port number sent to, and the content sent. In fact, the role of the Datagrampacket class is similar to the actual letter in the letter, including the letter sent to the address and receiver, as well as the content sent, the post office only need to be delivered by address. When receiving data, the received data must also be processed into an object of type Datagrampacket, containing information such as the sender's address, port number, and the contents of the data. Compared with TCP network transmission, IO programming is not a necessary content in UDP network programming, and the structure is simpler than that of TCP mode network programming.

The following describes the UDP way of network programming, the client and server-side implementation steps, as well as through the basic example of the UDP way of network programming in the Java language to implement the way.

UDP network programming, programming steps and TCP similar, but the use of classes and methods exist a relatively large difference, the following first introduction to the UDP network programming client implementation process.

The steps involved in UDP client programming are also 4 parts: establishing a connection, sending data, receiving data, and closing a connection.

This paper first introduces the implementation of the connection in the network programming of UDP mode. The UDP way of establishing a connection and TCP mode is different, only need to establish a connection object, you do not need to specify the server's IP and port number. The implemented code is:

DatagramSocket ds = new DatagramSocket();

This establishes a client connection that uses the unused port number of one local computer that is randomly assigned by the system. In this connection, the server-side IP and port are not specified, so the UDP network connection is more like a transmitter than a specific connection.

Of course, you can create a client connection by setting the port number used by the connection.

DatagramSocket ds = new DatagramSocket(5000);

This creates a connection using port No. 5000 on the local computer. Typically, there is no need to specify a port number when establishing a client connection.

Next, introduce the implementation of sending data in UDP client programming. In the UDP network programming, IO technology is not necessary, when sending data, the need to send the data content first converted to a byte array, and then the data content, server IP and server port number together to construct a Datagrampacket type of object, This completes the preparation of the data, which can be sent by calling the Send method in the network connection object when it is sent. For example, to send the string "Hello" to the IP is 127.0.0.1, the port number is 10001 of the server, the implementation of the code to send data is as follows:

String s = “Hello”;
String host = “127.0.0.1”;
int port = 10001;
//将发送的内容转换为byte数组
byte[] b = s.getBytes();
//将服务器IP转换为InetAddress对象
InetAddress server = InetAddress.getByName(host);
//构造发送的数据包对象
DatagramPacket sendDp = new DatagramPacket(b,b.length,server,port);
//发送数据
ds.send(sendDp);

In this example code, regardless of what the data is sent, it needs to be converted to a byte array, and then the server-side IP address is constructed into an object of type inetaddress, which is constructed as a Datagrampacket type object after the preparation is complete, In UDP programming, the data content sent, server-side IP, and port number are included in the Datagrampacket object. When you are ready to do so, call the Send method of the connection object DS to send the Datagrampacket object.

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.