Java Doc for SocketChannel.java in  » 6.0-JDK-Core » io-nio » java » nio » channels » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » io nio » java.nio.channels 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.nio.channels.spi.AbstractInterruptibleChannel
      java.nio.channels.SelectableChannel
         java.nio.channels.spi.AbstractSelectableChannel
            java.nio.channels.SocketChannel

SocketChannel
abstract public class SocketChannel extends AbstractSelectableChannel implements ByteChannel,ScatteringByteChannel,GatheringByteChannel(Code)
A selectable channel for stream-oriented connecting sockets.

Socket channels are not a complete abstraction of connecting network sockets. Binding, shutdown, and the manipulation of socket options must be done through an associated java.net.Socket object obtained by invoking the SocketChannel.socket() socket method. It is not possible to create a channel for an arbitrary, pre-existing socket, nor is it possible to specify the java.net.SocketImpl object to be used by a socket associated with a socket channel.

A socket channel is created by invoking one of the SocketChannel.open open methods of this class. 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 SocketChannel.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 SocketChannel.isConnected isConnected method.

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

The input and output sides of a socket channel may independently be shut down without actually closing the channel. Shutting down the input side of a channel by invoking the java.net.Socket.shutdownInputshutdownInput method of an associated socket object will cause further reads on the channel to return -1, the end-of-stream indication. Shutting down the output side of the channel by invoking the java.net.Socket.shutdownOutput shutdownOutput method of an associated socket object will cause further writes on the channel to throw a ClosedChannelException .

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 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 SocketChannel.connect connect and SocketChannel.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.


author:
   Mark Reinhold
author:
   JSR-51 Expert Group
version:
   1.41, 07/05/06
since:
   1.4



Constructor Summary
protected  SocketChannel(SelectorProvider provider)
     Initializes a new instance of this class.

Method Summary
abstract public  booleanconnect(SocketAddress remote)
     Connects this channel's socket.

If this channel is in non-blocking mode then an invocation of this method initiates a non-blocking connection operation.

abstract public  booleanfinishConnect()
     Finishes the process of connecting a socket channel.

A non-blocking connection operation is initiated by placing a socket channel in non-blocking mode and then invoking its SocketChannel.connectconnect method.

abstract public  booleanisConnected()
     Tells whether or not this channel's network socket is connected.
abstract public  booleanisConnectionPending()
     Tells whether or not a connection operation is in progress on this channel.
public static  SocketChannelopen()
     Opens a socket channel.

The new channel is created by invoking the java.nio.channels.spi.SelectorProvider.openSocketChannelopenSocketChannel method of the system-wide default java.nio.channels.spi.SelectorProvider object.

public static  SocketChannelopen(SocketAddress remote)
     Opens a socket channel and connects it to a remote address.

This convenience method works as if by invoking the SocketChannel.open() method, invoking the SocketChannel.connect(SocketAddress) connect method upon the resulting socket channel, passing it remote, and then returning that channel.

abstract public  intread(ByteBuffer dst)
    
abstract public  longread(ByteBuffer[] dsts, int offset, int length)
    
final public  longread(ByteBuffer[] dsts)
    
abstract public  Socketsocket()
     Retrieves a socket associated with this channel.

The returned object will not declare any public methods that are not declared in the java.net.Socket class.

final public  intvalidOps()
     Returns an operation set identifying this channel's supported operations.

Socket channels support connecting, reading, and writing, so this method returns ( SelectionKey.OP_CONNECT |  SelectionKey.OP_READ |  SelectionKey.OP_WRITE ).

abstract public  intwrite(ByteBuffer src)
    
abstract public  longwrite(ByteBuffer[] srcs, int offset, int length)
    
final public  longwrite(ByteBuffer[] srcs)
    


Constructor Detail
SocketChannel
protected SocketChannel(SelectorProvider provider)(Code)
Initializes a new instance of this class.




Method Detail
connect
abstract public boolean connect(SocketAddress remote) throws IOException(Code)
Connects this channel's socket.

If this channel is in non-blocking mode then an invocation of this method initiates a non-blocking connection operation. If the connection is established immediately, as can happen with a local connection, then this method returns true. Otherwise this method returns false and the connection operation must later be completed by invoking the SocketChannel.finishConnect finishConnect method.

If this channel is in blocking mode then an invocation of this method will block until the connection is established or an I/O error occurs.

This method performs exactly the same security checks as the java.net.Socket class. That is, if a security manager has been installed then this method verifies that its java.lang.SecurityManager.checkConnect checkConnect method permits connecting to the address and port number of the given remote endpoint.

This method may be invoked at any time. If a read or write operation upon this channel is invoked while an invocation of this method is in progress then that operation will first block until this invocation is complete. If a connection attempt is initiated but fails, that is, if an invocation of this method throws a checked exception, then the channel will be closed.


Parameters:
  remote - The remote address to which this channel is to be connected true if a connection was established,false if this channel is in non-blocking modeand the connection operation is in progress
throws:
  AlreadyConnectedException - If this channel is already connected
throws:
  ConnectionPendingException - If a non-blocking connection operation is already in progresson this channel
throws:
  ClosedChannelException - If this channel is closed
throws:
  AsynchronousCloseException - If another thread closes this channelwhile the connect operation is in progress
throws:
  ClosedByInterruptException - If another thread interrupts the current threadwhile the connect operation is in progress, therebyclosing the channel and setting the current thread'sinterrupt status
throws:
  UnresolvedAddressException - If the given remote address is not fully resolved
throws:
  UnsupportedAddressTypeException - If the type of the given remote address is not supported
throws:
  SecurityException - If a security manager has been installedand it does not permit access to the given remote endpoint
throws:
  IOException - If some other I/O error occurs



finishConnect
abstract public boolean finishConnect() throws IOException(Code)
Finishes the process of connecting a socket channel.

A non-blocking connection operation is initiated by placing a socket channel in non-blocking mode and then invoking its SocketChannel.connectconnect method. Once the connection is established, or the attempt has failed, the socket channel will become connectable and this method may be invoked to complete the connection sequence. If the connection operation failed then invoking this method will cause an appropriate java.io.IOException to be thrown.

If this channel is already connected then this method will not block and will immediately return true. If this channel is in non-blocking mode then this method will return false if the connection process is not yet complete. If this channel is in blocking mode then this method will block until the connection either completes or fails, and will always either return true or throw a checked exception describing the failure.

This method may be invoked at any time. If a read or write operation upon this channel is invoked while an invocation of this method is in progress then that operation will first block until this invocation is complete. If a connection attempt fails, that is, if an invocation of this method throws a checked exception, then the channel will be closed.

true if, and only if, this channel's socket is nowconnected
throws:
  NoConnectionPendingException - If this channel is not connected and a connection operationhas not been initiated
throws:
  ClosedChannelException - If this channel is closed
throws:
  AsynchronousCloseException - If another thread closes this channelwhile the connect operation is in progress
throws:
  ClosedByInterruptException - If another thread interrupts the current threadwhile the connect operation is in progress, therebyclosing the channel and setting the current thread'sinterrupt status
throws:
  IOException - If some other I/O error occurs



isConnected
abstract public boolean isConnected()(Code)
Tells whether or not this channel's network socket is connected.

true if, and only if, this channel's network socketis connected



isConnectionPending
abstract public boolean isConnectionPending()(Code)
Tells whether or not a connection operation is in progress on this channel.

true if, and only if, a connection operation has beeninitiated on this channel but not yet completed by invoking theSocketChannel.finishConnect finishConnect method



open
public static SocketChannel open() throws IOException(Code)
Opens a socket channel.

The new channel is created by invoking the java.nio.channels.spi.SelectorProvider.openSocketChannelopenSocketChannel method of the system-wide default java.nio.channels.spi.SelectorProvider object.

A new socket channel
throws:
  IOException - If an I/O error occurs



open
public static SocketChannel open(SocketAddress remote) throws IOException(Code)
Opens a socket channel and connects it to a remote address.

This convenience method works as if by invoking the SocketChannel.open() method, invoking the SocketChannel.connect(SocketAddress) connect method upon the resulting socket channel, passing it remote, and then returning that channel.


Parameters:
  remote - The remote address to which the new channel is to be connected
throws:
  AsynchronousCloseException - If another thread closes this channelwhile the connect operation is in progress
throws:
  ClosedByInterruptException - If another thread interrupts the current threadwhile the connect operation is in progress, therebyclosing the channel and setting the current thread'sinterrupt status
throws:
  UnresolvedAddressException - If the given remote address is not fully resolved
throws:
  UnsupportedAddressTypeException - If the type of the given remote address is not supported
throws:
  SecurityException - If a security manager has been installedand it does not permit access to the given remote endpoint
throws:
  IOException - If some other I/O error occurs



read
abstract public int read(ByteBuffer dst) throws IOException(Code)

throws:
  NotYetConnectedException - If this channel is not yet connected



read
abstract public long read(ByteBuffer[] dsts, int offset, int length) throws IOException(Code)

throws:
  NotYetConnectedException - If this channel is not yet connected



read
final public long read(ByteBuffer[] dsts) throws IOException(Code)

throws:
  NotYetConnectedException - If this channel is not yet connected



socket
abstract public Socket socket()(Code)
Retrieves a socket associated with this channel.

The returned object will not declare any public methods that are not declared in the java.net.Socket class.

A socket associated with this channel



validOps
final public int validOps()(Code)
Returns an operation set identifying this channel's supported operations.

Socket channels support connecting, reading, and writing, so this method returns ( SelectionKey.OP_CONNECT |  SelectionKey.OP_READ |  SelectionKey.OP_WRITE ).

The valid-operation set



write
abstract public int write(ByteBuffer src) throws IOException(Code)

throws:
  NotYetConnectedException - If this channel is not yet connected



write
abstract public long write(ByteBuffer[] srcs, int offset, int length) throws IOException(Code)

throws:
  NotYetConnectedException - If this channel is not yet connected



write
final public long write(ByteBuffer[] srcs) throws IOException(Code)

throws:
  NotYetConnectedException - If this channel is not yet connected



Methods inherited from java.nio.channels.spi.AbstractSelectableChannel
final public Object blockingLock()(Code)(Java Doc)
final public SelectableChannel configureBlocking(boolean block) throws IOException(Code)(Java Doc)
final protected void implCloseChannel() throws IOException(Code)(Java Doc)
abstract protected void implCloseSelectableChannel() throws IOException(Code)(Java Doc)
abstract protected void implConfigureBlocking(boolean block) throws IOException(Code)(Java Doc)
final public boolean isBlocking()(Code)(Java Doc)
final public boolean isRegistered()(Code)(Java Doc)
final public SelectionKey keyFor(Selector sel)(Code)(Java Doc)
final public SelectorProvider provider()(Code)(Java Doc)
final public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException(Code)(Java Doc)

Methods inherited from java.nio.channels.SelectableChannel
abstract public Object blockingLock()(Code)(Java Doc)
abstract public SelectableChannel configureBlocking(boolean block) throws IOException(Code)(Java Doc)
abstract public boolean isBlocking()(Code)(Java Doc)
abstract public boolean isRegistered()(Code)(Java Doc)
abstract public SelectionKey keyFor(Selector sel)(Code)(Java Doc)
abstract public SelectorProvider provider()(Code)(Java Doc)
abstract public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException(Code)(Java Doc)
final public SelectionKey register(Selector sel, int ops) throws ClosedChannelException(Code)(Java Doc)
abstract public int validOps()(Code)(Java Doc)

Methods inherited from java.nio.channels.spi.AbstractInterruptibleChannel
final protected void begin()(Code)(Java Doc)
final public void close() throws IOException(Code)(Java Doc)
final protected void end(boolean completed) throws AsynchronousCloseException(Code)(Java Doc)
abstract protected void implCloseChannel() throws IOException(Code)(Java Doc)
final public boolean isOpen()(Code)(Java Doc)

Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.