网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  一个socket服务的例子! 请高手指点无法捕捉的输出如何修改?     
  文章作者:未知  文章来源:水木森林  
  查看:118次  录入:管理员--2007-11-17  
 
  一个socket服务的例子!
请高手指点无法捕捉的输出如何修改?
//服务端
package org.dyol.net;

import java.net.*;
import java.io.*;
import java.util.*;

public class console {

public static void main(string args[]){
if (args.length!=1)
throw new illegalargumentexception("unlawful number of args:user: console ");
new console(integer.parseint(args[0]));
}

public console(int port){
this.port = port;
try {
serversocket socket = new serversocket(port);
system.out.println("start service on "+port);
while(true){
new transmit(socket.accept()).start();
}
}catch (ioexception e){}
}

private class transmit extends thread {
private string sys = system.getproperty("os.name")+" "+system.getproperty("os.arch")+" "+system.getproperty("os.version");
private socket sock;
public transmit(socket sock){
this.sock = sock;
try {
input = new bufferedreader(new inputstreamreader(sock.getinputstream(),"gb2312"));
output = new printstream(sock.getoutputstream());
output.println(sys);
}catch(exception e){}
}
public void run(){
system.out.println("while(true){
try {
synchronized(output){
cmd = input.readline();
if( cmd == null ) break;
system.out.println("start command");
system.out.println("[ "+new date().tostring()+" receive command from : "+sock.getinetaddress()+" ]: "+cmd);
proc = runtime.getruntime().exec(cmd);
dis = new bufferedreader(new inputstreamreader(proc.getinputstream(),"gb2312"));
}
synchronized(output){
while((msg = dis.readline())!=null){
if(msg.equals("exit".tolowercase()))break;
output.println(msg);
}
}
system.out.println("end command");
}catch (ioexception e){
break;
}
}
system.out.println("end thread ---------------------->");
}
}

private int port = 520;
private bufferedreader input;
private printstream output;
private string cmd;
private process proc;
private string msg;
private bufferedreader dis ;

}

//客户端
import java.io.*;
import java.net.*;

public class client {
public static void main(string[] args){
if (args.length != 2)
throw new illegalargumentexception("wrong number of args");
string host = args[0];
int port = integer.parseint(args[1]);
new client(host, port);
}
public client(string host,int port){
try {
socket s = new socket(host, port);
final bufferedreader echo=new bufferedreader(new inputstreamreader(s.getinputstream(),"gb2312"));
printwriter ps = new printwriter(s.getoutputstream());
bufferedreader cmd = new bufferedreader(new inputstreamreader(system.in,"gb2312"));
final printwriter pis = new printwriter(system.out, true);
pis.println("connected to "+s.getinetaddress()+":" + s.getport());
thread t = new thread() {
public void run() {
char[] buffer = new char[1024];
int ch;
try {
while((ch = echo.read(buffer)) != -1) {
for(int i = 0; i < ch; i++) {
if (buffer[i] == '') pis.println();
else pis.print(buffer[i]);
}
pis.flush();
}
}catch (ioexception e){
pis.println(e);
}
pis.println("connection closed by server.");
system.exit(0);
}
};
t.setpriority(thread.currentthread().getpriority() + 1);
t.start();
string line;
while((line = cmd.readline()) != null) {
ps.print(line + "");
ps.flush();
}
s.close();
pis.println("connection closed by client.");
system.exit(0);
}catch (exception e) {}
}
}
 
 
上一篇: 一个jdbc-odbc桥的字符集参数的例子    下一篇: 一个socket服务器样板程
  相关文档
scjp:“菜鸟”也能玩java 11-17
ant学习指南 11-17
java的异常处理机制 11-16
tortoisesvn从1.2.6升级到1.3.2过程中遇到的问题 11-17
eclipse netbeans intellij的实用对比 11-16
blink 方法 11-16
java阵营效仿微软研发开发工具吸引开发者 11-17
setseconds 方法 11-16
用jface和swt构建简单的应用程序(2) 11-17
爪哇语言工厂方法创立性模式介绍(上) 11-17
jdk1.4的正则表达式使用介绍 11-17
细细讲述java技术开发的规则之开发篇 11-17
ibm发布的europa推21个eclipse开源项目 11-16
整数类型 11-17
如何轻松提高java代码的性能 11-17
java入门笔记3_datastructure 11-17
用javap反编译帮你理解java特性 11-16
关于移动百宝箱应用打包问题的解决办法 11-17
eclipse技巧 --?徭i篇 11-17
配置bea weblogic 8.1 jdbc连接 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息