5.1 Connectionless Protocol ClassesOne mechanism for transferring data between a two programs is called the connectionless protocol. With this protocol, a given packet is sent without any guarantee that it will be delivered to the recipient, and without any way for the sender to determine whether or not it has arrived. The connectionless protocol has a performance advantage over the connection-oriented protocol and is therefore useful for applications where increased performance can be traded off against the risk of losing some data, such as when transferring video data. The connectionless protocol class hierarchy is shown in the figure below.
A schematic showing the mechanism for transferring data between two programs using the connectionless protocol is shown below. The sender and recipient programs each create a DatagramPacket object and a DatagramSocket object. The sender's DatagramPacket object contains some data, and a destination IP address and port number. The send() method of the DatagramSocket object is then used to send the DatagramPacket object to its destination. The recipient, meanwhile, has created a DatagramSocket object that is associated with the appropriate port number at which it wishes to listen. The receive() method of the DatagramSocket object is used to place whatever data it receives into the servers DatagramPacket object.
5.1.1 DatagramPacket Classpublic final class DatagramPacket extends Object Object DatagramPacket A DatagramPacket object represents a packet of data consisting of an array of bytes. It is used to transfer data between a client machine and a server using the connectionless protocol. The DatagramPacket object also has associated with it an IP address and a port number to which it is being sent, or from which it was received. DatagramPacket() Constructor
Creates a DatagramPacket object. These constructors actually define two different types of DatagramPacket objects: q The first two create DatagramPacket objects that are intended to receive data, and don't require an InetAddress object. q The third and fourth versions create DatagramPacket objects that are intended to send data. The length variable is the number of bytes to read or send and must be less than or equal to the length of the byte array. Specifying an offset allows some of the data to be skipped over when reading or sending. The port number must be between 0 and 65535. Port numbers between 1 and 511 are reserved for well-known services. For instance, port 21 is reserved for FTP and port 23 is reserved for telnet. Port numbers between 512 and 1023 are also reserved and on some systems require super-user privileges to use them. Port numbers between 1024 and 65535 are available for general use. Methods to Return DatagramPacket Properties
getAddress() returns the IP address of the either the machine that sent the packet or the destination address, depending on whether the invoking DatagramPacket object is intended to receive or send data. getData() returns a byte array containing the data associated with the invoking DatagramPacket object. getLength() returns the number of bytes to be sent or received by the invoking DatagramPacket object. getOffset() returns the offset, if any. getPort() returns the port number of either the machine that sent the packet or the destination address, depending on whether the invoking DatagramPacket object is intended to send or receive data. Methods to Set DatagramPacket Properties
setAddress() is used to set or change the destination IP address of the invoking DatagramPacket object. setData() sets the data associated with the invoking DatagramPacket object. setLength() specifies the number of bytes to be sent by the invoking DatagramPacket object. setPort() sets the destination port number of the invoking DatagramPacket object.
5.1.2 DatagramSocket Classpublic class DatagramSocket extends Object Object DatagramSocket A DatagramSocket object represents the mechanism by which packets of data are sent and received using the connectionless protocol. DatagramSocket() Constructor
Creates a DatagramSocket object. The port number and IP address of the local machine can be specified. The port number must be between 0 and 65535. Some of the lower numbers are reserved for system use. (See DatagramPacket() Constructor on page 3.) If no port number is provided, the DatagramSocket object will be assigned any available port on the local machine. close() Method
close() closes the invoking DatagramSocket object and releases any resources assigned to it. Connection Methods
A DatagramSocket object may be connected, if desired, to a particular address and port number, so that it can only send or receive data from that address and port. (This is a completely different type of 'connection' to that of the connection-oriented protocol.) By default, a DatagramSocket object is not connected, and can send or receive data from any source. connect() connects the DatagramSocket object to the specified address and port. disconnect() disconnects the invoking DatagramSocket object. It does nothing if the DatagramSocket object is not connected. getInetAddress() returns an InetAddress object representing the IP address of the remote machine, if the invoking DatagramSocket object is connected, or null if it object is not connected. getPort() returns the port number to which the invoking DatagramSocket object is connected, or 1 if the DatagramSocket object is not connected. Methods to Return DatagramSocket Properties
getLocalAddress() returns an InetAddress object containing the local address to which the invoking DatagramSocket object is bound. getLocalPort() returns the port number to which the invoking DatagramSocket object is bound. getReceiveBufferSize() and getSendBufferSize() return the size of the input and output buffers used by the local machine for this socket. getSoTimeout() returns the time in milliseconds that the invoking DatagramSocket object will wait for an incoming packet of data. Methods to Set DatagramSocket Properties
setReceiveBufferSize() and setSendBufferSize() change the size of the input and output buffers used by the local machine for this socket. The local machine may or may not accept the size specified by these methods. setSoTimeout() changes the time in milliseconds that the invoking DatagramSocket object will wait for an incoming packet of data. Setting the value to 0 indicates that the DatagramSocket object will wait indefinitely. receive() Method
receive() receives a DatagramPacket object from the socket. This method blocks other activity until a packet is received or until the time-out time has elapsed. Once a packet has been received, the method returns. The received packet contains a byte array containing data, the length of the byte array, the sender's IP address, and the sender's port number. send() Method
send() sends a DatagramPacket object into the socket. The DatagramPacket object must contain a byte array containing the data to be transmitted, the length of the byte array, the destination IP address, and the destination port number.
5.1.3 MulticastSocket Classpublic class MulticastSocket extends DatagramSocket Object DatagramSocket MulticastSocket The MulticastSocket class is used to implement multicast data communication using the connectionless protocol. It can be used to send a message to multiple machines, those that are part of the sending machines group. The MulticastSocket object can join or leave groups of other multicast hosts on the Internet. A multicast group is specified by an IP address in a particular range, and by a standard UDP port number. When a MulticastSocket joins a particular multicast group, it receives DatagramPacket objects sent to the group by other hosts (which do not themselves need to be members of the group), as do all other members of the group. MulticastSocket() Constructor
Creates a MulticastSocket object. The MulticastSocket object can be bound to a port on the local host machine by providing the port number to the constructor. joinGroup() Method
joinGroup() causes the invoking MulticastSocket object to join a multicast group. An IOException is thrown if the specified address is not a multicast address. leaveGroup() Method
leaveGroup() causes the invoking MulticastSocket object to leave a multicast group. An IOException is thrown if the specified address is not a multicast address. Methods to Return MulticastSocket Properties
getInterface() returns an InetAddress object containing the IP address the invoking MulticastSocket object will use to send packets to multicast destinations. getTimeToLive() returns the time-to-live (TTL) value of the invoking MulticastSocket object. The TTL value is the number of hops an outgoing packet can experience before it dies. send() Method
send() sends the specified DatagramPacket object from the invoking MulticastSocket using the specified TTL value. A MulticastSocket object can use this method in addition to the the send() method defined in the DatagramSocket class. Methods to Set MulticastSocket Properties
setInterface() specifies the IP address the invoking MulticastSocket object will use to send packets to multicast destinations. setTimeToLive() changes the time-to-live (TTL) value of the invoking MulticastSocket object. Deprecated Methods
These methods have been deprecated as of Java 2 and should not be used for new code. getTTL() returns the time-to-live (TTL) value of the invoking MulticastSocket object as a byte. setTTL() method changes the TTL value of the invoking MulticastSocket object. |