Package java.net
Class DatagramSocketImpl
- java.lang.Object
-
- java.net.DatagramSocketImpl
-
- All Implemented Interfaces:
SocketOptions
- Direct Known Subclasses:
PlainDatagramSocketImpl
public abstract class DatagramSocketImpl extends Object implements SocketOptions
The abstract superclass for datagram and multicast socket implementations.
-
-
Field Summary
Fields Modifier and Type Field Description protected FileDescriptorfdFile descriptor that is used to address this socket.protected intlocalPortThe number of the local port to which this socket is bound.-
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 DatagramSocketImpl()Constructs an unbound datagram socket implementation.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected abstract voidbind(int port, InetAddress addr)Binds the datagram socket to the given local host/port.protected abstract voidclose()Closes this socket.protected voidconnect(InetAddress inetAddr, int port)Connects this socket to the specified remote address and port.protected abstract voidcreate()This method allocates the socket descriptor in the underlying operating system.protected voiddisconnect()Disconnects this socket from the remote host.protected FileDescriptorgetFileDescriptor()Gets theFileDescriptorof this datagram socket, which is invalid if the socket is closed or not bound.protected intgetLocalPort()Returns the local port to which this socket is bound.protected abstract intgetTimeToLive()Gets the time-to-live (TTL) for multicast packets sent on this socket.protected abstract bytegetTTL()Deprecated.UsegetTimeToLive()instead.protected abstract voidjoin(InetAddress addr)Adds this socket to the multicast groupaddr.protected abstract voidjoinGroup(SocketAddress addr, NetworkInterface netInterface)Adds this socket to the multicast groupaddr.protected abstract voidleave(InetAddress addr)Removes this socket from the multicast groupaddr.protected abstract 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 abstract intpeek(InetAddress sender)Peeks at the incoming packet to this socket and returns the address of thesender.protected abstract intpeekData(DatagramPacket pack)Receives data into the supplied datagram packet by peeking.protected abstract voidreceive(DatagramPacket pack)Receives data and stores it in the supplied datagram packetpack.protected abstract voidsend(DatagramPacket pack)Sends the given datagram packetpack.protected abstract voidsetTimeToLive(int ttl)Sets the time-to-live (TTL) option for multicast packets sent on this socket.protected abstract voidsetTTL(byte ttl)Deprecated.UsesetTimeToLive(int)instead.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.net.SocketOptions
getOption, setOption
-
-
-
-
Field Detail
-
fd
protected FileDescriptor fd
File descriptor that is used to address this socket.
-
localPort
protected int localPort
The number of the local port to which this socket is bound.
-
-
Method Detail
-
bind
protected abstract void bind(int port, InetAddress addr) throws SocketExceptionBinds the datagram socket to the given local host/port. Sockets must be bound prior to attempting to send or receive data.- Parameters:
port- the port on the local host to bind to.addr- 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.
-
close
protected abstract void close()
Closes this socket.
-
create
protected abstract void create() throws SocketExceptionThis method allocates the socket descriptor in the underlying operating system.- Throws:
SocketException- if an error occurs while creating the socket.
-
getFileDescriptor
protected FileDescriptor getFileDescriptor()
Gets theFileDescriptorof this datagram socket, which is invalid if the socket is closed or not bound.- Returns:
- the current file descriptor of this socket.
-
getLocalPort
protected int getLocalPort()
Returns the local port to which this socket is bound.
-
getTTL
@Deprecated protected abstract byte getTTL() throws IOException
Deprecated.UsegetTimeToLive()instead.Gets the time-to-live (TTL) for multicast packets sent on this socket.- 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:
getTimeToLive()
-
getTimeToLive
protected abstract int getTimeToLive() throws IOExceptionGets 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.- Returns:
- the time-to-live option as an integer value.
- Throws:
IOException- if an error occurs while getting the time-to-live option value.
-
join
protected abstract void join(InetAddress addr) throws IOException
Adds 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.- 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
protected abstract void joinGroup(SocketAddress addr, NetworkInterface netInterface) throws IOException
Adds 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.- 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
protected abstract void leave(InetAddress addr) throws IOException
Removes this socket from the multicast groupaddr.- 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
protected abstract void leaveGroup(SocketAddress addr, NetworkInterface netInterface) throws IOException
Removes this socket from the multicast groupaddr.- 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 abstract int peek(InetAddress sender) throws IOException
Peeks 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.- 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
protected abstract void receive(DatagramPacket pack) throws IOException
Receives 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.- Parameters:
pack- the datagram packet container to fill in the received data.- Throws:
IOException- if an error or timeout occurs while receiving data.
-
send
protected abstract void send(DatagramPacket pack) throws IOException
Sends the given datagram packetpack. The packet contains the data and the address and port information of the target host as well.- Parameters:
pack- the datagram packet to be sent.- Throws:
IOException- if an error occurs while sending the packet.
-
setTimeToLive
protected abstract void setTimeToLive(int ttl) throws IOExceptionSets the time-to-live (TTL) option for multicast packets sent on this socket.- 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
@Deprecated protected abstract void setTTL(byte ttl) throws IOException
Deprecated.UsesetTimeToLive(int)instead.Sets the time-to-live (TTL) option for multicast packets sent on this socket.- 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:
setTimeToLive(int)
-
connect
protected void connect(InetAddress inetAddr, int port) throws SocketException
Connects this socket to the specified remote address and port.- 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
protected void disconnect()
Disconnects this socket from the remote host.
-
peekData
protected abstract int peekData(DatagramPacket pack) throws IOException
Receives 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.- 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.
-
onBind
protected void onBind(InetAddress localAddress, int localPort)
Initialize the bind() state.
-
onConnect
protected void onConnect(InetAddress remoteAddress, int remotePort)
Initialize the connect() state.
-
onDisconnect
protected void onDisconnect()
Initialize the disconnected state.
-
onClose
protected void onClose()
Initialize the closed state.
-
-