`

jdk6.0从入门到精通-----chapter5网络编程 新I/O(含源码下载)

阅读更多
本例向用户通过参数指定的主机端口发送一段请求报文,然后读取和打印反馈报文。综合应用了NIO的缓冲区,通道和字符集。使用到了Tomcat服务器  参数为localhost 8080

package nio;

import java.io.IOException;
import java.net.UnknownHostException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class ReadURL 
{
  public static void main(String args[]) 
  {
    String host = args[0];
    int port = Integer.parseInt(args[1]);
    SocketChannel channel = null; //所有输入输出通过通道这个对象

    try 
    {
      // Setup
      InetSocketAddress socketAddress = new InetSocketAddress(host, port);//
      Charset charset = Charset.forName("UTF-8");
      CharsetDecoder decoder = charset.newDecoder();
      CharsetEncoder encoder = charset.newEncoder();

      // Allocate buffers
      ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
      CharBuffer charBuffer = CharBuffer.allocate(1024);

      // Connect
      channel = SocketChannel.open();
      channel.connect(socketAddress);

      // Send request
      String request = "GET / \r\n\r\n";
      channel.write(encoder.encode(CharBuffer.wrap(request)));

      // Read response
      while ((channel.read(buffer)) != -1) 
      {
      	buffer.flip();
        // Decode buffer
        decoder.decode(buffer, charBuffer, false);
        // Display
        charBuffer.flip();
        System.out.println(charBuffer);
        buffer.clear();
        charBuffer.clear();
      }
    } 
    catch (UnknownHostException e) 
    {
    	System.err.println(e);
    } 
    catch (IOException e) 
    {
      System.err.println(e);
    } 
    finally 
    {
      if (channel != null) 
      {
        try 
        {
        	channel.close();
        } 
        catch (IOException ignored) 
        {
        }
      }
    }
  }
}


运行后结果
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lan
g="en">
    <head>
    <title>Apache Tomcat</title>
    <style type="text/css">
    /*<![CDATA[*/
      body {
          color: #000000;
          background-color: #FFFFFF;
	  font-family: Arial, "Times New Roman", Times, serif;
          margin: 10px 0px;
      }

    img {
       border: none;
    }
    
    a:link, a:visited {
        color: blue
    }

    th {
        font-family: Verdana, "Times New Roman", Times, serif;
        font-size: 110%;
        font-weight: normal;
        font-style: italic;
        background: #D2A41C;
        text-align: left;
    }

    td {
        color: #000000;
	font-family: Arial, Helvetica, sans-serif;
    }
    
    td.menu {
        background: #FFDC75;
    }

    .center {
        text-align: center;
    }

    .code {
        color: #000000;
        font-family: "Courier New", Courier, monospace;
        font-size: 110%;
        margin-left: 2.5em;
    }
    
     #banner {
        margin-bottom: 12px;
     }


     p#congrats {
         margin-top: 0;
         font-weight: bold;
         text-align: center;
     }

     p#footer {
         text-align: right;
         font-size: 80%;
     }
     /*]]>*/
   </style>
</head>

<body>

<!-- Header -->
<table id="banner" width="100%">
    <tr>
      <td align="left" style="width:130px">
      <a href="http://tomcat.apache.org/">
        <img src="tomcat.gif" height="92" width="130" alt="The Mighty Tomcat - MEOW!"/>
      </a>
      </td>
      <td align="left" valign="top"><b>Apache Tomcat</b></td>
      <td align="right">
        <a href="http://www.apache.org/">
	  <img src="asf-logo-wide.gif" height="51" width="537" alt="The Apache Software Foundation"/>
	</a>
       </td>
     </tr>
</table>

<table>
    <tr>

        <!-- Table of Contents -->
        <td valign="top">
            <table width="100%" border="1" cellspacing="0" cellpadding="3">
                <tr>
		  <th>Administration</th>
                </tr>
           
     <tr>
		  <td class="menu">
		    <a href="manager/status">Status</a><br/>
                    <!--<a href="admin">Tomcat&nbsp;Administration</a><br/>-->
                    <a href="manager/html">Tomcat&nbsp;Manager</a><br/>
                    &nbsp;
                  </td>
                </tr>
            </table>

	    <br />
            <table width="100%" border="1" cellspacing="0" cellpadding="3">
                <tr>
		  <th>Documentation</th>
                </tr>
                <tr>
                  <td class="menu">
                    <a href="RELEASE-NOTES.txt">Release&nbsp;Notes</a><br/>
                    <a href="docs/changelog.html">Change&nbsp;Log</a><br/>
                    <a href="docs">Tomcat&nbsp;Documentation</a><br/>                        &nbsp;
                    &nbsp;
		    </td>
                </tr>
            </table>
	    
            <br/>
            <table width="100%" border="1" cellspacing="0" cellpadding="3">
                <tr>
  
                <th>Tomcat Online</th>
                </tr>
                <tr>
                  <td class="menu">
                    <a href="http://tomcat.apache.org/">Home&nbsp;Page</a><br/>
		    <a href="http://tomcat.apache.org/faq/">FAQ</a><br/>
                    <
a href="http://tomcat.apache.org/bugreport.html">Bug&nbsp;Database</a><br/>
                    <a href="http://issues.apache.org/bugzilla/buglist.cgi?bug_status=UNCONFIRMED&amp;bug_status=NEW&amp;bug_status=ASSIGNED&amp;bug_status=REOPENED&amp;bug_status=RESOLVED&amp;resolution=LATER&amp;resolution=REMIND&amp;resolution=---&amp;bugidtype=include&amp;product=Tomcat+6&amp;cmdtype=doit&amp;order=Importance">Open Bugs</a><br/>
                    <a href="http://mail-archives.apache.org/mod_mbox/tomcat-users/">Users&nbsp;Mailing&nbsp;List</a><br/>
                    <a href="http://mail-archives.apache.org/mod_mbox/tomcat-dev/">Developers&nbsp;Mailing&nbsp;List</a><br/>
                    <a href="irc://irc.freenode.net/#tomcat">IRC</a><br/>
		    &nbsp;
                  </td>
                </tr>
            </table>
	    
            <br/>
            <table width="100%" border="1" cellspacing="0" cellpadding="3">
                <tr>
		  <th>Miscellaneous</th>
                </tr>
        
        <tr>
                  <td class="menu">
                    <a href="examples/servlets/">Servlets Examples</a><br/>
                    <a href="examples/jsp/">JSP Examples</a><br/>
                    <a href="http://java.sun.com/products/jsp">Sun's&nbsp;Java&nbsp;Server&nbsp;Pages&nbsp;Site</a><br/>
                    <a href="http://java.sun.com/products/servlet">Sun's&nbsp;Servlet&nbsp;Site</a><br/>
    		    &nbsp;
                  </td>
                </tr>
            </table>
        </td>

        <td style="width:20px">&nbsp;</td>
	
        <!-- Body -->
        <td align="left" valign="top">
          <p id="congrats">If you're seeing this page via a web browser, it means you've setup Tomcat successfully. Congratulations!</p>
 
          <p>As you may have guessed by now, this is the default Tomcat home page. It can be found on the local filesystem at:</p>
          <p class="code">$CATALINA_HOME/webapps/ROOT/index.html</p>
	  
          <p>where "$CATALINA_HOME" is
 the root of the Tomcat installation directory. If you're seeing this page, and you don't think you should be, then you're either a user who has arrived at new installation of Tomcat, or you're an administrator who hasn't got his/her setup quite right. Providing the latter is the case, please refer to the <a href="docs">Tomcat Documentation</a> for more detailed setup and administration information than is found in the INSTALL file.</p>

            <p><b>NOTE: For security reasons, using the administration webapp
            is restricted to users with role "admin". The manager webapp
            is restricted to users with role "manager".</b>
            Users are defined in <code>$CATALINA_HOME/conf/tomcat-users.xml</code>.</p>

            <p>Included with this release are a host of sample Servlets and JSPs (with associated source code), extensive documentation, and an introductory guide to developing web applications.</p>

            <p>Tomcat mailing lists are available at the Tomcat project w
eb site:</p>

           <ul>
               <li><b><a href="mailto:users@tomcat.apache.org">users@tomcat.apache.org</a></b> for general questions related to configuring and using Tomcat</li>
               <li><b><a href="mailto:dev@tomcat.apache.org">dev@tomcat.apache.org</a></b> for developers working on Tomcat</li>
           </ul>

            <p>Thanks for using Tomcat!</p>

            <p id="footer"><img src="tomcat-power.gif" width="77" height="80" alt="Powered by Tomcat"/><br/>
	    &nbsp;

	    Copyright &copy; 1999-2007 Apache Software Foundation<br/>
            All Rights Reserved
            </p>
        </td>

    </tr>
</table>

</body>
</html>

该html对应的是Tomcat的主页

第五章所有源码见附件
分享到:
评论

相关推荐

    jdk6.0从入门到精通-----chapter17动态编程

    《JDK 6.0从入门到精通——Chapter 17 动态编程》 在Java编程领域,JDK 6.0版本引入了许多新特性,其中动态编程是提升开发效率和灵活性的重要方面。本章将深入探讨动态编程的概念、用途以及如何在JDK 6.0中利用这些...

    (源码下载)jdk6.0从入门到精通-----chapter2--输入输出,克隆对象

    标题 "(源码下载)jdk6.0从入门到精通-----chapter2--输入输出,克隆对象" 提供了我们要探讨的主题,即Java中的输入输出流(I/O Stream)和对象克隆。在这个章节中,我们将深入理解这两个关键概念。 **输入输出流...

    jdk6.0从入门到精通-----chapter8并发多线程(1)(源码下载)

    本章“jdk6.0从入门到精通-----chapter8并发多线程(1)”着重讲解了Java 6中关于多线程的基本知识和实践技巧。通过源码下载,读者可以更直观地理解多线程的实现方式。 首先,我们要理解什么是多线程。在单线程环境...

    jdk6.0从入门到精通-----chapter18与动态语言结合

    标题中的“jdk6.0从入门到精通-----chapter18与动态语言结合”指的是Java开发工具包(JDK)6.0版本中的一个章节,主要探讨了如何将Java与动态编程语言集成。在Java 6中,引入了一个重要的特性,即Java平台标准版6...

    jdk6.0从入门到精通-----chapter7线程

    《JDK 6.0线程入门到精通——Chapter 7》 在Java开发中,线程是程序执行的最小单元,它使得一个程序能够同时处理多个任务,从而提高了程序的效率和响应性。在JDK 6.0中,线程的管理和使用有了更加完善的特性,对于...

    jdk6.0从入门到精通-----chapter4--文件目录操作

    - JDK 6.0引入了NIO(New IO),提供了非阻塞I/O和通道(Channel)的概念,提高了I/O性能。 - `java.nio.file`包提供了新的文件操作API,如`Paths`、`Files`、`Path`等,这些类提供了更现代且功能强大的文件操作...

    jdk6.0从入门到精通-----chapter16反射机制-spring AOP

    在Java编程领域,JDK6.0是一个重要的版本,它为开发者提供了丰富的特性和改进。本章我们将深入探讨“反射机制”以及如何结合Spring AOP(面向切面编程)进行应用。反射是Java中的一种强大工具,允许程序在运行时检查...

    jdk 6.0 jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008(微信开发平台开发JDK)

    微信开发平台开发工具 JDK jdk 6.0 jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008

    开发工具 jdk-8u121-windows-i586

    开发工具 jdk-8u121-windows-i586开发工具 jdk-8u121-windows-i586开发工具 jdk-8u121-windows-i586开发工具 jdk-8u121-windows-i586开发工具 jdk-8u121-windows-i586开发工具 jdk-8u121-windows-i586开发工具 jdk-8...

    深入jdk6.0源码

    JDK6.0还引入了NIO.2(New I/O API),这是一个重要的更新,提供了非阻塞I/O操作,适用于高并发网络应用。此外,Scripting API允许在Java中嵌入脚本语言,如JavaScript,增加了语言的灵活性。 在JDBC(Java ...

    官方JDK6.0中文版

    - 首先,从官方或可信渠道下载JDK 6.0的安装程序。 - 运行安装程序,按照提示完成安装过程。 - 配置环境变量,如JAVA_HOME指向JDK安装目录,PATH添加bin子目录,确保系统能够找到Java命令。 - 检验安装是否成功,...

    JDK6.0+中文文档-lytim24.part1.rar

    java6.0文档chm版,分成三部分,分别为JDK6.0+中文文档-lytim24.part1.rar、JDK6.0+中文文档-lytim24.part2.rar、JDK6.0+中文文档-lytim24.part3.rar

    jdk1.8 jdk-8u5-windows-i586 32位官方正式版

    jdk1.8 jdk-8u5-windows-i586 32位官方正式版 jdk1.8 jdk-8u5-windows-i586 32位官方正式版

    jdk6.0中文文档

    3. **改进的网络功能**:引入了NIO.2,提供了异步I/O操作,增强了网络编程的能力,使开发者可以更高效地处理网络数据。 4. **XML处理增强**:更新了DOM和SAX解析器,支持XPath 2.0和XSLT 2.0,提高了XML文档处理的...

    jdk-6u39-windows-i586

    jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows-i586,jdk-6u39-windows...

    jdk-8u60-windows-i586-JDK1.8-32位

    jdk-8u60-windows-i586 jdk-8u60-windows-i586 jdk-8u60-windows-i586 jdk-8u60-windows-i586 jdk-8u60-windows-i586

    java-jdk1.8-8u361-all-jdk-win-linux

    java-jdk1.8-8u361-all-jdk-win-linux 该压缩包中包含jdk1.8-8u361下windows版本和linux版本,其包含快速安装包和对应的jdk压缩包版本,具体内容如下: jdk-8u361-linux-aarch64.rpm jdk-8u361-linux-i586.rpm jdk-8...

    JDK 6.0.zip

    这个压缩包“JDK 6.0.zip”显然是为了方便用户下载和安装这一特定版本的JDK。 JDK 6.0在2006年发布,它是Java SE(Standard Edition)的一部分,主要用于桌面应用开发。这一版本引入了许多新特性和改进,包括: 1....

    jdk6.0-04.zip

    JDK 6.0的网络编程接口进行了优化,包括对NIO(非阻塞I/O)的支持,这使得开发者能够构建高并发的网络应用,尤其适用于处理大量并发连接的情况。 五、XML处理 JDK 6.0引入了全新的StAX(Streaming API for XML)API...

Global site tag (gtag.js) - Google Analytics