Java socket programming (Part 2)(1)

黄舟
Release: 2016-12-23 16:20:31
Original
1798 people have browsed it

Self-addressing sockets (Datagram Sockets)
​, because each connection using a stream socket takes a certain amount of time. To reduce this overhead, the network API provides a second type of socket: self-addressing Socket (datagram socket), self-addressing uses UDP to send addressing information (from client program to service program or from service program to client program). The difference is that multiple IP information packets can be sent through self-addressing sockets. The self-addressing information is contained in the self-addressing packet, and the self-addressing packet is contained in the IP packet, which limits the length of the addressing information to 60,000 bytes. Figure 2 shows the self-addressing information of a self-addressed packet located within an IP packet.
Different from the way TCP ensures that information reaches the information destination, UDP provides another method. If the self-addressed information packet does not reach the destination, then UDP will not request the sender to resend the self-addressed packet. This is Because UDP contains error detection information in each self-addressed packet, UDP only performs a simple error check after each self-addressed packet reaches the destination. If the detection fails, UDP will discard the self-addressed packet and will not A replacement will be re-requested from the sender, which is similar to sending a letter through the post office. The sender does not need to establish a connection with the recipient before sending the letter. There is also no guarantee that the letter will reach the recipient. Self-addressed sockets work Includes the following three classes: DatagramPacket, DatagramSocket, and MulticastSocket. The DatagramPacket object depicts the address information of the self-addressed packet, the DatagramSocket represents the self-addressing socket of the client program and the service program, and the MulticastSocket represents the self-addressing socket capable of multicast transmission. These three classes are located in java. net package.
 DatagramPacket class
 Before using the self-addressed package, you need to first be familiar with the DatagramPacket class. The address information and the self-addressed package are compressed into the object created by this class in the form of a byte array at the same time
 DatagramPacket has several constructors, even these The form of the constructor is different, but usually they all have two parameters in common: byte [] buffer and int length. The buffer parameter contains a reference to a byte array that stores self-addressed packet information, and length represents a word. The length of the section array.
 The simplest constructor is DatagramPacket(byte [] buffer, int length). This constructor determines the self-addressed data packet array and the length of the array, but does not have any address and port information of the self-addressed data packet. This information It can be added later by calling the methods setAddress(InetAddress addr) and setPort(int port). The following code demonstrates these functions and methods.
byte [] buffer = new byte [100];
DatagramPacket dgp = new DatagramPacket (buffer, buffer.length);
InetAddress ia = InetAddress.getByName ("www.disney.com");
dgp.setAddress (ia) ;
dgp.setPort (6000); // Send datagram packet to port 6000.

The above is the content of Java socket programming (Part 2) (1). For more related content, please pay attention to the PHP Chinese website (www.php. cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!