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

}

}

}

Spinservers:美国圣何塞服务器,双E5/64GB DDR4/2TB SSD/10Gbps端口月流量10TB,$111/月

spinservers怎么样?spinservers大硬盘服务器。Spinservers刚刚在美国圣何塞机房补货120台独立服务器,CPU都是双E5系列,64-512GB DDR4内存,超大SSD或NVMe存储,数量有限,机器都是预部署好的,下单即可上架,无需人工干预,有需要的朋友抓紧下单哦。Spinservers是Majestic Hosting Solutions,LLC旗下站点,主营美国独立...

inux国外美老牌PhotonVPS月$2.5 ,Linux系统首月半价

PhotonVPS 服务商我们是不是已经很久没有见过?曾经也是相当的火爆的,我们中文习惯称作为饭桶VPS主机商。翻看之前的文章,在2015年之前也有较多商家的活动分享的,这几年由于服务商太多,乃至于有一些老牌的服务商都逐渐淡忘。这不有看到PhotonVPS商家发布促销活动。PhotonVPS 商家七月份推出首月半价Linux系统VPS主机,首月低至2.5美元,有洛杉矶、达拉斯、阿什本机房,除提供普...

VoLLcloud6折限量,香港CMI云服务器三网直连-200M带宽

vollcloud LLC首次推出6折促销,本次促销福利主要感恩与回馈广大用户对于我们的信任与支持,我们将继续稳步前行,为广大用户们提供更好的产品和服务,另外,本次促销码共限制使用30个,个人不限购,用完活动结束,同时所有vps产品支持3日内无条件退款和提供免费试用。需要了解更多产品可前往官网查看!vollcloud优惠码:VoLLcloud终生6折促销码:Y5C0V7R0YW商品名称CPU内存S...

java主机为你推荐
租用虚拟主机租用虚拟主机需要注意什么?虚机怎么选择虚拟主机?域名价格域名怎么评估价钱?免费国外空间免费国外全能空间申请国内免费空间国内哪里有免费的空间?ip代理地址ip代理有什么用?免费vps服务器免费服务器有哪些域名购买域名购买的流程是什么?成都虚拟空间成都有没有能玩ps主机游戏的网咖?asp网站空间ASP空间是什么?
个人域名备案 sugarhosts 电影服务器 流媒体服务器 外国域名 http500内部服务器错误 秒杀预告 怎么测试下载速度 卡巴斯基试用版 搜索引擎提交入口 电信主机 in域名 美国凤凰城 石家庄服务器托管 七牛云存储 存储服务器 hdsky 九零网络 时间服务器 e-mail 更多