Condividi tramite


MulticastSocket Class

Definition

The multicast datagram socket class is useful for sending and receiving IP multicast packets.

[Android.Runtime.Register("java/net/MulticastSocket", DoNotGenerateAcw=true)]
public class MulticastSocket : Java.Net.DatagramSocket
[<Android.Runtime.Register("java/net/MulticastSocket", DoNotGenerateAcw=true)>]
type MulticastSocket = class
    inherit DatagramSocket
Inheritance
MulticastSocket
Attributes

Remarks

The multicast datagram socket class is useful for sending and receiving IP multicast packets. A MulticastSocket is a (UDP) DatagramSocket, with additional capabilities for joining "groups" of other multicast hosts on the internet.

A multicast group is specified by a class D IP address and by a standard UDP port number. Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive. The address 224.0.0.0 is reserved and should not be used.

One would join a multicast group by first creating a MulticastSocket with the desired port, then invoking the joinGroup(InetAddress groupAddr) method:

// join a Multicast group and send the group salutations
            ...
            String msg = "Hello";
            InetAddress group = InetAddress.getByName("228.5.6.7");
            MulticastSocket s = new MulticastSocket(6789);
            s.joinGroup(group);
            byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
            DatagramPacket hi = new DatagramPacket(bytes, bytes.length,
                                        group, 6789);
            s.send(hi);
            // get their responses!
            byte[] buf = new byte[1000];
            DatagramPacket recv = new DatagramPacket(buf, buf.length);
            s.receive(recv);
            ...
            // OK, I'm done talking - leave the group...
            s.leaveGroup(group);

When one sends a message to a multicast group, <B>all</B> subscribing recipients to that host and port receive the message (within the time-to-live range of the packet, see below). The socket needn't be a member of the multicast group to send messages to it.

When a socket subscribes to a multicast group/port, it receives datagrams sent by other hosts to the group/port, as do all other members of the group and port. A socket relinquishes membership in a group by the leaveGroup(InetAddress addr) method. <B> Multiple MulticastSocket's</B> may subscribe to a multicast group and port concurrently, and they will all receive group datagrams.

Currently applets are not allowed to use multicast sockets.

Added in 1.1.

Java documentation for java.net.MulticastSocket.

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

MulticastSocket()

Create a multicast socket.

MulticastSocket(Int32)

Create a multicast socket and bind it to a specific port.

MulticastSocket(IntPtr, JniHandleOwnership)

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

MulticastSocket(SocketAddress)

Create a MulticastSocket bound to the specified socket address.

Properties

Broadcast

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

(Inherited from DatagramSocket)
Channel

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

(Inherited from DatagramSocket)
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.

(Inherited from DatagramSocket)
Interface

Retrieve the address of the network interface used for multicast packets. -or- Set the multicast network interface used by methods whose behavior would be affected by the value of the network interface.

IsBound

Returns the binding state of the socket.

(Inherited from DatagramSocket)
IsClosed

Returns whether the socket is closed or not.

(Inherited from DatagramSocket)
IsConnected

Returns the connection state of the socket.

(Inherited from DatagramSocket)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
LocalAddress

Gets the local address to which the socket is bound.

(Inherited from DatagramSocket)
LocalPort

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

(Inherited from DatagramSocket)
LocalSocketAddress

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

(Inherited from DatagramSocket)
LoopbackMode

Get the setting for local loopback of multicast datagrams. -or- Disable/Enable local loopback of multicast datagrams The option is used by the platform's networking code as a hint for setting whether multicast data will be looped back to the local socket.

NetworkInterface

Get the multicast network interface set. -or- Specify the network interface for outgoing multicast datagrams sent on this socket.

PeerReference (Inherited from Object)
Port

Returns the port number to which this socket is connected.

(Inherited from DatagramSocket)
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.

(Inherited from DatagramSocket)
RemoteSocketAddress

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

(Inherited from DatagramSocket)
ReuseAddress

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

(Inherited from DatagramSocket)
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.

(Inherited from DatagramSocket)
SoTimeout

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

(Inherited from DatagramSocket)
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.

TimeToLive

Get the default time-to-live for multicast packets sent out on the socket. -or- Set the default time-to-live for multicast packets sent out on this MulticastSocket in order to control the scope of the multicasts.

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.

(Inherited from DatagramSocket)
TTL
Obsolete.

Get the default time-to-live for multicast packets sent out on the socket. -or- Set the default time-to-live for multicast packets sent out on this MulticastSocket in order to control the scope of the multicasts.

Methods

Bind(SocketAddress)

Binds this DatagramSocket to a specific address and port.

(Inherited from DatagramSocket)
Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Close()

Closes this datagram socket.

(Inherited from DatagramSocket)
Connect(InetAddress, Int32)

Connects the socket to a remote address for this socket.

(Inherited from DatagramSocket)
Connect(SocketAddress)

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

(Inherited from DatagramSocket)
ConnectAsync(InetAddress, Int32) (Inherited from DatagramSocket)
ConnectAsync(SocketAddress) (Inherited from DatagramSocket)
Disconnect()

Disconnects the socket.

(Inherited from DatagramSocket)
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.

(Inherited from DatagramSocket)
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)
JoinGroup(InetAddress)

Joins a multicast group.

JoinGroup(SocketAddress, NetworkInterface)

Joins the specified multicast group at the specified interface.

LeaveGroup(InetAddress)

Leave a multicast group.

LeaveGroup(SocketAddress, NetworkInterface)

Leave a multicast group on a specified local interface.

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.

(Inherited from DatagramSocket)
ReceiveAsync(DatagramPacket) (Inherited from DatagramSocket)
Send(DatagramPacket, SByte)
Obsolete.

Sends a datagram packet to the destination, with a TTL (time- to-live) other than the default for the socket.

Send(DatagramPacket)

Sends a datagram packet from this socket.

(Inherited from DatagramSocket)
SendAsync(DatagramPacket) (Inherited from DatagramSocket)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetOption(ISocketOption, Object)

Sets the value of a socket option.

(Inherited from DatagramSocket)
SupportedOptions()

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

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