JAVA Network Programming-UDP Communication

Source: Internet
Author: User

First, you need to know three points about network data transmission:

1. Find the peer IP Address

2. Data is sent to the application specified by the other party. To identify these applications, the network applications are identified by numbers. For convenience, this number is called port and logical port.

3. Define communication rules. This communication rule is called a protocol.


International organizations define the common protocol TCP/IP.

127.0.0.1 the local loopback address is hard to remember. Available Host Name: localhost

The port range of 0-65535 is 0-1024 and the system uses or retains the port. The mysql port is 3306.

1. UDP

UDP is untrusted to TCP.

TCP has three handshakes before the connection is established. After the connection is established, there is a data retransmission mechanism to ensure stable data transmission.

UDP does not have a handshake or retransmission mechanism.

To put it simply, UDP only sends messages, regardless of whether the recipient receives the messages.
TCP requires a packet after each communication is completed to confirm whether the packet is received and whether the verification is complete. Otherwise, the packet will be re-transmitted.

UDP

Encapsulate data and source and target data into data packets without establishing a connection. The size of each data packet is limited to 64. Because of no connection, It is an unreliable protocol. No need to establish a connection, fast.

TCP

Establish a connection to form a channel for data transmission. The connection is a reliable protocol that transfers large data volumes and completes the connection through a three-way handshake. A connection must be established to reduce the efficiency.

Three-way handshake (Zhang San, are you there ?) (I am here .) (Well, I know you are here .)


The following is an example of UDP transmission.

(1) UDP sender

Requirement: Send a piece of text data through UDP transmission.

Ideas:

1. Establish the udpsocket Service

2. provide data and encapsulate the data into data packets

3. Send data packets through the sending function of the socket service.

4. Close the resource.

Import java. io. IOException; import java.net. datagramPacket; import java.net. datagramSocket; import java.net. inetAddress; import java.net. socketException; import java.net. unknownHostException; public class Work {public static void main (String [] args) throws IOException {// create a UDp Service by using the DatagramSocket object, randomly select the data port to send the DatagramSocket ds = new DatagramSocket (); // determine the data and encapsulate it into a data packet byte [] buf = "dadfdadsfsafasdf ". getBytes (); DatagramPacket dp = new DatagramPacket (buf, buf. length, InetAddress. getByName ("192.168.0.100"), 1000); // use the socket service to send existing data packets and use the send method ds. send (dp); // closes the resource ds. close ();}}


(2) UDP receiving end

Define the udp Receiver

Requirements:

Define an application to receive and process data transmitted over UDP.

1. Define the udpSocket service and usually listen to a port. In fact, it is to define a digital ID for the receiving network application. It is convenient to identify which data can be processed by the application.

2. Define a data packet because the received byte data is to be stored,

Because more functions of the data packet object can be used to advance different data information in byte data.

3. Use the receive method of the socket service to store the received data into the predefined data packet.

4. Extract the different data using the special functions of the data packet object and print it on the console.

5. Close Resources

Import java. io. IOException; import java.net. datagramPacket; import java.net. datagramSocket; public class ReData {public static void main (String [] args) throws IOException {DatagramSocket ds = new DatagramSocket (10000); byte [] arr = new byte [1024]; datagramPacket dp = new DatagramPacket (arr, arr. length); ds. receive (dp);/String ip = dp. getAddress (). getHostAddress (); String data = new String (dp. getData (), 0, Dp. getLength (); int port = dp. getPort (); System. out. println (ip + ":" + data + ":" + port); ds. close () ;}} DatagramSocket ds = new DatagramSocket (10000); If you execute this statement in a while (true) loop, an exception occurs on the port.

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.