Java Network Programming--UDP communication

Source: Internet
Author: User

First, the network transmits data need to understand for example three points

1. Find the other IP

2, the data to be sent to the other side of the application specified, in order to identify these applications, so the network applications are identified with numbers. In order to conveniently address this number, called Port, the logical port

3, define the communication rules. This communication rule is called a protocol.


The international organization defines the universal protocol TCP/IP.

127.0.0.1 native loopback address is not easy to remember, host name available hostname: localhost

0-65535port range 0-1024 system uses or retains port. MySQL has a port number of 3306

A, UDP

UDP relative to TCP is not trustworthy.

TCP has 3 handshake before establishing the connection, and the data retransmission mechanism after the connection is established to ensure the stable transmission of the data.

UDP does not have a handshake mechanism or retransmission mechanism .

The simple point is that the UDP communication only tube, regardless of the other side to receive.


TCP will have a packet after each communication to confirm receipt. The check is perfect, otherwise it will be re-transmitted.

Udp

The data and the source and destination are encapsulated into a packet, and no connection is required. The limit size of each datagram is within 64, because there is no connection. is an unreliable agreement.

No need to establish a connection, fast.

Tcp

Establishes the connection, forms the data transmission channel, carries on the big data quantity transmission in the connection, through three times handshake completes the connection. is a reliable protocol. A connection must be established. The efficiency will be slightly lower.

Three times handshake, (Zhang San are you there?) ) (I'm in. (OK, I know you're here.)

)


Here is a sample of how to send UDP

(i) UDP transmitter terminal

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

Ideas:

1. Establish Udpsocket Service

2. Provide data and encapsulate the data in a packet

3, through the socket service send function. Send out the data packets.

4, close the resources.

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 UDP service, via Datagramsocket object, randomly select data port to send Datagramsocket ds=new Datagramsocket ();//Determine data. and encapsulated as packet byte[] buf= "Dadfdadsfsafasdf". GetBytes ();D atagrampacket dp=new datagrampacket (Buf,buf.length, Inetaddress.getbyname ("192.168.0.100"), 1000);//through the socket service. Send the existing data packets through the Send method Ds.send (DP);//Close Resource ds.close ();}}


(ii) UDP receiving end

Defining the receive side of UDP

Demand:

Define an application. Used to receive and process data transmitted by the UDP protocol.

1, the definition of udpsocket services, will generally listen to a port.

The fact is to define a digital ID for this receiving network application. Easy to understand what data comes to the application to handle.

2. Define a packet. Because the bytes of data that are received are stored.

Because there are many other features in the packet object that can advance different data information in the byte data.

3. The received data is stored in a defined packet via the Receive method of the socket service

4, through the unique functions of the packet object. Take these different data out and print them 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[10 ;D Atagrampacket dp = new Datagrampacket (arr, arr.length);d s.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);d s.close ();}} Datagramsocket ds = new Datagramsocket (10000); If you run the statement in a while (true) loop, the port used exception occurs.

Java Network Programming--UDP communication

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.