`
seara
  • 浏览: 620507 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java网络编程从入门到精通(32):一个非阻塞I/O的例子

阅读更多
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]-->

本文为原创,如需转载,请注明作者和出处,谢谢!

上一篇:Java网络编程从入门到精通(31):非阻塞I/O简介

为了使读者更好地理解非阻塞I/O,本节给出了一个简单的例子用来演示如何将非阻塞I/O应用到网络程序中。读者可以先不必管这个例子的具体细节。因为这个例子的主要目的并不是讲解非阻塞I/O的使用,而是先让读者对非阻塞I/O有一个笼统的感性认识。在看完这个例子后,读者可能会有很多疑问,在本章后面的部分将会逐渐揭开这些迷团。这个例子的主要功能是访问新浪网,并将新浪网的首页在控制台上输出。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->package test;

importjava.net.*;
importjava.nio.*;
importjava.nio.channels.*;
importjava.io.*;

publicclassFirstNonBlockingIO
{
publicstaticvoidmain(String[]args)throwsException
{
SocketAddressremote
=newInetSocketAddress("www.sina.com.cn",80);
SocketChannelchannel
=SocketChannel.open(remote);
Stringrequest
="GET/HTTP/1.1\r\n"+
"Host:www.sina.com.cn\r\n"+
"Connection:close\r\n\r\n";
ByteBufferheader
=ByteBuffer.wrap(request.getBytes());
channel.write(header);
ByteBufferbuffer
=ByteBuffer.allocate(1024);
WritableByteChannelout
=Channels.newChannel(System.out);
while(channel.read(buffer)!=-1)
{
buffer.flip();
out.write(buffer);
buffer.clear();
}
channel.close();
}
}

测试

执行如下命令:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->javatest.FirstNonBlockingIO>sina.txt


打开sina.txt后,会看到如下的文件内容:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->HTTP/1.0200OK
Date:Sun
,01Apr200706:53:50GMT
Server:Apache/
2.0.58(Unix)
Last-Modified:Sun
,01Apr200706:50:47GMT
Connection:close


</body>
</html>


由于新浪网的主页内容太多,因此,为了方便查看程序运行结果,使用输出重定向符“>”将本该输出到控制台的内容输出到sina.txt文件中。从例程7-1可以看出,主要有三点和同步I/O存在差异。

1. 连接服务器(第013行)。使用SocketChannel类,而不是Socket类。
2. 向服务端写数据(第018行)。 使用SocketChannel类中的write方法,而不是OutputStream。
3. 从服务端读数据(第021行)。使用SocketChannel类中的read方法,而不是InputStream。

除了上面的三点外,在本例中还使用了缓冲区来处理输入输出数据。因此,通道(Channels)和缓冲区(Buffers)是学习非阻塞I/O之前必须掌握的知识。在下面的文章等将详细讲解这两部分的内容。

下一篇:Java网络编程从入门到精通(33):非阻塞I/O的缓冲区(Buffer)



国内最棒的Google Android技术社区(eoeandroid),欢迎访问!

《银河系列原创教程》发布

《Java Web开发速学宝典》出版,欢迎定购

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics