JDK1.4新特性之I/O APIs篇(三)

日期: 2007-12-26 作者:BitsCN整理 来源:TechTarget中国

  首先,让我们来看看TimeServer.java(详细代码见清单一)。该程序首先检查参数是否是一个数字串,注意这里模式匹配的用法。
  
  if ((args.length == 1) && Pattern.matches("[0-9]+", args[0]))
  port = Integer.parseInt(args[0]);
  
  接着在方法setup()中,调用类ServerSocketChannel的静态方法open()建立一个server-socket channel,此时它还并没有和具体的主机和端口绑定起来,此时我们需要利用到相关联的server socket的bind()方法,server socket可以用类ServerSocketChannel的socket()方法得到。

  ServerSocketChannel ssc = ServerSocketChannel.open();
  InetSocketAddress isa
   = new InetSocketAddress(InetAddress.getLocalHost(), port);
  ssc.socket().bind(isa);
  
  最后,监听服务请求的任务在方法serve()中实现。首先,调用类ServerSocketChannel的方法accept()接受连接并返回一个SocketChannel对象,接着调用该对象的write()方法向channel中写入数据。注意在数据写入之前对它的处理过程。
  
  SocketChannel sc = ssc.accept();
   String now = new Date().toString();
   sc.write(encoder.encode(CharBuffer.wrap(now + "rn")));
  /********************清单一:TimeServer.java完整的程序清单********************/
  /*
   * @(#)TimeServer.java 1.3 01/12/13
   * Listen for connections and tell callers what time it is.
   * Demonstrates NIO socket channels (accepting and writing),
   * buffer handling, charsets, and regular expressions.
   *
   * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
   */

  import java.io.*;
  import java.net.*;
  import java.nio.*;
  import java.nio.channels.*;
  import java.nio.charset.*;
  import java.util.*;
  import java.util.regex.*;
  public class TimeServer
  {
   // We can’t use the normal daytime port (unless we’re running as root,
   // which is unlikely), so we use this one instead
   private static int PORT = 8013;
   // The port we’ll actually use
   private static int port = PORT;
   // Charset and encoder for US-ASCII
   private static Charset charset = Charset.forName("US-ASCII");
   private static CharsetEncoder encoder = charset.newEncoder();
   // Direct byte buffer for writing
   private static ByteBuffer dbuf = ByteBuffer.allocateDirect(1024);
   // Open and bind the server-socket channel
   //
   private static ServerSocketChannel setup() throws IOException
   {
   ServerSocketChannel ssc = ServerSocketChannel.open(); bitsCN.Com
   InetSocketAddress isa
   = new InetSocketAddress(InetAddress.getLocalHost(), port);
   ssc.socket().bind(isa);
   return ssc;
   }
   // Service the next request to come in on the given channel
   //
   private static void serve(ServerSocketChannel ssc) throws IOException
   {
   SocketChannel sc = ssc.accept();
   try
   {
   String now = new Date().toString();
   sc.write(encoder.encode(CharBuffer.wrap(now + "rn")));
   System.out.println(sc.socket().getInetAddress() + " : " + now);
   sc.close();
   }
   finally
   {
   // Make sure we close the channel (and hence the socket)
   sc.close();
   }
   }
  
   public static void main(String[] args) throws IOException
   {
   if (args.length > 1)
   {
   System.err.println("Usage: java TimeServer [port]");
   return;
   } www_bitscn_com
  
   // If the first argument is a string of digits then we take that
   // to be the port number
   if ((args.length == 1) && Pattern.matches("[0-9]+", args[0]))
   port = Integer.parseInt(args[0]);
  ServerSocketCh

我们一直都在努力坚持原创.......请不要一声不吭,就悄悄拿走。

我原创,你原创,我们的内容世界才会更加精彩!

【所有原创内容版权均属TechTarget,欢迎大家转发分享。但未经授权,严禁任何媒体(平面媒体、网络媒体、自媒体等)以及微信公众号复制、转载、摘编或以其他方式进行使用。】

微信公众号

TechTarget微信公众号二维码

TechTarget

官方微博

TechTarget中国官方微博二维码

TechTarget中国

相关推荐

  • 企业轻松集成SaaS CRM和ERP应用(二)

    “这些项目很多都没有取得成功,因为人们把太多时间的浪费在了连接两个系统所用的技术上,而没有重视他们所要集成的业务流程,” Kelman解释说。“如果员工没有看到……

  • 基于Google.com的Apis的客户端程序

    这是一个非常小的Google客户端程序,原作者是Smile Seo,我只是转译并在适当的地方加上了注释。

  • “VC++ 和Windows APIs”之通用APIs的分类(五)

    许多国外的VC++书籍中都曾经这样说过,“真正的程序员一定会用VC++”,但是用VC++的程序员不一定是真正的程序员!在当今的商业社会中,程序开发的速度和效率永远是竞争胜利的一个要点,许多用VC++的人都说用API开发速度太慢而不去了解和深入。

  • “VC++ 和Windows APIs”之通用APIs的分类(四)

    许多国外的VC++书籍中都曾经这样说过,“真正的程序员一定会用VC++”,但是用VC++的程序员不一定是真正的程序员!在当今的商业社会中,程序开发的速度和效率永远是竞争胜利的一个要点,许多用VC++的人都说用API开发速度太慢而不去了解和深入。