|
|
| 首页 | 技术文章 | 软件下载 | 博客 | 论坛 | 精品教程 | 黑客动画 | 视频资源 | 在线服务 | 黑客游戏 | | ||||
|
|
||||||||
|
||||||||
|
|||||
| Java套接字实现网络编程之基础篇 | |||||
作者:未知 文章来源:CnXHacker.Net 点击数: 更新时间:2006-1-18 ![]() |
|||||
|
为了示范我们在上面谈到的慨念并完成SSClient程序,下面我们创建一个SSServer程序,程序将创建一个ServerSocket对象来监听端口10000的连接请求,如果成功服务程序将等待连接输入,开始一个线程处理连接,并响应来自客户程序的命令。下面就是这段程序的代码: Listing 3: SSServer.java // SSServer.java import java.io.*; import java.net.*; import java.util.*; class SSServer { public static void main (String [] args) throws IOException { System.out.println ("Server starting...\n"); // Create a server socket that listens for incoming connection // requests on port 10000. ServerSocket server = new ServerSocket (10000); while (true) { // Listen for incoming connection requests from client // programs, establish a connection, and return a Socket // object that represents this connection. Socket s = server.accept (); System.out.println ("Accepting Connection...\n"); // Start a thread to handle the connection. new ServerThread (s).start (); } } } class ServerThread extends Thread { private Socket s; ServerThread (Socket s) { this.s = s; } public void run () { BufferedReader br = null; PrintWriter pw = null; try { // Create an input stream reader that chains to the socket's // byte-oriented input stream. The input stream reader // converts bytes read from the socket to characters. The // conversion is based on the platform's default character // set. InputStreamReader isr; isr = new InputStreamReader (s.getInputStream ()); // Create a buffered reader that chains to the input stream // reader. The buffered reader supplies a convenient method // for reading entire lines of text. br = new BufferedReader (isr); // Create a print writer that chains to the socket's byte- // oriented output stream. The print writer creates an // intermediate output stream writer that converts // characters sent to the socket to bytes. The conversion // is based on the platform's default character set. pw = new PrintWriter (s.getOutputStream (), true); // Create a calendar that makes it possible to obtain date // and time information. Calendar c = Calendar.getInstance (); // Because the client program may send multiple commands, a // loop is required. Keep looping until the client either // explicitly requests termination by sending a command // beginning with letters BYE or implicitly requests // termination by closing its output stream. do { // Obtain the client program's next command. String cmd = br.readLine (); // Exit if client program has closed its output stream. if (cmd == null) break; // Convert command to uppercase, for ease of comparison. cmd = cmd.toUpperCase (); // If client program sends BYE command, terminate. if (cmd.startsWith ("BYE")) break; // If client program sends DATE or TIME command, return // current date/time to the client program. if (cmd.startsWith ("DATE") || cmd.startsWith ("TIME")) pw.println (c.getTime ().toString ()); // If client program sends DOM (Day Of Month) command, // return current day of month to the client program. if (cmd.startsWith ("DOM")) pw.println ("" + c.get (Calendar.DAY_OF_MONTH)); // If client program sends DOW (Day Of Week) command, // return current weekday (as a string) to the client // program. if (cmd.startsWith ("DOW")) switch (c.get (Calendar.DAY_OF_WEEK)) { case Calendar.SUNDAY : pw.println ("SUNDAY"); break; case Calendar.MONDAY : pw.println ("MONDAY"); break; case Calendar.TUESDAY : pw.println ("TUESDAY"); break; case Calendar.WEDNESDAY: pw.println ("WEDNESDAY"); break; case Calendar.THURSDAY : pw.println ("THURSDAY"); break; case Calendar.FRIDAY : pw.println ("FRIDAY"); break; case Calendar.SATURDAY : pw.println ("SATURDAY"); } // If client program sends DOY (Day of Year) command, // return current day of year to the client program. if (cmd.startsWith ("DOY")) pw.println ("" + c.get (Calendar.DAY_OF_YEAR)); // If client program sends PAUSE command, sleep for three // seconds. if (cmd.startsWith ("PAUSE")) try { Thread.sleep (3000); } catch (InterruptedException e) { } } while (true); { catch (IOException e) { System.out.println (e.toString ()); } finally { System.out.println ("Closing Connection...\n"); try { if (br != null) br.close (); if (pw != null) pw.close (); if (s != null) s.close (); } catch (IOException e) { } } } } 运行这段程序将得到下面的输出: Server starting... Accepting Connection... Closing Connection... SSServer的源代码声明了一对类:SSServer 和ServerThread;SSServer的main()方法创建了一个ServerSocket对象来监听端口10000上的连接请求,如果成功, SSServer进入一个无限循环中,交替调用ServerSocket的 accept() 方法来等待连接请求,同时启动后台线程处理连接(accept()返回的请求)。线程由ServerThread继承的start()方法开始,并执行ServerThread的run()方法中的代码。 一旦run()方法运行,线程将创建BufferedReader, PrintWriter和 Calendar对象并进入一个循环,这个循环由读(通过BufferedReader的 readLine())来自客户程序的一行文本开始,文本(命令)存储在cmd引用的string对象中,如果客户程序过早的关闭输出流,会发生什么呢?答案是:cmd将得不到赋值。 注意必须考虑到这种情况:在服务程序正在读输入流时,客户程序关闭了输出流,如果没有对这种情况进行处理,那么程序将产生异常。 一旦编译了SSServer的源代码,通过输入Java SSServer来运行程序,在开始运行SSServer后,就可以运行一个或多个SSClient程序 |
|||||
| 文章录入:IceRiver 责任编辑:admin | |||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | |||||
| 最新热点 | 最新推荐 | 相关文章 | ||
| SIDVault Simple_Bind 函数多 SIDVault Simple_Bind 函数多 ClamAV 实现多个空指针引用漏 ClamAV 实现多个空指针引用漏 Safari在处理Java Applet的下 毒王AV终结者再生新变种 网游 Windows Live新主页类似Live 攻陷AJAX 黑帽大会研究人员展示如何攻 Java曝严重漏洞 影响手机PC多 |
网友评论:(只显示最新5条。评论内容只代表网友观点,与本站立场无关!) |
| 关于我们 - 版权声明 - 帮助(?) - 广告服务 - 联系我们 - 友情链接 - 用户注册 - | Powered by ICE RIVER - STUDIO |
| » CnXHacker.CoM | © CopyRight 2002-2006, CnXHacker.CoM™, Inc. All Rights Reserved. |