快速业务通道

C/S环境下Java应用程序运行过程讲解 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-25
c = new Connection(client_socket); } } catch(IOException e) fail(e,"Exception while listening for connections") } // 启动服务器主程序 public static void main(String args[]) { int port = 0; if (args.length == 1) { try port = Integer.parseInt(args[0]); catch(NumberFormatException e) port = 0; } new Server(port); }                       // End of the main } // End of Server class //以下定义了Connection类,它是用来处理与客户机的所有通信的线程。 class Connection extends Thread { protected Socket client; protected DataInputStream in; protected PrintStream out; // 初始化通信流并启动线程 public Connection(Socket client_socket) { client = client_socket; 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 // 服务例程:读出一行文本;反转文本;返回文本。 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、编写客户机类Java 程序 // Client.java import java.io.*; import java.net.*; public class Client extends { public static final int Default_Port = 6543; // 定义出错例程 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; // 解析端口参数 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{ // 产生一个Socket ,通过指定的端口与主机通信。 s = new Socket(args[0], port); // 产生用于发出和接收的文本字符流 DataInputStream sin = new DataInputStream(s.getInputStream()); PrintStream sout = new DataInputStream(s.getInputStream()); // 从控制台读入字符流 DataInputStream in = new DataInputStream(System.in); // 返回连接的地址和端口 ystem.out.println("Connected to" s.getInetAddress() ":" s.getPort()); String line; For(;;) { // 显示提示符 System.out.print(" >"); System.out.flush(); // 读入控制台输入的一行字符 line = in.readline(); if (line == null) break; // 将接收的文本行送至服务器 sout.println(line); // 从服务器接收一行字符 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 catc

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号