共用方式為


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

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

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 (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 (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

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() (Inherited from Object)
Dispose(Boolean) (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>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (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

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to