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

}

}

}

LOCVPS:VPS主机全场8折,德国/荷兰/美国KVM终身7折

LOCVPS发来了针对元旦新年的促销活动,除了全场VPS主机8折优惠外,针对德国/荷兰KVM #1/美国KVM#2 VPS提供终身7折优惠码(限量50名,先到先得)。LOCVPS是一家成立于2012年的国人VPS服务商,提供中国香港、韩国、美国、日本、新加坡、德国、荷兰、俄罗斯等地区VPS服务器,基于KVM或XEN架构(推荐优先选择KVM),均选择直连或者优化线路,国内延迟低,适合建站或远程办公使...

趣米云月付460元,香港CN2云服务器VPS月付低至18元

趣米云早期为做技术起家,为3家IDC提供技术服务2年多,目前商家在售的服务有香港vps、香港独立服务器、香港站群服务器等,线路方面都是目前最优质的CN2,直连大陆,延时非常低,适合做站,目前商家正在做七月优惠活动,VPS低至18元,价格算是比较便宜的了。趣米云vps优惠套餐:KVM虚拟架构,香港沙田机房,线路采用三网(电信,联通,移动)回程电信cn2、cn2 gia优质网络,延迟低,速度快。自行封...

UCloud 618活动:香港云服务器月付13元起;最高可购3年,AMD/Intel系列

ucloud6.18推出全球大促活动,针对新老用户(个人/企业)提供云服务器促销产品,其中最低配快杰云服务器月付5元起,中国香港快杰型云服务器月付13元起,最高可购3年,有AMD/Intel系列。当然这都是针对新用户的优惠。注意,UCloud全球有31个数据中心,29条专线,覆盖五大洲,基本上你想要的都能找到。注意:以上ucloud 618优惠都是新用户专享,老用户就随便看看!点击进入:uclou...

java主机为你推荐
虚拟主机价格个人虚拟主机选择多大的价格多少的合适?linux虚拟主机linux下最好的虚拟机是什么呢 VMware 还是virtualbox网站服务器租用网站服务器租用网站空间域名网站、域名空间三者的关系重庆虚拟空间在重庆开一家VR体验馆价格要多少?asp网站空间ASP空间是什么?独立ip虚拟主机独立ip的虚拟主机和vps的区别和优势??虚拟主机服务商请问哪个服务商的虚拟主机比较好呀windows虚拟主机windows10用什么虚拟机深圳虚拟主机深圳鼎峰网络科技 虚拟主机空间怎么样
北京网站空间 美国域名 二级域名申请 godaddy主机 精品网 丹弗 e蜗牛 softbank邮箱 183是联通还是移动 hostloc 世界测速 中国电信测速网 umax120 支持外链的相册 四川电信商城 监控服务器 论坛主机 php服务器 中国联通宽带测试 网站防护 更多