`
moshangchenzi
  • 浏览: 51905 次
  • 性别: Icon_minigender_1
  • 来自: 南宁
社区版块
存档分类
最新评论

JDK6.0学习笔记(九)tcp多线程服务器

阅读更多
  1. /**
  2.  * tcp开发,有连接的Socket操作采用TCP协议,效率低,传输可靠性高
  3.  * TCP下的Socket必须在发送数据之前与目的地的Socket取得一个连接
  4.  * 一旦建立了连接,Socket就可以使用数据流借口,按照打开-读-写-关闭 的顺序来操作数据流
  5.  * 多线程服务器
  6.  * 主程序在9080端口侦听到新的客户端的连接请求时,启动一个独立的子线程为其服务 
  7.  * */
  8. import java.net.*;
  9. public class StartupEchoServer {
  10.     public static void main(String[] args) {
  11.         try {
  12.             ServerSocket serverSocket = new ServerSocket(9080);//主线程负责接受客户端连接
  13.             System.out.println("EchoServer在9080端口上侦听......");
  14.             while (true) {
  15.                 Socket socket = serverSocket.accept();
  16.                 System.out.println("建立一个连接...\n");
  17.                 new EchoServer(socket).start();
  18.             }
  19.         } catch (Exception e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23. }
 
  1. /**
  2.  * tcp开发,多线程服务器客户端 
  3.  * */
  4. import java.io.*;
  5. import java.net.*;
  6. public class EchoClient {
  7.     public static void main(String[] args) {
  8.         Socket socket = null;
  9.         BufferedReader in = null;
  10.         PrintWriter out = null;
  11.         try {
  12.             socket = new Socket("localhost"9080);// 创建与服务器的连接,获取IP地址
  13.             in = new BufferedReader(new InputStreamReader(socket
  14.                     .getInputStream()));
  15.             out = new PrintWriter(socket.getOutputStream(), true);
  16.             out.println("DOW");
  17.             System.out.println("今天是:" + in.readLine());
  18.             out.println("DOY");
  19.             System.out.println("今天是一年的:" + in.readLine());
  20.             byte[] b = new byte[2048];
  21.             String msg = new String(b, 0, System.in.read(b));
  22.             out.println("FREE:" + msg);
  23.             System.out.println("收到答复:" + in.readLine());
  24.         } catch (Exception e) {
  25.         } finally {
  26.             try {
  27.                 if (in != null)
  28.                     in.close();
  29.                 if (out != null)
  30.                     out.close();
  31.                 if (socket != null)
  32.                     socket.close();// Socket.close() 客户端主动断开连接
  33.             } catch (Exception e1) {
  34.             }
  35.         }
  36.     }
  37. }
  1. /**
  2.  * tcp开发,多线程服务器
  3.  * 子线程负责处理客户端的请求
  4.  * */
  5. import java.io.*;
  6. import java.net.*;
  7. import java.util.*;
  8. public class EchoServer extends Thread {
  9.     private Socket socket = null;//主程序传递来的Socket对象
  10.     public EchoServer(Socket s) {
  11.         this.socket = s;
  12.     }
  13.     public void run() {
  14.         BufferedReader in = null;
  15.         PrintWriter out = null;
  16.         Calendar c = Calendar.getInstance();//子线程负责处理客户端的请求
  17.         try {
  18.             in = new BufferedReader(new InputStreamReader(this.socket
  19.                     .getInputStream()));//取得输入流
  20.             out = new PrintWriter(this.socket.getOutputStream(), true);//取得输出流
  21.             do {
  22.                 String msg = in.readLine();
  23.                 if (msg == null)
  24.                     break;
  25.                 msg = msg.toUpperCase();
  26.                 System.out.println("收到指令:" + msg);
  27.                 if (msg.equals("DOW"))//返回今天是星期几
  28.                     switch (c.get(Calendar.DAY_OF_WEEK)) {
  29.                     case Calendar.SUNDAY:
  30.                         out.println("SUNDAY");
  31.                         break;
  32.                     case Calendar.MONDAY:
  33.                         out.println("MONDAY");
  34.                         break;
  35.                     case Calendar.TUESDAY:
  36.                         out.println("TUESDAY");
  37.                         break;
  38.                     case Calendar.WEDNESDAY:
  39.                         out.println("WEDNESDAY");
  40.                         break;
  41.                     case Calendar.THURSDAY:
  42.                         out.println("THURSDAY");
  43.                         break;
  44.                     case Calendar.FRIDAY:
  45.                         out.println("FRIDAY");
  46.                         break;
  47.                     case Calendar.SATURDAY:
  48.                         out.println("SATURDAY");
  49.                     }
  50.                 if (msg.equals("DOY"))//今天是一年的第几天
  51.                     out.println("" + c.get(Calendar.DAY_OF_YEAR));
  52.                 if (msg.startsWith("FREE"))//客户端的信息以FEEE开头,返回信息原文
  53.                     out.println(msg.substring(5));
  54.             } while (true);
  55.         } catch (Exception e) {
  56.         } finally {
  57.             try {
  58.                 System.out.println("关闭一个连接...\n");
  59.                 if (in != null)
  60.                     in.close();
  61.                 if (out != null)
  62.                     out.close();
  63.                 if (this.socket != null)
  64.                     this.socket.close();//ServerSocket.close() 服务器主动断开连接
  65.             } catch (Exception e3) {
  66.             }
  67.         }
  68.     }
  69. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics