关闭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("关闭时出现异常";}

}

}

}

ZJI:台湾CN2/香港高主频服务器7折每月595元起,其他全场8折

ZJI原名维翔主机,是原来Wordpress圈知名主机商家,成立于2011年,2018年9月更名为ZJI,提供香港、日本、美国独立服务器(自营/数据中心直营)租用及VDS、虚拟主机空间、域名注册业务。ZJI今年全新上架了台湾CN2线路服务器,本月针对香港高主频服务器和台湾CN2服务器提供7折优惠码,其他机房及产品提供8折优惠码,优惠后台湾CN2线路E5服务器月付595元起。台湾一型CPU:Inte...

美国200G美国高防服务器16G,800元

美国高防服务器提速啦专业提供美国高防服务器,美国高防服务器租用,美国抗攻击服务器,高防御美国服务器租用等。我们的海外高防服务器带给您坚不可摧的DDoS防护,保障您的业务不受攻击影响。HostEase美国高防服务器位于加州和洛杉矶数据中心,均为国内访问速度最快最稳定的美国抗攻击机房,带给您快速的访问体验。我们的高防服务器配有最高层级的DDoS防护系统,每款抗攻击服务器均拥有免费DDoS防护额度,让您...

易探云330元/年,成都4核8G/200G硬盘/15M带宽,仅1888元/3年起

易探云服务器怎么样?易探云是国内一家云计算服务商家,致力香港云服务器、美国云服务器、国内外服务器租用及托管等互联网业务,目前主要地区为运作香港BGP、香港CN2、广东、北京、深圳等地区。目前,易探云推出的国内云服务器优惠活动,国内云服务器2核2G5M云服务器低至330元/年起;成都4核8G/200G硬盘/15M带宽,仅1888元/3年起!易探云便宜vps服务器配置推荐:易探云vps云主机,入门型云...

java主机为你推荐
美国虚拟主机美国虚拟主机用着怎么样?cm域名注册.Cm是什么域名 网址尾部是.CM的是哪里的网址?哪可以注册?海外主机那些韩国主机,美国主机是怎么来的?asp虚拟空间ASP空间是什么意思?网站空间购买哪里买网站空间好?大连虚拟主机找个大连企业建站公司,大家给推荐一下吧。天津虚拟主机天津有代理店掌柜的公司吗?在哪?河南虚拟主机新网的虚拟主机怎么样?www二级域名www的域名是一级域名还是二级域名域名解析什么是域名解析,为什么要域名解析
欧洲免费vps 免费二级域名申请 国内免备案主机 5折 英文简历模板word win8升级win10正式版 typecho 有益网络 web服务器架设 phpmyadmin配置 1g空间 太原网通测速平台 中国网通测速 鲁诺 双12 免费的域名 lamp的音标 电信宽带测速软件 江苏双线 googlevoice 更多