Network Programming learning notes (3) UDP and network programming learning notes

Source: Internet
Author: User

Network Programming learning notes (3) UDP and network programming learning notes
1. unreliable, efficient, datagram (data is packaged into a packet and sent out in a single packet), non-connection. 2. UDP is not connected. Therefore, it does not distinguish between the server and the client strictly. 3. UDP communication process: UDP uses byte Arrays for dialog. "Server": connect to data (1) create a byte array to receive data sent by the other party. byte buf [] = new byte [1024]; (2) create a data PACKET Socket DatagramSocket ds = new DatagramSocket (5678); // The port number 5678 is udp 5678, and TCP also has a port 5678 (3) create a new package, the package fills up the byte array, and the second parameter indicates the length of the package in the array. When the receive () method is called to receive data, the received content is stored in the byte array buf. DatagramPacket dp = new DatagramPacket (buf, buf. length); (4) receive the data sent by the other party. When someone sends the data, it puts the data in the baggage. receive is also a blocking method. If no one sends the data, it will keep waiting. Ds. receive (dp); System. out. println (new String (buf, 0, dp. getLength (); // converts the contents in the baggage to String output String (buf, 0, dp. getLength () indicates constructing a string through a part of the array. Dp. getLength () indicates the space occupied by the byte array. Ds. close (); "customer": sending data (1) converting the content to be sent to a byte array byte [] buf = (new String ("Hello ")). getBytes (); (2) create a package and put the byte array. InetSocketAddress is a subclass of SocketAddress that represents the IP address. DatagramPacket dp = new DatagramPacket (buf, buf. length, new InetSocketAddress ("127.0.0.1", 5678); (3) create a data PACKET Socket using ramsocket ds = new using ramsocket (9999); // 9999 is the client port (4) send information ds. sent (dp); ds. close (); in the preceding example, messages of the String type are transmitted. If you want to transmit a message of the Long type, you must use ByteArrayOutputStream to transmit the message. The server uses ByteArrayInputStream to receive the message. Client: long n = 1000L; ByteArrayOutputStream baos = new ByteArrayOutputStream (); DataOutputStream dos = new DataOutputStream (baos); // to write long data, use DataOutputStream to process stream dos. writeLong (n); // Write Data byte [] buf = baos to the byte array output stream. toByteArray (); // converts the content in the stream to a byte array, creates a mongorampacket, and sends a message using UDP's mongoramsocket. The following is the same as the preceding example. Server: ds. receive (dp); ByteArrayInputStream bais = new ByteArrayInputStream (buf); DataInputStream dis = new DataInputStream (bais); System. out. println (dis. readLong (); note: the most popular streams in UDP are ByteArrayOutputStream, DataOutputStream, ByteArrayInputStream, and DataInputStream.

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.