Java network programming from getting started to mastering (32): an example of non-blocking I/O

Source: Internet
Author: User

To give readers a better understanding of non-blocking I/O, this section provides a simple example that demonstrates how to apply non-blocking I/O to a network program. The reader may not have to worry about the specifics of the example first. Because the main purpose of this example is not to explain the use of non-blocking I/O, but to first let the reader have a general perceptual knowledge of non-blocking I/O. After reading this example, the reader may have a lot of questions and will gradually uncover these mysteries later in the chapter. The main function of this example is to visit the Sina Network, and the Sina homepage on the console output.

package test;
Import java.net.*;
import java.nio.*;
import java.nio.channels.*;
Import java.io .*;         
public class Firstnonblockingio
{
public static void Main (string[] args) throws Exception
{
socketaddress remote = new Inetsocketaddress ("www.sina.com.cn", 80);
Socketchannel channel = Socketchannel.open (remote);                          
String request = "get/http/1.1\r\n" +
"host:www.sina.com.cn\r\n" +
"Connection:close\r\n\r\n";
Bytebuffer Header = Bytebuffer.wrap (Request.getbytes ());        
Channel.write (header);
Bytebuffer buffer = bytebuffer.allocate (1024);
Writablebytechannel out = Channels.newchannel (System.out);             
while (channel.read (buffer)!=-1)
{
Buffer.flip ()
Out.write (buffer);
Buffer.clear ();
}
ChaNnel.close ();
}
}

Test

Execute the following command:

Java test. Firstnonblockingio > Sina.txt

When you open Sina.txt, you will see the following file contents:

HTTP/1.0 200 OK
Date: Sun, 01 Apr 2007 06:53:50 GMT
Server: Apache/2.0.58 (Unix)
Last-Modified: Sun, 01 Apr 2007 06:50:47 GMT
Connection: close
</body>

Because of the content of Sina's homepage is too many, so, in order to be easy to view program run result, use output redirection character ">" to output the content that should output to the console to Sina.txt file. As you can see from routine 7-1, there are mainly three points and synchronous I/o differences.

1. Connect to the server (line No. 013). Use the Socketchannel class instead of the socket class.

2. Write data to the server (line No. 018). Use the Write method in the Socketchannel class instead of the OutputStream.

3. Read the data from the server (line No. 021). Use the Read method in the Socketchannel class instead of the InputStream.

In this case, in addition to the three points above, a buffer is used to process the input and output data. Therefore, channels (channels) and buffers (buffers) are knowledge that must be mastered prior to learning non-blocking I/O. The contents of these two sections will be explained in detail in the following articles.

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.