关闭java程序,实现主机与客户机的交互.

java主机  时间:2021-01-03  阅读:()

package_ho mework 11;importjava.io.*;importjava.net.*;public class C lient {public final static int Default_Port=6666;//定义异常信息显示public final static void message(String s{

System.out.println("使用方法:Java C lient"+s;Sys te m.e xit(0;

}public static void main(String args[]{int port=Default_Port;

S ocket s=null;if((ar gs.le ngth!=1&&(ar gs.le ngth!=2message(;if(args.length==1 {port=Default_Port;

}else{try{

port=Inte ger.parseInt(args[1];

}catch(NumberFormatExcep tio n e{message(;

}

}try{

//产生一个Socket ,通过指定的端口与主机通信s=new Socket(args[0],port;

BufferedReader sin=new BufferedReader(new InputStreamReader(s.getInputStream(;

PrintStream sout=new PrintStream(s.getOutputStream(;

BufferedReader in=new BufferedReader(new InputS treamReader(System.in;

//返回连接地址

System.out.println("已连接到:"+s.getInetAddress(+":"+s.getPort(;S tring line;for(; ;{

Sys te m.o ut.pr int(">";

System.out.flush(;

//读信息

line=in.readLine(;if(line==null break;

//把信息发给服务器sout.pr intln(line;line=s in.readLine(;if(line==null{

S ys te m.o ut.pr int ln("服务器已关闭";b re ak;}

Sys te m.o ut.println(line;

}

}catch(IOException e 1{

System.err.println(e1;

}finally{try{if(s !=null s.c lose(;

}catch(IOException e2{

Sys te m.o ut.pr int ln("关闭时出现异常"; }}

}

}

package_ho mework 11;importjava.io.*;importjava.net.*;public class Server extends Thread{public final static int Default_Port=6666;protected int port;protected ServerSocket listen_socket;

//异常处理public static void fail(Exception e,String msg{System.err.println(ms g+":"+e;

Sys te m.e xit(0;

}

//监听客户端的连接请求pub lic Server(int port {if(port==0 port=Default_Port;this.port=port;try{listen_socket=new ServerSocket(port;

}catch(IOException e{

fail(e,"Exception occur in server socket"; }

System.out.println("Server: listening on port"+port; start(;

}

//接收客户端发出的连接请求并产生对象与之对应pub lic vo id run( {try{while(true{

Socket client_socket=listen_socket.accept(;

LinkedList linked=new LinkedList(client_socket;

}

}catch(IOException e{fail(e,"Exception while listening for links";

}

}public static void main(String args[]{int port=0;if(args.length==1 {try{port=Inte ger.parseInt(args[0];

}catch(NumberFormatExcep tio n e{

port=0;

System.out.println("port numb er not input";}

}new Server(port;

}

}package_ho mework 11;importjava.io.*;importjava.net.*;public class LinkedList extends Thread{protected Socket client;protected BufferedReader in;protected PrintStream out;

//启动线程public LinkedList(Socket client_socket {c lient=c lient_socket;try{in=new BufferedReader(

new InputStreamReader(client.getInputStream(;out=new PrintStream(c lie nt.ge tOutp utS tream(;

} catch(IOException e{try{c lient.c lose(;

}catch(IOException e2{

Sys te m.o ut.pr int ln("无法关闭";

}

Sys te m.e rr.println("在S ock et上建立流时出现问题:"+e;re turn;}s tar t(;

}

//服务器读取信息后反转文本并发送pub lic vo id run({

String line="";

StringBuffer revline;int len;try{for(; ;{

line=in.readLine(;if(line==nullbr eak;len=line.length(;revline=new StringBuffer(len;for(int i=len-1;i>=0;i--revline.insert(len-1-i,line.charAt(i;out.p rintln(re vline;

}

}catch(IOException e{

Sys te m.o ut.pr int ln("读取时出现异常";}finally{try{c lient.c lose(;

}catch(IOException e2{

Sys te m.o ut.pr int("关闭时出现异常";}

}

}

}

Nocser:马来西亚独立服务器促销$60.00/月

Nocser刚刚在WHT发布了几款促销服务器,Intel Xeon X3430,8GB内存,1TB HDD,30M不限流量,月付$60.00。Nocser是一家注册于马来西亚的主机商,主要经营虚拟主机、VPS和马来西亚独立服务器业务,数据中心位于马来西亚AIMS机房,线路方面,AIMS到国内电信一般,绕日本NTT;联通和移动比较友好,联通走新加坡,移动走香港,延迟都在100左右。促销马来西亚服务器...

Spinservers:美国独立服务器(圣何塞),$111/月

spinservers是Majestic Hosting Solutions,LLC旗下站点,主营美国独立服务器租用和Hybrid Dedicated等,spinservers这次提供的大硬盘、大内存服务器很多人很喜欢。TheServerStore自1994年以来,它是一家成熟的企业 IT 设备供应商,专门从事二手服务器和工作站业务,在德克萨斯州拥有40,000 平方英尺的仓库,库存中始终有数千台...

95IDC香港特价物理机服务器月付299元起,5个ip/BGP+CN2线路;美国CERA服务器仅499元/月起

95idc是一家香港公司,主要产品香港GIA线路沙田CN2线路独服,美国CERA高防服务器,日本CN2直连服务器,即日起,购买香港/日本云主机,在今年3月份,95IDC推出来一款香港物理机/香港多ip站群服务器,BGP+CN2线路终身7折,月付350元起。不过今天,推荐一个价格更美的香港物理机,5个ip,BGP+CN2线路,月付299元起,有需要的,可以关注一下。95idc优惠码:优惠码:596J...

java主机为你推荐
.net虚拟主机哪里有支持net4.0的虚拟主机美国主机空间哪个美国ASP的主机空间最稳定,最好使!!免费vps服务器请推荐一个免费的云服务器?me域名me域名怎么样?美国服务器托管美国服务器租用有哪些系列?免备案虚拟空间想买个免备案的虚拟主机,不知道哪里的好点免费网站空间免费个人网站 空间美国网站空间购买美国网站空间使用会不会麻烦呢,美国网站空间我想买个国外的网站空间,那家好,懂的用过的来说说虚拟主机管理系统大家都用的是什么虚拟主机管理系统?分享一下
主机域名 网址域名注册 电信服务器租用 站群服务器 godaddy续费优惠码 抢票工具 gg广告 申请个人网站 ftp教程 有奖调查 129邮箱 如何注册阿里云邮箱 网站在线扫描 raid10 免费asp空间 华为k3 江苏徐州移动 htaccess ncp是什么 最新优惠 更多