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

}

}

}

PacificRack(年付低至19美元),夏季促销PR-M系列和多IP站群VPS主机

这几天有几个网友询问到是否有Windows VPS主机便宜的VPS主机商。原本他们是在Linode、Vultr主机商挂载DD安装Windows系统的,有的商家支持自定义WIN镜像,但是这些操作起来特别效率低下,每次安装一个Windows系统需要一两个小时,所以如果能找到比较合适的自带Windows系统的服务器那最好不过。这不看到PacificRack商家有提供夏季促销活动,其中包括年付便宜套餐的P...

JUSTG提供俄罗斯和南非CN2 GIA主机年$49.99美元JUSTGgia南非cn2南非CN2justG

JUSTG,这个主机商第二个接触到,之前是有介绍到有提供俄罗斯CN2 GIA VPS主机活动的,商家成立时间不久看信息是2020年,公司隶属于一家叫AFRICA CLOUD LIMITED的公司,提供的产品为基于KVM架构VPS主机,数据中心在非洲(南非)、俄罗斯(莫斯科),国内访问双向CN2,线路质量不错。有很多服务商实际上都是国人背景的,有的用英文、繁体搭建的冒充老外,这个服务商不清楚是不是真...

小白云 (80元/月),四川德阳 4核2G,山东枣庄 4核2G,美国VPS20元/月起三网CN2

小白云是一家国人自营的企业IDC,主营国内外VPS,致力于让每一个用户都能轻松、快速、经济地享受高端的服务,成立于2019年,拥有国内大带宽高防御的特点,专注于DDoS/CC等攻击的防护;海外线路精选纯CN2线路,以确保用户体验的首选线路,商家线上多名客服一对一解决处理用户的问题,提供7*24无人全自动化服务。商家承诺绝不超开,以用户体验为中心为用提供服务,一直坚持主打以产品质量用户体验性以及高效...

java主机为你推荐
免费vps服务器有没有便宜的vps,最好是免费的海外域名什么叫海外域名?美国网站空间我想买个国外的网站空间,那家好,懂的用过的来说说网站空间价格普通的网站空间要多少钱一年100m虚拟主机100M虚拟主机有多大,能放多少东西上海虚拟主机谁能告诉我杭州哪个公司的虚拟主机最好,机房最好是上海或浙江的.北京虚拟主机租用租用虚拟主机在哪里租用比较好山东虚拟主机山东东营制作网站的公司在哪里?域名解析域名解析是什么意思啊?域名网怎么样申请网站域名?
域名服务器 com域名注册1元 网通服务器租用 网站挂马检测工具 免费个人网站申请 什么是刀片服务器 seednet admit的用法 网站卫士 免费cdn 卡巴斯基免费试用版 域名与空间 国外的代理服务器 免费蓝钻 登陆qq空间 免费个人网页 存储服务器 好看的空间 winds websitepanel 更多