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("关闭时出现异常";}
}
}
}
ttcloud怎么样?ttcloud是一家海外服务器厂商,运营服务器已经有10年时间,公司注册地址在香港地区,业务范围包括服务器托管,机柜托管,独立服务器等在内的多种服务。我们后台工单支持英文和中文服务。TTcloud最近推出了新上架的日本独立服务器促销活动,价格 $70/月起,季付送10Mbps带宽。也可以跟进客户的需求进行各种DIY定制。点击进入:ttcloud官方网站地址TTcloud拥有自...
RAKsmart怎么样?RAKsmart机房即日起开始针对洛杉矶机房的独立服务器进行特别促销活动:低至$76.77/月,最低100Mbps带宽,最高10Gbps带宽,优化线路,不限制流量,具体包括有:常规服务器、站群服务器、10G大带宽服务器、整机机柜托管。活动截止6月30日结束。RAKsmart,美国华人老牌机房,专注于圣何塞服务器,有VPS、独立服务器等。支持PayPal、支付宝付款。点击直达...
? ? ? ?创梦网络怎么样,创梦网络公司位于四川省达州市,属于四川本地企业,资质齐全,IDC/ISP均有,从创梦网络这边租的服务器均可以****,属于一手资源,高防机柜、大带宽、高防IP业务,另外创梦网络近期还会上线四川联通大带宽,四川联通高防IP,一手整CIP段,四川电信,联通高防机柜,CN2专线相关业务。成都优化线路,机柜租用、服务器云服务器租用,适合建站做游戏,不须要在套CDN,全国访问快...