Java Network Programming from entry to mastery (32): An example of non-blocking I/O

Source: Internet
Author: User

To better understand non-blocking I/O, this section provides a simple example to demonstrate how to apply non-blocking I/O to a network program. You do not have to worry about the details of this example. The main purpose of this example is not to explain the use of non-blocking I/O, but to give readers a general understanding of non-blocking I/O. After reading this example, you may have a lot of questions. In the later sections of this chapter, we will gradually unveil these puzzles. The main function of this example is to access Sina and output the homepage of Sina on the console.


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" +
"Host: www.sina.com.cn" +
"Connection: close ";
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

Run the following command:

 

Java test. FirstNonBlockingIO> sina.txt

After you open sina.txt, you will see the following file content:

 

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>
</Html>

Because Sina has too much content on its home page, In order to conveniently view the program running result, use the "> outputs" output to output the content to the sina.txt file of the console. 7-1 can be seen from the routine, there are three main differences and synchronization I/O.

1. Connect to the server (013rd rows ). Use the SocketChannel class instead of the Socket class.
2. Write Data to the server (018th rows ). Use the write method in the SocketChannel class instead of OutputStream.
3. read data from the server (021st rows ). Use the read method in the SocketChannel class instead of InputStream.

In addition to the preceding three points, a buffer is used in this example to process input and output data. Therefore, Channels and Buffers are required before learning non-blocking I/O. The following articles describe the two parts in detail.

 

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.