天天看點

Java NIO SocketChannelJava NIO SocketChannel

Java Nio 

1

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/index.html">Java NIO Tutorial</a>

2

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/overview.html">Java NIO Overview</a>

3

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/channels.html">Java NIO Channel</a>

4

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/buffers.html">Java NIO Buffer</a>

5

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/scatter-gather.html">Java NIO Scatter / Gather</a>

6

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/channel-to-channel-transfers.html">Java NIO Channel to Channel Transfers</a>

7

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/selectors.html">Java NIO Selector</a>

8

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/file-channel.html">Java NIO FileChannel</a>

9

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html">Java NIO SocketChannel</a>

10

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/server-socket-channel.html">Java NIO ServerSocketChannel</a>

11

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/datagram-channel.html">Java NIO DatagramChannel</a>

12

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/pipe.html">Java NIO Pipe</a>

13

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/nio-vs-io.html">Java NIO vs. IO</a>

Java NIO SocketChannelJava NIO SocketChannel

Rate article:

Share article:

<a target="_blank" href="https://twitter.com/share">Tweet</a>

Table of Contents

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html#opening-a-socketchannel">Opening a SocketChannel</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html#closing-a-socketchannel">Closing a SocketChannel</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html#reading-from-a-socketchannel">Reading from a SocketChannel</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html#writing-to-a-socketchannel">Writing to a SocketChannel</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html#non-blocking-mode">Non-blocking Mode</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html#connect">connect()</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html#write">write()</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html#read">read()</a>

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/socketchannel.html#non-blocking-mode-with-selectors">Non-blocking Mode with Selectors</a>

You open a <code>SocketChannel</code> and connect to a server somewhere on the internet.

<a target="_blank"></a>

Here is how you open a <code>SocketChannel</code>:

You close a <code>SocketChannel</code> after use by calling the <code>SocketChannel.close()</code> method. Here is how that is done:

To read data from a <code>SocketChannel</code> you call one of the <code>read()</code> methods. Here is an example:

First a <code>Buffer</code> is allocated. The data read from the <code>SocketChannel</code> is read into the <code>Buffer</code>.

Second the <code>SocketChannel.read()</code> method is called. This method reads data from the <code>SocketChannel</code> into the<code>Buffer</code>. The <code>int</code> returned by the <code>read()</code> method tells how many bytes were witten into the <code>Buffer</code>. If -1 is returned, the end-of-stream is reached (the connection is closed).

Writing data to a <code>SocketChannel</code> is done using the <code>SocketChannel.write()</code> method, which takes a <code>Buffer</code> as parameter. Here is an example:

Notice how the <code>SocketChannel.write()</code> method is called inside a while-loop. There is no guarantee of how many bytes the <code>write()</code> method writes to the <code>SocketChannel</code>. Therefore we repeat the <code>write()</code> call until the <code>Buffer</code>has no further bytes to write.

You can set a <code>SocketChannel</code> into non-blocking mode. When you do so, you can call <code>connect()</code>, <code>read()</code> and<code>write()</code> in asynchronous mode.

If the <code>SocketChannel</code> is in non-blocking mode, and you call <code>connect()</code>, the method may return before a connection is established. To determine whether the connection is established, you can call the <code>finishConnect()</code> method, like this:

In non-blocking mode the <code>write()</code> method may return without having written anything. Therefore you need to call the<code>write()</code> method in a loop. But, since this is already being done in the previous write examples, no need to do anything differently here.

In non-blocking mode the <code>read()</code> method may return without having read any data at all. Therefore you need to pay attention to the returned <code>int</code>, which tells how many bytes were read.

The non-blocking mode of <code>SocketChannel</code>'s works much better with <code>Selector</code>'s. By registering one or more<code>SocketChannel</code>'s with a <code>Selector</code>, you can ask the <code>Selector</code> for channels that are ready for reading, writing etc. How to use <code>Selector</code>'s with <code>SocketChannel</code>'s is explained in more detail in a later text in this tutorial.

<a target="_blank" href="http://tutorials.jenkov.com/java-nio/server-socket-channel.html">Next:   Java NIO ServerSocketChannel</a>