Java NIO Socketchannel

Source: Internet
Author: User

A Java NIO Socketchannel is a channel, a connected to a TCP network socket. It is Java NIO ' s equivalent of Java Networking ' s Sockets. There is ways a socketchannel can be created:

    1. You open a socketchannel and connect to a server somewhere on the Internet.
    2. A socketchannel can created when an incoming connection arrives at a ServerSocketChannel .

Opening a Socketchannel

Here's how you open a Socketchannel:

Socketchannel Socketchannel = socketchannel.open (); Socketchannel.connect (new inetsocketaddress (" Http://jenkov.com ", 80));

Closing a Socketchannel

You close a socketchannel after use by calling the Socketchannel.close () method. Here's how it's done:

Socketchannel.close ();    

Reading from a Socketchannel

To read data from a socketchannel one of the Read () methods. Here are an example:

Bytebuffer buf = bytebuffer.allocate (); int bytesread = Socketchannel.read (BUF);

First a Buffer is allocated. The data read from the Socketchannel are read into the Buffer.

Second the Socketchannel.read () method is called. This method reads data from the Socketchannel into the Buffer. The int returned by the read () method tells how many bytes were witten into the Buffer. If-1 is returned, the End-of-stream was reached (the connection is closed).

Writing to a Socketchannel

Writing data to a socketchannel are done using the Socketchannel.write () method, which takes a Buffer as parameter. Here are an example:

String NewData = "New string to write to file ..." += bytebuffer.allocate ($); Buf.clear (); Buf.put ( Newdata.getbytes ()); Buf.flip ();  while (Buf.hasremaining ()) {    channel.write (BUF);}

Notice how the Socketchannel write () method is called inside a while-loop. There is no guarantee of what many bytes the write () method writes to the Socketchannel. Therefore We repeat the write () call until the Buffer have no further bytes to write.

Non-blocking Mode

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

Connect ()

If the Socketchannel is in non-blocking mode, and your call to connect (), the method may return before a connection is Establi Shed. To determine whether the connection was established, you can call the Finishconnect () method, like this:

Socketchannel.configureblocking (false); Socketchannel.connect (new inetsocketaddress (" Http://jenkov.com ", ());  while (! Socketchannel.finishconnect ()) {    //wait, or do    something else ...}

Write ()

In non-blocking mode, the Write () method may return without has written anything. Therefore the Write () method in a loop of need to call. But, since the already being did in the previous write examples, no need to do anything differently here.

Read ()

In non-blocking mode, the Read () method may return without has read any data on all. Therefore need to pay attention to the returned int., which tells how many bytes were read.

Non-blocking Mode with selectors

The non-blocking mode of Socketchannel ' s works much better with Selector ' s. By registering one or more Socketchannel ' s with a Selector, you can ask the Selector for channels Ng, writing etc. How to use Selector's with Socketchannel's is the explained in more detail in a later text in this tutorial.

Java NIO Socketchannel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.