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刚刚在WHT发布了几款促销服务器,Intel Xeon X3430,8GB内存,1TB HDD,30M不限流量,月付$60.00。Nocser是一家注册于马来西亚的主机商,主要经营虚拟主机、VPS和马来西亚独立服务器业务,数据中心位于马来西亚AIMS机房,线路方面,AIMS到国内电信一般,绕日本NTT;联通和移动比较友好,联通走新加坡,移动走香港,延迟都在100左右。促销马来西亚服务器...
spinservers是Majestic Hosting Solutions,LLC旗下站点,主营美国独立服务器租用和Hybrid Dedicated等,spinservers这次提供的大硬盘、大内存服务器很多人很喜欢。TheServerStore自1994年以来,它是一家成熟的企业 IT 设备供应商,专门从事二手服务器和工作站业务,在德克萨斯州拥有40,000 平方英尺的仓库,库存中始终有数千台...
95idc是一家香港公司,主要产品香港GIA线路沙田CN2线路独服,美国CERA高防服务器,日本CN2直连服务器,即日起,购买香港/日本云主机,在今年3月份,95IDC推出来一款香港物理机/香港多ip站群服务器,BGP+CN2线路终身7折,月付350元起。不过今天,推荐一个价格更美的香港物理机,5个ip,BGP+CN2线路,月付299元起,有需要的,可以关注一下。95idc优惠码:优惠码:596J...