共用方式為


SocketChannel Class

Definition

A selectable channel for stream-oriented connecting sockets.

[Android.Runtime.Register("java/nio/channels/SocketChannel", DoNotGenerateAcw=true)]
public abstract class SocketChannel : Java.Nio.Channels.Spi.AbstractSelectableChannel, IDisposable, Java.Interop.IJavaPeerable, Java.Nio.Channels.IByteChannel, Java.Nio.Channels.IGatheringByteChannel, Java.Nio.Channels.IScatteringByteChannel
[<Android.Runtime.Register("java/nio/channels/SocketChannel", DoNotGenerateAcw=true)>]
type SocketChannel = class
    inherit AbstractSelectableChannel
    interface IByteChannel
    interface IReadableByteChannel
    interface IChannel
    interface ICloseable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface IWritableByteChannel
    interface IGatheringByteChannel
    interface IScatteringByteChannel
Inheritance
Attributes
Implements

Remarks

A selectable channel for stream-oriented connecting sockets.

A socket channel is created by invoking one of the #open open methods of this class. It is not possible to create a channel for an arbitrary, pre-existing socket. A newly-created socket channel is open but not yet connected. An attempt to invoke an I/O operation upon an unconnected channel will cause a NotYetConnectedException to be thrown. A socket channel can be connected by invoking its #connect connect method; once connected, a socket channel remains connected until it is closed. Whether or not a socket channel is connected may be determined by invoking its #isConnected isConnected method.

Socket channels support non-blocking connection:&nbsp;A socket channel may be created and the process of establishing the link to the remote socket may be initiated via the #connect connect method for later completion by the #finishConnect finishConnect method. Whether or not a connection operation is in progress may be determined by invoking the #isConnectionPending isConnectionPending method.

Socket channels support asynchronous shutdown, which is similar to the asynchronous close operation specified in the Channel class. If the input side of a socket is shut down by one thread while another thread is blocked in a read operation on the socket's channel, then the read operation in the blocked thread will complete without reading any bytes and will return -1. If the output side of a socket is shut down by one thread while another thread is blocked in a write operation on the socket's channel, then the blocked thread will receive an AsynchronousCloseException.

Socket options are configured using the #setOption(SocketOption,Object) setOption method. Socket channels support the following options: <blockquote> <table class="striped"> <caption style="display:none">Socket options</caption> <thead> <tr> <th scope="col">Option Name</th> <th scope="col">Description</th> </tr> </thead> <tbody> <tr> <th scope="row"> java.net.StandardSocketOptions#SO_SNDBUF SO_SNDBUF</th> <td> The size of the socket send buffer </td> </tr> <tr> <th scope="row"> java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF</th> <td> The size of the socket receive buffer </td> </tr> <tr> <th scope="row"> java.net.StandardSocketOptions#SO_KEEPALIVE SO_KEEPALIVE</th> <td> Keep connection alive </td> </tr> <tr> <th scope="row"> java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR</th> <td> Re-use address </td> </tr> <tr> <th scope="row"> java.net.StandardSocketOptions#SO_LINGER SO_LINGER</th> <td> Linger on close if data is present (when configured in blocking mode only) </td> </tr> <tr> <th scope="row"> java.net.StandardSocketOptions#TCP_NODELAY TCP_NODELAY</th> <td> Disable the Nagle algorithm </td> </tr> </tbody> </table> </blockquote> Additional (implementation specific) options may also be supported.

Socket channels are safe for use by multiple concurrent threads. They support concurrent reading and writing, though at most one thread may be reading and at most one thread may be writing at any given time. The #connect connect and #finishConnect finishConnect methods are mutually synchronized against each other, and an attempt to initiate a read or write operation while an invocation of one of these methods is in progress will block until that invocation is complete.

Added in 1.4.

Java documentation for java.nio.channels.SocketChannel.

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

SocketChannel(IntPtr, JniHandleOwnership)

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

SocketChannel(SelectorProvider)

Initializes a new instance of this class.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsBlocking

Indicates whether this channel is in blocking mode.

(Inherited from AbstractSelectableChannel)
IsConnected

Indicates whether this channel's socket is connected.

IsConnectionPending

Indicates whether this channel's socket is still trying to connect.

IsOpen

Returns true if this channel is open.

(Inherited from AbstractInterruptibleChannel)
IsRegistered

Indicates whether this channel is registered with one or more selectors.

(Inherited from AbstractSelectableChannel)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
LocalAddress
PeerReference (Inherited from Object)
RemoteAddress
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.

Methods

Begin()

Marks the beginning of an I/O operation that might block indefinitely.

(Inherited from AbstractInterruptibleChannel)
Bind(SocketAddress)

Added in 1.

BlockingLock()

Gets the object used for the synchronization of register and configureBlocking.

(Inherited from AbstractSelectableChannel)
Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Close()

Closes this channel.

(Inherited from AbstractInterruptibleChannel)
ConfigureBlocking(Boolean)

Adjusts this channel's blocking mode.

(Inherited from AbstractSelectableChannel)
Connect(SocketAddress)

Connects this channel's socket.

ConnectAsync(SocketAddress)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
End(Boolean)

Marks the end of an I/O operation that might block indefinitely.

(Inherited from AbstractInterruptibleChannel)
Equals(Object)

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

(Inherited from Object)
FinishConnect()

Finishes the process of connecting a socket channel.

FinishConnectAsync()
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
ImplCloseChannel()

Closes this channel.

(Inherited from AbstractSelectableChannel)
ImplCloseSelectableChannel()

Closes this selectable channel.

(Inherited from AbstractSelectableChannel)
ImplConfigureBlocking(Boolean)

Adjusts this channel's blocking mode.

(Inherited from AbstractSelectableChannel)
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)
KeyFor(Selector)

Gets this channel's selection key for the specified selector.

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

Opens a socket channel.

Open(SocketAddress)

Opens a socket channel and connects it to a remote address.

Provider()

Returns the provider that created this channel.

(Inherited from AbstractSelectableChannel)
Read(ByteBuffer)

Reads bytes from this socket channel into the given buffer.

Read(ByteBuffer[], Int32, Int32)

Reads bytes from this socket channel into a subset of the given buffers.

Read(ByteBuffer[])

Reads bytes from this socket channel and stores them in the specified array of buffers.

Register(Selector, Operations, Object)

Registers this channel with the given selector, returning a selection key.

(Inherited from AbstractSelectableChannel)
Register(Selector, Operations)

Registers this channel with the given selector, returning a selection key.

(Inherited from SelectableChannel)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetOption(ISocketOption, Object)

Added in 1.

ShutdownInput()

Shutdown the connection for reading without closing the channel.

ShutdownOutput()

Shutdown the connection for writing without closing the channel.

Socket()

Retrieves a socket associated with this channel.

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
ValidOps()

Returns an operation set identifying this channel's supported operations.

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)
Write(ByteBuffer)

Writes bytes from the given byte buffer to this socket channel.

Write(ByteBuffer[], Int32, Int32)

Attempts to write a subset of the given bytes from the buffers to this socket channel.

Write(ByteBuffer[])

Writes bytes from all the given byte buffers to this socket channel.

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)
WriteAsync(IGatheringByteChannel, ByteBuffer[], Int32, Int32)
WriteAsync(IGatheringByteChannel, ByteBuffer[])
ReadAsync(IReadableByteChannel, ByteBuffer)
ReadAsync(IScatteringByteChannel, ByteBuffer[], Int32, Int32)
ReadAsync(IScatteringByteChannel, ByteBuffer[])
WriteAsync(IWritableByteChannel, ByteBuffer)

Applies to