Build your own QQ using Java (2)

Source: Internet
Author: User
Custom socket)

Because it takes a certain amount of time to use each connection of the stream socket, to reduce this overhead, the Network API provides the second socket: a self-addressing socket ), self-addressing uses UDP to send addressing information (from the customer program to the service program or from the service program to the customer Program). The difference is that multiple IP address information packets can be sent through the self-addressing socket, the self-addressing information is included in the self-addressing package, and the self-addressing package is included in the IP package, which limits the addressing information length to 60000 bytes. Figure 2 shows the self-addressing information of the Self-addressing Package located in the IP package.


Unlike TCP, UDP provides another method to ensure that the information package does not reach the destination ,, UDP will not request the sender to resend the self-addressing packet because UDP contains error detection information in each self-addressing packet, after each self-addressing packet arrives at the destination, UDP only performs a simple error check. If the check fails, UDP will discard the self-addressing packet and will not request the replacement from the sender, this is similar to sending a mail through the Post Office. The sender does not need to establish a connection with the recipient before sending the mail, nor can it ensure that the mail can arrive at the recipient.

The work of the Self-addressing socket includes the following three classes: datagrampacket, datagramsocket, and multicastsocket. The initrampacket object depicts the address information of the Self-addressing package. initramsocket indicates the self-addressing socket of the customer program and the service program, and multicastsocket depicts the self-addressing socket capable of multi-point transfer, these three classes are located in the java.net package.

  Datagrampacket class

Before using a self-addressing package, you must first familiarize yourself with the datagrampacket class. Address information and the Self-addressing package are compressed into the objects created by this class in byte arrays at the same time.

Datagrampacket has several constructors. Even though these constructors have different forms, they usually have two common parameters: byte [] buffer and INT length, the buffer parameter contains a reference to the byte array that stores the information of the Self-addressing data packet. Length indicates the length of the byte array.

The simplest constructor is mongorampacket (byte [] buffer, int length). This constructor determines the length of the Self-addressing data packet array and array, however, there is no address or port information for the Self-addressing data packet. You can call the setaddress (inetaddress ADDR) and setport (INT port) Methods to add the information, 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 datemedipacket to Port 6000.

If you prefer to include both the address and port number when calling the constructor, you can use the mongorampacket (byte [] buffer, int length, inetaddress ADDR, int port) function, the following code demonstrates another option.

Byte [] buffer = new byte [100];
Inetaddress IA = inetaddress. getbyname ("www.disney.com ");
Datagrampacket DGP = new datagrampacket (buffer, buffer. length, IA,
6000 );

You can call the setdata (byte [] buffer) and setlength (INT length) Methods to change the byte array and its length after creating the initrampacket object. You can call getdata () to obtain the byte array reference at any time, and call getlength () to obtain the length of the byte array. The following code demonstrates these methods:

Byte [] buffer2 = new byte [1, 256];
DGP. setdata (buffer2 );
DGP. setlength (buffer2.length );

For more information about datagrampacket, see the SDK documentation.

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.