DatagramSocket Class

Definition

This class represents a socket for sending and receiving datagram packets.

[Android.Runtime.Register("java/net/DatagramSocket", DoNotGenerateAcw=true)]
public class DatagramSocket : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ICloseable
[<Android.Runtime.Register("java/net/DatagramSocket", DoNotGenerateAcw=true)>]
type DatagramSocket = class
    inherit Object
    interface ICloseable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Inheritance
DatagramSocket
Derived
Attributes
Implements

Remarks

This class represents a socket for sending and receiving datagram packets.

A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.

Where possible, a newly constructed DatagramSocket has the SocketOptions#SO_BROADCAST SO_BROADCAST socket option enabled so as to allow the transmission of broadcast datagrams. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.

Example: DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress(8888)); Which is equivalent to: DatagramSocket s = new DatagramSocket(8888); Both cases will create a DatagramSocket able to receive broadcasts on UDP port 8888.

Added in JDK1.0.

Java documentation for java.net.DatagramSocket.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

Name Description
DatagramSocket()

Constructs a datagram socket and binds it to any available port on the local host machine.

DatagramSocket(DatagramSocketImpl)

Creates an unbound datagram socket with the specified DatagramSocketImpl.

DatagramSocket(Int32, InetAddress)

Creates a datagram socket, bound to the specified local address.

DatagramSocket(Int32)

Constructs a datagram socket and binds it to the specified port on the local host machine.

DatagramSocket(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

DatagramSocket(SocketAddress)

Creates a datagram socket, bound to the specified local socket address.

Properties

Name Description
Broadcast

Tests if SO_BROADCAST is enabled. -or- Enable/disable SO_BROADCAST.

Channel

Returns the unique java.nio.channels.DatagramChannel object associated with this datagram socket, if any.

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
InetAddress

Returns the address to which this socket is connected.

IsBound

Returns the binding state of the socket.

IsClosed

Returns whether the socket is closed or not.

IsConnected

Returns the connection state of the socket.

JniIdentityHashCode

Gets the identity hash code assigned to this Java peer by the interop runtime.

(Inherited from Object)
JniPeerMembers
LocalAddress

Gets the local address to which the socket is bound.

LocalPort

Returns the port number on the local host to which this socket is bound.

LocalSocketAddress

Returns the address of the endpoint this socket is bound to.

PeerReference

Gets the JNI object reference for this Java peer.

(Inherited from Object)
Port

Returns the port number to which this socket is connected.

ReceiveBufferSize

Get value of the SO_RCVBUF option for this DatagramSocket, that is the buffer size used by the platform for input on this DatagramSocket. -or- Sets the SO_RCVBUF option to the specified value for this DatagramSocket.

RemoteSocketAddress

Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

ReuseAddress

Tests if SO_REUSEADDR is enabled. -or- Enable/disable the SO_REUSEADDR socket option.

SendBufferSize

Get value of the SO_SNDBUF option for this DatagramSocket, that is the buffer size used by the platform for output on this DatagramSocket. -or- Sets the SO_SNDBUF option to the specified value for this DatagramSocket.

SoTimeout

Retrieve setting for SO_TIMEOUT. -or- Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

TrafficClass

Gets traffic class or type-of-service in the IP datagram header for packets sent from this DatagramSocket. -or- Sets traffic class or type-of-service octet in the IP datagram header for datagrams sent from this DatagramSocket.

Methods

Name Description
Bind(SocketAddress)

Binds this DatagramSocket to a specific address and port.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Close()

Closes this datagram socket.

Connect(InetAddress, Int32)

Connects the socket to a remote address for this socket.

Connect(SocketAddress)

Connects this socket to a remote socket address (IP address + port number).

ConnectAsync(InetAddress, Int32)
ConnectAsync(SocketAddress)
Disconnect()

Disconnects the socket.

Dispose()

Releases the resources held by this Java peer.

(Inherited from Object)
Dispose(Boolean)

Releases the resources held by this Java peer.

(Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetOption(ISocketOption)

Returns the value of a socket option.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Receive(DatagramPacket)

Receives a datagram packet from this socket.

ReceiveAsync(DatagramPacket)
Send(DatagramPacket)

Sends a datagram packet from this socket.

SendAsync(DatagramPacket)
SetDatagramSocketImplFactory(IDatagramSocketImplFactory)

Sets the datagram socket implementation factory for the application.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetOption(ISocketOption, Object)

Sets the value of a socket option.

SupportedOptions()

Returns a set of the socket options supported by this socket.

ToArray<T>()

Creates a managed array from this Java array wrapper.

(Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime()

Unregisters this Java peer from the interop runtime.

(Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

Name Description
IJavaPeerable.Disposed()

Notifies the interop runtime that this managed peer has been disposed.

(Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced()

Releases this Java peer unless it is retained by another managed reference.

(Inherited from Object)
IJavaPeerable.Finalized()

Notifies the interop runtime that this managed peer has been finalized.

(Inherited from Object)
IJavaPeerable.JniManagedPeerState

Gets the state that describes the relationship between this managed peer and its JNI reference.

(Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32)

Sets the interop identity hash code for this Java peer.

(Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates)

Sets the managed and JNI peer state for this Java peer.

(Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference)

Sets the JNI object reference used by this managed peer.

(Inherited from Object)

Extension Methods

Name Description
GetJniTypeName(IJavaPeerable)

Gets the JNI name of the type of the instance self.

JavaAs<TResult>(IJavaPeerable)

Try to coerce self to type TResult, checking that the coercion is valid on the Java side.

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
TryJavaCast<TResult>(IJavaPeerable, TResult)

Try to coerce self to type TResult, checking that the coercion is valid on the Java side.

Applies to