Screen Sharing for UDP-Based Network Communication (1)

Source: Internet
Author: User

Screen Sharing for UDP-Based Network Communication (1)

UDP is a widely used network transmission protocol. The sender only sends data, regardless of whether the data can be delivered.

Application Scope: Sometimes the receiver may lose part of the data due to network problems, but it does not affect the functions of the program. For example, some data may be lost during live video streaming, but it may be stuck at most without affecting the function.

For sendersThe sender's address and port are required, and the sender's port must be known. A socket is also required to transmit data.

Here, they can be compared to the postal system.

The sender is the sender, the receiver is the recipient, and the delivery is the postman.

 
 
  1. // Create a sender (sender). SocketAddress sender = new InetSocketAddress ("127.0.0.1", 912 );
  2. // Create a receiver (recipient) SocketAddress receiver = new InetSocketAddress ("127.0.0.1", 913 );
  3. // Create a sender ramsocket socket = new sender ramsocket (sender );

For the sender, he needs to pack the items to be sent, that is, the packages. And then deliver it to the postman.

 
 
  1. byte[] msg="Hello!".getBytes();   DatagramPacket m = new DatagramPacket(msg, msg.length, receiver);   
  2. socket.send(m);   

For the recipientHe needs to know where to get the data, who is the postman, and received a package.

 
 
  1. // Create the receiving object (recipient) SocketAddress receiver = new InetSocketAddress ("127.0.0.1", 913 );
  2. // The socket (Postman) receiving ramsocket socket = new DatagramSocket (receiver ER );
  3. // Define the package export rampacket data = new export rampacket (buf, buf. length );
  4. // Use socket to receive the data package socket. receive (data );

Some protocols need to be defined.

UDP provides one-to-one sharing, and can share data in multicast mode, that is, one-to-multiple.

The following uses simple screen sharing as an example.

First, we need to make it clear that we want to share the screen of a computer with others.

That is, you can use the LAN multicast to screen the computer.

Because the array sent each time cannot be too large, the image captured on the screen needs to be sent multiple times, and the source image will be assembled when the client receives the image. Therefore, an information header is required to save the basic information of the image so that the client can successfully spell back the source image after receiving it.

The key lies in how to define this information header. In the receiver, we need to know the number of times the image is sent to us by the sender, and the total number of bytes, it is also necessary to determine whether some data is discarded due to network reasons, so that data cannot be restored.

Here, I use the following methods:

The information header is defined as follows:

The first byte is of type and 0 is used to represent the image temporarily.

The second byte is the number of data groups, which means the number of times the image is sent out and the number of times it needs to be received on the client before the pin can be returned.

The third byte is a random mark used to tell the client whether data is lost. If data is lost,

Then, discard all relevant data and skip this frame if the source image cannot be spelled back.

The fourth byte is the number of digits in the length of the data to be transmitted. For example, if it is actually 1234 bytes, the value is 4.

The next n are length information, for example, data [4] = 1; data [5] = 2; data [6] = 3; this indicates the length is 1234

Each time, 10000 actual bytes of data are sent.

Add about 10 header information. Therefore, each array is 10010 in length.

After receiving a message, the client determines whether data is lost. If not, the source image is spelled back and displayed.

After receiving the data, if you find that some data is lost in the previous group, you need to clear all the data in the previous group and continue to receive

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.