Class PlainDatagramSocketImpl
- All Implemented Interfaces:
SocketOptions
public class PlainDatagramSocketImpl extends DatagramSocketImpl
-
Field Summary
Fields inherited from class java.net.DatagramSocketImpl
fd, localPortFields inherited from interface java.net.SocketOptions
IP_MULTICAST_IF, IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS, SO_BINDADDR, SO_BROADCAST, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY -
Constructor Summary
Constructors Constructor Description PlainDatagramSocketImpl()PlainDatagramSocketImpl(FileDescriptor fd, int localPort) -
Method Summary
Modifier and Type Method Description voidbind(int port, InetAddress address)Binds the datagram socket to the given localhost/port.voidclose()Closes this socket.voidconnect(InetAddress inetAddr, int port)Connects this socket to the specified remote address and port.voidcreate()This method allocates the socket descriptor in the underlying operating system.voiddisconnect()Disconnects this socket from the remote host.protected voidfinalize()Invoked when the garbage collector has detected that this instance is no longer reachable.ObjectgetOption(int option)Gets the value for the specified socket option.intgetTimeToLive()Gets the time-to-live (TTL) for multicast packets sent on this socket.bytegetTTL()Gets the time-to-live (TTL) for multicast packets sent on this socket.voidjoin(InetAddress addr)Adds this socket to the multicast groupaddr.voidjoinGroup(SocketAddress addr, NetworkInterface netInterface)Adds this socket to the multicast groupaddr.voidleave(InetAddress addr)Removes this socket from the multicast groupaddr.voidleaveGroup(SocketAddress addr, NetworkInterface netInterface)Removes this socket from the multicast groupaddr.protected intpeek(InetAddress sender)Peeks at the incoming packet to this socket and returns the address of thesender.intpeekData(DatagramPacket pack)Receives data into the supplied datagram packet by peeking.voidreceive(DatagramPacket pack)Receives data and stores it in the supplied datagram packetpack.voidsend(DatagramPacket packet)Sends the given datagram packetpack.voidsetOption(int option, Object value)Sets the value of the specified socket option.voidsetTimeToLive(int ttl)Sets the time-to-live (TTL) option for multicast packets sent on this socket.voidsetTTL(byte ttl)Sets the time-to-live (TTL) option for multicast packets sent on this socket.Methods inherited from class java.net.DatagramSocketImpl
getFileDescriptor, getLocalPort
-
Constructor Details
-
PlainDatagramSocketImpl
-
PlainDatagramSocketImpl
public PlainDatagramSocketImpl()
-
-
Method Details
-
bind
Description copied from class:DatagramSocketImplBinds the datagram socket to the given localhost/port. Sockets must be bound prior to attempting to send or receive data.- Specified by:
bindin classDatagramSocketImpl- Parameters:
port- the port on the localhost to bind.address- the address on the multihomed localhost to bind.- Throws:
SocketException- if an error occurs while binding, for example, if the port has been already bound.
-
close
public void close()Description copied from class:DatagramSocketImplCloses this socket.- Specified by:
closein classDatagramSocketImpl
-
create
Description copied from class:DatagramSocketImplThis method allocates the socket descriptor in the underlying operating system.- Specified by:
createin classDatagramSocketImpl- Throws:
SocketException- if an error occurs while creating the socket.
-
finalize
Description copied from class:ObjectInvoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.Note that objects that override
finalizeare significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicitclosemethod (and implementCloseable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like aBigIntegerwhere typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.If you must use finalizers, consider at least providing your own
ReferenceQueueand having your own thread process that queue.Unlike constructors, finalizers are not automatically chained. You are responsible for calling
super.finalize()yourself.Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.
-
getOption
Description copied from interface:SocketOptionsGets the value for the specified socket option.- Parameters:
option- the option identifier.- Returns:
- the option value.
- Throws:
SocketException- if an error occurs reading the option value.
-
getTimeToLive
Description copied from class:DatagramSocketImplGets the time-to-live (TTL) for multicast packets sent on this socket. The TTL option defines how many routers a packet may be pass before it is discarded.- Specified by:
getTimeToLivein classDatagramSocketImpl- Returns:
- the time-to-live option as an integer value.
- Throws:
IOException- if an error occurs while getting the time-to-live option value.
-
getTTL
Description copied from class:DatagramSocketImplGets the time-to-live (TTL) for multicast packets sent on this socket.- Specified by:
getTTLin classDatagramSocketImpl- Returns:
- the time-to-live option as a byte value.
- Throws:
IOException- if an error occurs while getting the time-to-live option value.- See Also:
DatagramSocketImpl.getTimeToLive()
-
join
Description copied from class:DatagramSocketImplAdds this socket to the multicast groupaddr. A socket must join a group before being able to receive data. Further, a socket may be a member of multiple groups but may join any group only once.- Specified by:
joinin classDatagramSocketImpl- Parameters:
addr- the multicast group to which this socket has to be joined.- Throws:
IOException- if an error occurs while joining the specified multicast group.
-
joinGroup
Description copied from class:DatagramSocketImplAdds this socket to the multicast groupaddr. A socket must join a group before being able to receive data. Further, a socket may be a member of multiple groups but may join any group only once.- Specified by:
joinGroupin classDatagramSocketImpl- Parameters:
addr- the multicast group to which this socket has to be joined.netInterface- the local network interface which will receive the multicast datagram packets.- Throws:
IOException- if an error occurs while joining the specified multicast group.
-
leave
Description copied from class:DatagramSocketImplRemoves this socket from the multicast groupaddr.- Specified by:
leavein classDatagramSocketImpl- Parameters:
addr- the multicast group to be left.- Throws:
IOException- if an error occurs while leaving the group or no multicast address was assigned.
-
leaveGroup
Description copied from class:DatagramSocketImplRemoves this socket from the multicast groupaddr.- Specified by:
leaveGroupin classDatagramSocketImpl- Parameters:
addr- the multicast group to be left.netInterface- the local network interface on which this socket has to be removed.- Throws:
IOException- if an error occurs while leaving the group.
-
peek
Description copied from class:DatagramSocketImplPeeks at the incoming packet to this socket and returns the address of thesender. The method will block until a packet is received or timeout expires.- Specified by:
peekin classDatagramSocketImpl- Parameters:
sender- the origin address of a packet.- Returns:
- the address of
senderas an integer value. - Throws:
IOException- if an error or a timeout occurs while reading the address.
-
receive
Description copied from class:DatagramSocketImplReceives data and stores it in the supplied datagram packetpack. This call will block until either data has been received or, if a timeout is set, the timeout has expired. If the timeout expires anInterruptedIOExceptionis thrown.- Specified by:
receivein classDatagramSocketImpl- Parameters:
pack- the datagram packet container to fill in the received data.- Throws:
IOException- if an error or timeout occurs while receiving data.
-
peekData
Description copied from class:DatagramSocketImplReceives data into the supplied datagram packet by peeking. The data is not removed from socket buffer and can be received again by anotherpeekData()orreceive()call. This call blocks until either data has been received or, if a timeout is set, the timeout has been expired.- Specified by:
peekDatain classDatagramSocketImpl- Parameters:
pack- the datagram packet used to store the data.- Returns:
- the port the packet was received from.
- Throws:
IOException- if an error occurs while peeking at the data.
-
send
Description copied from class:DatagramSocketImplSends the given datagram packetpack. The packet contains the data and the address and port information of the target host as well.- Specified by:
sendin classDatagramSocketImpl- Parameters:
packet- the datagram packet to be sent.- Throws:
IOException- if an error occurs while sending the packet.
-
setOption
Description copied from interface:SocketOptionsSets the value of the specified socket option.- Parameters:
option- the option identifier.value- the value to be set for the option.- Throws:
SocketException- if an error occurs setting the option value.
-
setTimeToLive
Description copied from class:DatagramSocketImplSets the time-to-live (TTL) option for multicast packets sent on this socket.- Specified by:
setTimeToLivein classDatagramSocketImpl- Parameters:
ttl- the time-to-live option value. Valid values are 0 < ttl <= 255.- Throws:
IOException- if an error occurs while setting the option.
-
setTTL
Description copied from class:DatagramSocketImplSets the time-to-live (TTL) option for multicast packets sent on this socket.- Specified by:
setTTLin classDatagramSocketImpl- Parameters:
ttl- the time-to-live option value. Valid values are 0 < ttl <= 255.- Throws:
IOException- if an error occurs while setting the option.- See Also:
DatagramSocketImpl.setTimeToLive(int)
-
connect
Description copied from class:DatagramSocketImplConnects this socket to the specified remote address and port.- Overrides:
connectin classDatagramSocketImpl- Parameters:
inetAddr- the address of the target host which has to be connected.port- the port on the target host which has to be connected.- Throws:
SocketException- if the datagram socket cannot be connected to the specified remote address and port.
-
disconnect
public void disconnect()Description copied from class:DatagramSocketImplDisconnects this socket from the remote host.- Overrides:
disconnectin classDatagramSocketImpl
-