首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > Java 库的建立方法及其实例(3)
【标  题】:Java 库的建立方法及其实例(3)
【关键字】:方法,Java,实例,Java
【来  源】:网络

Java 库的建立方法及其实例(3)

Java 库的建立方法及其实例(3)

下面的例子说明了这一技术。使用者的代码使用Server.setDebugStream(),指定一个PrintStream对 象。然后,调试信息就被输出到这个流中。



// set this to a print stream if you want debug info

// sent to it; otherwise, leave it null

static private PrintStream debugStream;



// call this to send the debugging output somewhere

static public void setDebugStream( PrintStream ps ) {

debugStream = ps;

}

当使用者使用了调试的流的时候,你的库代码可以打印错误:



// send debug info to the print stream, if there is one

static public void debug( String s ) {

if (debugStream != null)

debugStream.println( s );

}


下面,来完整的看一看这个具体的例子:

EchoServer

// $Id$



import java.io.*;

import java.net.*;

import mylib.*;



public class EchoServer extends Server

{

public EchoServer( int port ) {

// The superclass knows what to do with the port number, we

// don´t have to care about it

super( port );

}



// This is called by the Server class when a connection

// comes in. "in" and "out" come from the incoming socket

// connection

public void handleConnection( Socket socket ) {

try {

InputStream in = socket.getInputStream();

OutputStream out = socket.getOutputStream();



// just copy the input to the output

while (true)

out.write( in.read() );



} catch( IOException ie ) {

System.out.println( ie );

}

}

protected void cleanUp() {

System.out.println( "Cleaning up" );

}



static public void main( String args[] ) throws Exception {

// Grab the port number from the command-line

int port = Integer.parseInt( args[0] );



// Have debugging info sent to standard error stream

Server.setDebugStream( System.err );



// Create the server, and it´s up and running

new EchoServer( port );

}

}

mylib.Server


// $Id$



package mylib;



import java.io.*;

import java.net.*;



abstract public class Server implements Runnable

{

// the port we´ll be listening on

private int port;



// how many connections we´ve handled

int numConnections;



// the Reporter that´s reporting on this Server

private Reporter reporter;



// set this to true to tell the thread to stop accepting

// connections

private boolean mustQuit = false;



public Server( int port ) {

// remember the port number so the thread can

// listen on it

this.port = port;



// the constructor starts a background thread

new Thread( this ).start();



// and start a reporter

reporter = new Reporter( this );

}



// this is our background thread

public void run() {

ServerSocket ss = null;

try {



// get ready to listen

ss = new ServerSocket( port );



while( !mustQuit ) {



// give out some debugging info

debug( "Listening on "+port );



// wait for an incoming connection

Socket s = ss.accept();



// record that we got another connection

numConnections++;



// more debugging info

debug( "Got connection on "+s );



// process the connection -- this is implemented

// by the subclass

handleConnection( s );

}

} catch( IOException ie ) {

debug( ie.toString() );

}



debug( "Shutting down "+ss );



cleanUp();

}



// the default implementation does nothing

abstract public void handleConnection( Socket s );



// tell the thread to stop accepting connections

public void close() {

mustQuit = true;

reporter.close();

}



// Put any last-minute clean-up stuff in here

protected void cleanUp() {

}



// everything below provides a simple debug system for

// this package



// set this to a print stream if you want debug info

// sent to it; otherwise, leave it null

static private PrintStream debugStream;



// we have two versions of this ...

static public void setDebugStream( PrintStream ps ) {

debugStream = ps;

}



// ... just for convenience

static public void setDebugStream( OutputStream out ) {

debugStream = new PrintStream( out );

}



// send debug info to the print stream, if there is one

static public void debug( String s ) {

if (debugStream != null)

debugStream.println( s );

}

}

mylib.Reporter


// $Id$



package mylib;



class Reporter implements Runnable

{

// the Server we are reporting on

private Server server;



// our background thread

private Thread thread;



// set this to true to tell the thread to stop accepting

// connections

private boolean mustQuit = false;



Reporter( Server server ) {

this.server = server;



// create a background thread

thread = new Thread( this );

thread.start();

}



public void run() {

while (!mustQuit) {

// do the reporting

Server.debug( "server has had "+server.numConnections+" connections" );



// then pause a while

try {

Thread.sleep( 5000 );

} catch( InterruptedException ie ) {}

}

}



// tell the background thread to quit

public void close() {

mustQuit = true;

}

} (未完待续)
Java 库的建立方法及其实例(4):【上一篇】
Java 库的建立方法及其实例(2):【下一篇】
【相关文章】
  • Java 库的建立方法及其实例(4)
  • Java 库的建立方法及其实例(5)
  • Java 库的建立方法及其实例(6)
  • Java 库的建立方法及其实例(7)
  • java的关键知识点
  • 汇编语言编程实例一
  • 实模式与保护模式切换实例
  • 中断和异常的转移方法
  • 演示中断处理的实例(实例六)
  • 演示异常处理的实例(实例七)
  • 【随机文章】
  • 联机通讯电缆的制作
  • EJB轻松进阶之二
  • 游戏编程入门(1) -- 精灵 ISprite
  • 大数运算(六)
  • Virtual PC虚拟磁盘压缩三步走
  • .NET 事件模型教程(二)
  • 如何在PHP中使用数组
  • 禁止在“显示属性”中出现“效果,web,设置”这三个分菜单
  • 《雅典娜》新人选择装备时的一点建议
  • Ajax 浅谈
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.