be silently discarded. Invoking send() sends the content

public abstract long write(ByteBuffer[] srcs) throws IOException; public abstract long write(ByteBuffer[] srcs, int offset, int length) throws IOException; } The read() method returns the number of bytes read, which may be zero if the channel is in nonblocking mode. The return value of write() is consistent with send(): either the number of bytes in your buffer(s) or 0 if the datagram cannot be sent (because the channel is nonblocking). Either can throw NotYetConnectedException if invoked while the DatagramChannel is not in a connected state. Datagram channels are different beasts than stream sockets. Stream sockets are immensely useful because of their ordered, reliable data-transport characteristics. Most network connections are stream sockets (predominantly TCP/IP). But stream-oriented protocols such as TCP/IP necessarily incur significant overhead to maintain the stream semantics on top of the packet-oriented Internet infrastructure, and the stream metaphor does not apply to all situations. Datagram throughput can be higher than for stream protocols, and datagrams can do some things streams can’t. Here are some reasons to choose datagram sockets over stream sockets: Your application can tolerate lost or out-of-order data. You want to fire and forget and don’t need to know if the packets you sent were received. Throughput is more important than reliability. You need to send to multiple receivers (multicast or broadcast) simultaneously. The packet metaphor fits the task at hand better than the stream metaphor. If one or more of these characteristics apply to your application, then a datagram design may be appropriate. Example 3-9 shows how to use a DatagramChannel to issue requests to time servers at multiple addresses. It then waits for the replies to arrive. For each reply that comes back, the remote time is compared to the local time. Because datagram delivery is not guaranteed, some responses may never arrive. Most Linux and Unix systems provide time service by default. There are also several public time servers on the Internet, such as time.nist.gov. Firewalls or your ISP may interfere with datagram delivery. Your mileage may vary. Example 3-9. Time-service client using DatagramChannel package com.ronsoft.books.nio.channels; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.DatagramChannel; import java.net.InetSocketAddress; import java.util.Date; 115
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services

Comments are closed.