The channel of Java Network programming-nio

Source: Internet
Author: User

Java NiO channels are similar to streams, but are somewhat different:

    • The data can be read from the channel, and the data can be written to the channel. But stream reads and writes are usually one-way.
    • Channels can be read and written asynchronously.
    • The data in the channel is always read to a buffer first, or it is always written from a buffer.
The implementation of channel

These are the most important implementations of the channel in Java NIO:

    • FileChannel: Read and write data from a file (non-asynchronous read/write)
    • Datagramchannel: Can read and write data on the network through UDP
    • Socketchannel: Can read and write data on the network through TCP
    • Serversocketchannel: You can listen for incoming TCP connections, like Web servers. A socketchannel is created for each new incoming connection

It's only focused on Socketchannel and Serversocketchannel.

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:
Open a socketchannel and connect to a server on the Internet.
When a new connection arrives at Serversocketchannel, a socketchannel is created.

Open Socketchannel

Socketchannel Socketchannel = Socketchannel.open ();

socketChannel.connect(newInetSocketAddress("localhost"8001));

Close Socketchannel

Socketchannel.close ();

Reading data from Socketchannel

ByteBuffer buf = ByteBuffer.allocate(48);

intbytesRead = 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).

Write Socketchannel

String NewData = "New String to write to file ..." + System.currenttimemillis ();
Bytebuffer buf = Bytebuffer.allocate (48);
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 channel of Java Network programming-nio

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.