Java在Client/Server网络中的应用 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-23
ket; ???? try ???? { ???? in = new DataInputStream(client.getinputStream()); ???? out = new PrintStream(client.getOutputStream()); ???? } ???? catch(IOException e) ???? { ???? try client.close(); ???? catch(IOException e2); ???? System.err.println("Exception while getting socket streram: " + e); ???? Return; ???? } ???? this.start; ???? }???????????????????????????????????? // End of Connection method ???? // ·t??ày3ì£o?á3?ò?DD??±?£?·′×a??±?£?·μ????±??£ ???? public void run() ???? { ???? String line; ???? StringBuffer revline; ???? int len; ???? try ???? { ???? for(;;) ???? { ???? // Read a line ???? line = in.readline(); ???? if(line == null) break; ???? // Reverse the line ???? len = line.length(); ???? revline = new StringBuffer(len); ???? for(int i = len-1; i >=0; i--) ???? revline.insert(len-1-I;line.charAt(i)); ???? // Write out the reverse line ???? out.println(revline); ???? } ???? catch(IOException e); ???? finally try client.close(); ???? catch(IOException e2); ???? } ???? // End of run method ???? } ???? // End of Connection class ???? 3?¢±àD′?í?§?úààJava 3ìDò ???? // Client.java ???? import java.io.*; ???? import java.net.*; ???? public class Client extends ???? { ???? public static final int Default_Port = 6543; ???? // ?¨ò?3?′íày3ì ???? public static final void usage() ???? { ???? System.out.println("Usage: Java Client []"); ???? System.exit(0); ???? } ???? public static void main(String args[]) ???? { ???? int port = Default_Port; ???? Socket s = null; ???? // ?a?????ú2?êy ???? if ((args.length != 1)&&(args.length != 2 )) usage(); ???? if (args.length == 1) ???? port = Default_Port; ???? else ???? { ???? try port = Integer.parseInt(args[1]); ???? catch(NumberFormaatException e) usage(); ???? } ???? try{ ???? // 2úéúò???Socket £?í¨1y???¨μ????úó??÷?úí¨D??£ ???? s = new Socket(args[0], port); ???? // 2úéúó?óú·¢3?oí?óê?μ???±?×?·?á÷ ???? DataInputStream sin = new DataInputStream(s.getInputStream ()); ???? PrintStream sout = new DataInputStream(s.getInputStream()); ???? // ′ó????ì¨?áè?×?·?á÷ ???? DataInputStream in = new DataInputStream(System.in); ???? // ·μ??á??óμ?μ??·oí???ú ???? ystem.out.println("Connected to"+s.getInetAddress()+":"+ s.getPort()); ???? String line; ???? For(;;) ???? { ???? // ??ê?ìáê?·? ???? System.out.print(" >"); ???? System.out.flush(); ???? // ?áè?????ì¨ê?è?μ?ò?DD×?·? ???? line = in.readline(); ???? if (line == null) break; ???? // ???óê?μ???±?DD?í?á·t???÷ ???? sout.println(line); ???? // ′ó·t???÷?óê?ò?DD×?·? ???? line = sin.readline(); ???? // Check if connection is closed(i.e. for EOF) ???? if(line == null) ???? { ???? System.out.println("Connection closed by server."); ???? Break; ???? } ???? // ?ú????ì¨é???ê??óê?μ?×?·? ???? System.out.println(line); ???? } ???? // End of for loop ???? } ???? / End of try ???? |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于Java在Client/Server网络中的应用 - 编程入门网的所有评论