Class PlainDatagramSocketImpl
- java.lang.Object
-
- java.net.DatagramSocketImpl
-
- java.net.PlainDatagramSocketImpl
-
- All Implemented Interfaces:
SocketOptions
public class PlainDatagramSocketImpl extends DatagramSocketImpl
-
-
Field Summary
-
Fields inherited from class java.net.DatagramSocketImpl
fd, localPort
-
Fields 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_REUSEPORT, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY
-
-
Constructor Summary
Constructors Constructor Description PlainDatagramSocketImpl()PlainDatagramSocketImpl(FileDescriptor fd, int localPort)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbind(int port, InetAddress address)Binds the datagram socket to the given local host/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 voidonBind(InetAddress localAddress, int localPort)Initialize the bind() state.protected voidonClose()Initialize the closed state.protected voidonConnect(InetAddress remoteAddress, int remotePort)Initialize the connect() state.protected voidonDisconnect()Initialize the disconnected state.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 Detail
-
PlainDatagramSocketImpl
public PlainDatagramSocketImpl(FileDescriptor fd, int localPort)
-
PlainDatagramSocketImpl
public PlainDatagramSocketImpl()
-
-
Method Detail
-
bind
public void bind(int port, InetAddress address) throws SocketExceptionDescription copied from class:DatagramSocketImplBinds the datagram socket to the given local host/port. Sockets must be bound prior to attempting to send or receive data.- Specified by:
bindin classDatagramSocketImpl- Parameters:
port- the port on the local host to bind to.address- the address on the multihomed local host to bind to.- Throws:
SocketException- if an error occurs while binding, for example, if the port has been already bound.
-
onBind
protected void onBind(InetAddress localAddress, int localPort)
Description copied from class:DatagramSocketImplInitialize the bind() state.- Overrides:
onBindin classDatagramSocketImpl
-
close
public void close()
Description copied from class:DatagramSocketImplCloses this socket.- Specified by:
closein classDatagramSocketImpl
-
onClose
protected void onClose()
Description copied from class:DatagramSocketImplInitialize the closed state.- Overrides:
onClosein classDatagramSocketImpl
-
create
public void create() throws SocketExceptionDescription 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
protected void finalize() throws ThrowableDescription 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
public Object getOption(int option) throws SocketException
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
public int getTimeToLive() throws IOExceptionDescription 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
public byte getTTL() throws IOExceptionDescription 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
public void join(InetAddress addr) throws IOException
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
public void joinGroup(SocketAddress addr, NetworkInterface netInterface) throws IOException
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
public void leave(InetAddress addr) throws IOException
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
public void leaveGroup(SocketAddress addr, NetworkInterface netInterface) throws IOException
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
protected int peek(InetAddress sender) throws IOException
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
public void receive(DatagramPacket pack) throws IOException
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
public int peekData(DatagramPacket pack) throws IOException
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
public void send(DatagramPacket packet) throws IOException
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
public void setOption(int option, Object value) throws SocketExceptionDescription 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
public void setTimeToLive(int ttl) throws IOExceptionDescription 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
public void setTTL(byte ttl) throws IOExceptionDescription 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
public void connect(InetAddress inetAddr, int port) throws SocketException
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.
-
onConnect
protected void onConnect(InetAddress remoteAddress, int remotePort)
Description copied from class:DatagramSocketImplInitialize the connect() state.- Overrides:
onConnectin classDatagramSocketImpl
-
disconnect
public void disconnect()
Description copied from class:DatagramSocketImplDisconnects this socket from the remote host.- Overrides:
disconnectin classDatagramSocketImpl
-
onDisconnect
protected void onDisconnect()
Description copied from class:DatagramSocketImplInitialize the disconnected state.- Overrides:
onDisconnectin classDatagramSocketImpl
-
-