您的位置:首页 > 其它

解决在DHCP环境下私自指定IP和私自搭建DHCP服务器的方法

2007-08-30 17:17 696 查看
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedStream {
/*
* 目的:测试java管道流
* 设计思路: 两个内部类;继承Thread通过run()发送,接受信息
* 描述:本示例不仅阐述了java管道流,同时使用了java设计模式 Singleton(单例)
*/
//程序入口
public static void main(String[] args) {
//输出\输入管道流类对象;通过类方法单例获取
PipedOutputStream out=Sender.getOut();
PipedInputStream in=Receiver.getIn();

try{
//输入\输出管道流链接方法 connect()
in.connect(out);
}catch(IOException ioe){
ioe.printStackTrace();
}
//启动线程,用start()
new Sender().start();
new Receiver().start();
}
//通过管道流链接发送消息;使用单例必须声明静态内部类
static class Sender extends Thread{
private static PipedOutputStream out=null;
//单例返回管道对象
public static synchronized PipedOutputStream getOut(){
if(out==null){
out=new PipedOutputStream();
}
return out;
}
//发送消息
public void run() {
String mess="Hello Receiver;I'm Sender !\n";
try{
//注意:转成字节数组发送
out.write(mess.getBytes());
out.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}

}
//通过管道流链接接受消息;使用单例必须声明静态内部类
static class Receiver extends Thread{

private static PipedInputStream in=null;
//单例返回管道对象
public static synchronized PipedInputStream getIn(){
if(in==null){
in=new PipedInputStream();
}
return in;
}
//接受消息
public void run() {
//定义字节数组
byte[] byt=new byte[1024];
try{
//读取消息
int len=in.read(byt);
//生成新字符串
String mess=new String(byt, 0, len);
System.out.println("The Sender say:\n"+mess);
}catch(Exception e){
e.printStackTrace();
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐