Java NiO Learning (eight)

Source: Internet
Author: User

Socketchannel

The Socketchannel in Java NiO is a channel that connects to a TCP network socket. Socketchannel can be created in the following 2 ways:

    1. Open a socketchannel and connect to a server on the Internet.
    2. When a new connection arrives at Serversocketchannel, a socketchannel is created.

Open Socketchannel

Here's how Socketchannel is opened:

Socketchannel Socketchannel = Socketchannel.open (); Socketchannel.connect (New Inetsocketaddress ("http://jenkov.com") , 80));

Close Socketchannel

When Socketchannel is exhausted, call Socketchannel.close () to close socketchannel:

Socketchannel.close ();
Reading data from Socketchannel

To read the data from the Socketchannel, call one of the Read () methods. Here is an example:

Bytebuffer buf = bytebuffer.allocate; int bytesread = Socketchannel.read (BUF);
First, assign a buffer. The data read from Socketchannel will be placed in this buffer. Then, call Socketchannel.read (). This method reads the data from the Socketchannel into buffer. The int value returned by the Read () method indicates how many bytes were read into the buffer. If 1 is returned, it indicates that the end of the stream has been read (the connection is closed). Writing Socketchannel writes the data to Socketchannel using the Socketchannel.write () method, which takes a buffer as a parameter. Examples are as follows:
String NewData = "New String to write to file ..." + System.currenttimemillis (); Bytebuffer buf = bytebuffer.allocate; Buf.clear (); Buf.put (Newdata.getbytes ()); Buf.flip (); while ( Buf.hasremaining ()) {    channel.write (BUF);}

Note the call to the Socketchannel.write () method is in a while loop. The Write () method does not guarantee how many bytes can be written to Socketchannel. So, we repeatedly call write () until buffer has no bytes to write.

Non-blocking mode

The Socketchannel can be set to non-blocking modes (non-blocking mode). After setting up, you can downgrade the Connect (), read (), and write () in asynchronous mode.

Connect ()

If Socketchannel is in nonblocking mode, call Connect () at this point, the method may return before the connection is established. In order to determine whether a connection is established, you can call the Finishconnect () method. Like this:

Socketchannel.configureblocking (false); Socketchannel.connect (New Inetsocketaddress ("http://jenkov.com", 80)); while (! Socketchannel.finishconnect ()) {    //wait, or do something else ...}
Write ()

In non-blocking mode, the write () method may return if nothing has been written. So you need to call write () in the loop. There are already examples, and we will not dwell on them here.

Read ()

In non-blocking mode, the read () method may return when no data has been read. So pay attention to its int return value, which tells you how many bytes were read.

Non-blocking modes and selectors

Non-blocking mode works better with selectors, and by registering one or more socketchannel with selector, you can ask the selector which channel is ready to read, write, and so on. The use of selector and Socketchannel will be detailed later.




Java NiO Learning (eight)

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.