Skip to content

Most visited

Recently visited

navigation
Added in API level 1

DatagramSocketImpl

public abstract class DatagramSocketImpl
extends Object implements SocketOptions

java.lang.Object
   ↳ java.net.DatagramSocketImpl


The abstract superclass for datagram and multicast socket implementations.

Summary

Inherited constants

From interface java.net.SocketOptions

Fields

protected FileDescriptor fd

File descriptor that is used to address this socket.

protected int localPort

The number of the local port to which this socket is bound.

Public constructors

DatagramSocketImpl()

Constructs an unbound datagram socket implementation.

Protected methods

abstract void bind(int port, InetAddress addr)

Binds the datagram socket to the given local host/port.

abstract void close()

Closes this socket.

void connect(InetAddress inetAddr, int port)

Connects this socket to the specified remote address and port.

abstract void create()

This method allocates the socket descriptor in the underlying operating system.

void disconnect()

Disconnects this socket from the remote host.

FileDescriptor getFileDescriptor()

Gets the FileDescriptor of this datagram socket, which is invalid if the socket is closed or not bound.

int getLocalPort()

Returns the local port to which this socket is bound.

abstract byte getTTL()

This method was deprecated in API level 1. Use getTimeToLive() instead.

abstract int getTimeToLive()

Gets the time-to-live (TTL) for multicast packets sent on this socket.

abstract void join(InetAddress addr)

Adds this socket to the multicast group addr.

abstract void joinGroup(SocketAddress addr, NetworkInterface netInterface)

Adds this socket to the multicast group addr.

abstract void leave(InetAddress addr)

Removes this socket from the multicast group addr.

abstract void leaveGroup(SocketAddress addr, NetworkInterface netInterface)

Removes this socket from the multicast group addr.

abstract int peek(InetAddress sender)

Peeks at the incoming packet to this socket and returns the address of the sender.

abstract int peekData(DatagramPacket pack)

Receives data into the supplied datagram packet by peeking.

abstract void receive(DatagramPacket pack)

Receives data and stores it in the supplied datagram packet pack.

abstract void send(DatagramPacket pack)

Sends the given datagram packet pack.

abstract void setTTL(byte ttl)

This method was deprecated in API level 1. Use setTimeToLive(int) instead.

abstract void setTimeToLive(int ttl)

Sets the time-to-live (TTL) option for multicast packets sent on this socket.

Inherited methods

From class java.lang.Object
From interface java.net.SocketOptions

Fields

fd

Added in API level 1
FileDescriptor fd

File descriptor that is used to address this socket.

localPort

Added in API level 1
int localPort

The number of the local port to which this socket is bound.

Public constructors

DatagramSocketImpl

Added in API level 1
DatagramSocketImpl ()

Constructs an unbound datagram socket implementation.

Protected methods

bind

Added in API level 1
void bind (int port, 
                InetAddress addr)

Binds the datagram socket to the given local host/port. Sockets must be bound prior to attempting to send or receive data.

Parameters
port int: the port on the local host to bind to.
addr InetAddress: 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

Added in API level 1
void close ()

Closes this socket.

connect

Added in API level 1
void connect (InetAddress inetAddr, 
                int port)

Connects this socket to the specified remote address and port.

Parameters
inetAddr InetAddress: the address of the target host which has to be connected.
port int: 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.

create

Added in API level 1
void create ()

This method allocates the socket descriptor in the underlying operating system.

Throws
SocketException if an error occurs while creating the socket.

disconnect

Added in API level 1
void disconnect ()

Disconnects this socket from the remote host.

getFileDescriptor

Added in API level 1
FileDescriptor getFileDescriptor ()

Gets the FileDescriptor of this datagram socket, which is invalid if the socket is closed or not bound.

Returns
FileDescriptor the current file descriptor of this socket.

getLocalPort

Added in API level 1
int getLocalPort ()

Returns the local port to which this socket is bound.

Returns
int

getTTL

Added in API level 1
byte getTTL ()

This method was deprecated in API level 1.
Use getTimeToLive() instead.

Gets the time-to-live (TTL) for multicast packets sent on this socket.

Returns
byte 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

Added in API level 1
int getTimeToLive ()

Gets 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
int the time-to-live option as an integer value.
Throws
IOException if an error occurs while getting the time-to-live option value.

join

Added in API level 1
void join (InetAddress addr)

Adds this socket to the multicast group addr. 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 InetAddress: the multicast group to which this socket has to be joined.
Throws
IOException if an error occurs while joining the specified multicast group.

joinGroup

Added in API level 1
void joinGroup (SocketAddress addr, 
                NetworkInterface netInterface)

Adds this socket to the multicast group addr. 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 SocketAddress: the multicast group to which this socket has to be joined.
netInterface NetworkInterface: the local network interface which will receive the multicast datagram packets.
Throws
IOException if an error occurs while joining the specified multicast group.

leave

Added in API level 1
void leave (InetAddress addr)

Removes this socket from the multicast group addr.

Parameters
addr InetAddress: the multicast group to be left.
Throws
IOException if an error occurs while leaving the group or no multicast address was assigned.

leaveGroup

Added in API level 1
void leaveGroup (SocketAddress addr, 
                NetworkInterface netInterface)

Removes this socket from the multicast group addr.

Parameters
addr SocketAddress: the multicast group to be left.
netInterface NetworkInterface: the local network interface on which this socket has to be removed.
Throws
IOException if an error occurs while leaving the group.

peek

Added in API level 1
int peek (InetAddress sender)

Peeks at the incoming packet to this socket and returns the address of the sender. The method will block until a packet is received or timeout expires.

Parameters
sender InetAddress: the origin address of a packet.
Returns
int the address of sender as an integer value.
Throws
IOException if an error or a timeout occurs while reading the address.

peekData

Added in API level 1
int peekData (DatagramPacket pack)

Receives data into the supplied datagram packet by peeking. The data is not removed from socket buffer and can be received again by another peekData() or receive() call. This call blocks until either data has been received or, if a timeout is set, the timeout has been expired.

Parameters
pack DatagramPacket: the datagram packet used to store the data.
Returns
int the port the packet was received from.
Throws
IOException if an error occurs while peeking at the data.

receive

Added in API level 1
void receive (DatagramPacket pack)

Receives data and stores it in the supplied datagram packet pack. This call will block until either data has been received or, if a timeout is set, the timeout has expired. If the timeout expires an InterruptedIOException is thrown.

Parameters
pack DatagramPacket: the datagram packet container to fill in the received data.
Throws
IOException if an error or timeout occurs while receiving data.

send

Added in API level 1
void send (DatagramPacket pack)

Sends the given datagram packet pack. The packet contains the data and the address and port information of the target host as well.

Parameters
pack DatagramPacket: the datagram packet to be sent.
Throws
IOException if an error occurs while sending the packet.

setTTL

Added in API level 1
void setTTL (byte ttl)

This method was deprecated in API level 1.
Use setTimeToLive(int) instead.

Sets the time-to-live (TTL) option for multicast packets sent on this socket.

Parameters
ttl byte: 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

Added in API level 1
void setTimeToLive (int ttl)

Sets the time-to-live (TTL) option for multicast packets sent on this socket.

Parameters
ttl int: the time-to-live option value. Valid values are 0 < ttl <= 255.
Throws
IOException if an error occurs while setting the option.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.