Java networking:udp Datagramsocket (translator)

Source: Internet
Author: User

Original: http://tutorials.jenkov.com/java-networking/udp-datagram-sockets.html
    • UDP vs. TCP
    • Sending Data via a datagramsocket
    • Receiving Data via a datagramsocket

Datagramsocket' s is Java's mechanism for network communication via UDP instead of TCP. UDP is still layered ontop of IP. You can use the Java ' s DatagramSocket both for sending and receiving UPD datagrams.

The Datagramsocket class uses UDP instead of TCP in the Java network communication mechanism. UDP is still on top of the IP layer. You can send and receive UDP packets using the Datagramsocket class .

UDP vs. TCP

UDP works a bit differently from TCP. When you send the data via TCP you first create a connection. Once The TCP connection is established TCP guarantess that your data arrives at the other end, or it'll tell you this an Error occurred.

With UDP just, send packets of data (datagrams) to some IP address on the network. You have no guarantee that the data would arrive. You also has no guarantee about the order which UDP packets arrive in at the receiver. This means. UDP have less protocol overhead (no stream integrity checking) than TCP.

UDP is appropriate for data transfers where it doesn ' t matter if a packet are lost in transition. For instance, imagine a transfer of a live tv-signal over the Internet. You want the signal to arrive on the clients as close to live as possible. Therefore, if a frame or both is lost, you don ' t really care. You don't want the live broadcast to being delayed just to make sure all frames is shown at the client. You ' d rather skip the missed frames, and move directly to the newest frames at all times.

This could also is the case with a surveillance camera broadcasting over the Internet. Who cares-happened in the past, when you were trying to monitor the present. You don't want to end up being-seconds behind reality, just because you want-show all frames to the person Monitorin G The camera. It's a bit different with the storage of the camera recordings. Want to lose a once recording the images from the camera to disk. Rather want a little delay, than not has those frames to go back and examine, if something important occurs.

Sending Data via a datagramsocket

To send data via Java ' s DatagramSocket must first create a DatagramPacket . Here's how it's done:

byte[] buffer = new Byte[65508];inetaddress address = Inetaddress.getbyname ("jenkov.com");D atagrampacket packet = new Dat Agrampacket (    buffer, buffer.length, address, 9000);

The byte buffer (the byte array) is the data, which is, is sent in the UDP datagram. The length of the above buffer, 65508 bytes, is the maximum amount of data you can send in a single UDP packet.

The length given to the constructor are the length of the data in the DatagramPacket buffer to send. All data in the buffer after that amount of data is ignored.

The InetAddress instance contains the address of the node (e.g. server) to send the UDP packet to. The InetAddress class represents an IP address (Internet address). The getByName() method returns an InetAddress instance with the IP address matching the given host name.

The port parameter is the UDP port, the server to receiver, the data is listeing on. UDP and TCP ports is not the same. A computer can has different processes listening on e.g. port with UDP and in TCP at the same time.

To send the DatagramPacket must create a DatagramSocket targeted at sending data. Here's how it's done:

Datagramsocket datagramsocket = new Datagramsocket ();

To send data from send() the method, like this:

Datagramsocket.send (packet);

Here are a full example:

Datagramsocket datagramsocket = new Datagramsocket (); byte[] buffer = "0123456789". GetBytes (); inetaddress Receiveraddress = Inetaddress.getlocalhost ();D atagrampacket packet = new Datagrampacket (        buffer, buffer.length, Receiveraddress,;d atagramsocket.send (packet);
Receiving Data via a datagramsocket

Receiving data via A is do by first DatagramSocket creating a and then DatagramPacket receiving data into it via the DatagramSocket ' s receive() method. Here are an example:

Datagramsocket datagramsocket = new Datagramsocket, byte[] buffer = new BYTE[10];D atagrampacket packet = new Datagrampa Cket (buffer, buffer.length);d atagramsocket.receive (packet);

Notice how the are instantiated with the DatagramSocket parameter value of passed to its constructor. This parameter is the UDP port, the is DatagramSocket -to-receive UDP packets on. As mentioned earlier, TCP and UDP ports is not the same, and thus does not overlap. You can have a different processes listening on both TCP and UDP port, without any conflict.

Second, a byte buffer and a is DatagramPacket created. Notice how DatagramPacket the have no information about the node to send data to, as it does when creating a for DatagramPacket sending data. This is because we be going DatagramPacket to use the for receiving data and not sending it. Thus No destination address is needed.

Finally the DatagramSocket ' s receive() method is called. This method blocks until a is DatagramPacket received.

The data received is located in the DatagramPacket ' s byte buffer. This buffer can is obtained by calling:

byte[] buffer = Packet.getdata ();    

How much data were received in the buffer are up to your find out. The protocol are using should specify either how much data is sent per UDP packet, or specify an end-of-data marker yo u can look for instead.

A real server program would probably call receive() the method in a loop, and pass all received's to DatagramPacket a pool of worker thr EADS, just like a TCP server does with incoming connections (Seejava multithreaded Servers for more details).

Next:java Networking:url + urlconnection

Java networking:udp Datagramsocket (translator)

Related Article

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.