當(dāng)柳上原的風(fēng)吹向天際的時(shí)候...

          真正的快樂來源于創(chuàng)造

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          注:下例僅為學(xué)習(xí)使用,高性能的服務(wù)器例子請采用NIO方案。

          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.io.PrintWriter;
          import java.net.InetAddress;
          import java.net.ServerSocket;
          import java.net.Socket;
          import java.util.Scanner;

          public class ThreadedServer{
              
          public static void main(String[] args) throws IOException{
                  ServerSocket s
          =new ServerSocket(9999);
                  
                  
          while(true){
                      Socket incoming
          =s.accept();
                      InetAddress address
          =incoming.getInetAddress();
                      System.out.println(
          "開始與"+address+"進(jìn)行通訊.");
                      
                      
          new Thread(new EchoHandler(incoming)).start();
                  }
              }
          }

          class EchoHandler implements Runnable{
              
          private Socket incoming;
              
              
          public EchoHandler(Socket incoming){
                  
          this.incoming=incoming;
              }
              
              
              
          public void run(){
                  
          try{
                      InputStream  inStram
          =incoming.getInputStream();
                      OutputStream outStream
          =incoming.getOutputStream();
                      
                      Scanner in
          =new Scanner(inStram);
                      PrintWriter out
          =new PrintWriter(outStream);
                      out.println(
          "你好!輸入Bye退出。");
                      
                      
          while(in.hasNextLine()){
                          String line
          =in.nextLine();
                          System.out.println(
          "客戶端說:"+line);
                          
                          
          if(line.equalsIgnoreCase("Bye")){
                              
          break;
                          }
                          
          else if(line.equalsIgnoreCase("login")){
                              System.out.println(
          "客戶端想登錄");
                          }
                          
          else if(line.equalsIgnoreCase("register")){
                              System.out.println(
          "客戶端想注冊");                
                          }
                          
          else if(line.equalsIgnoreCase("dispacth")){
                              System.out.println(
          "客戶端想轉(zhuǎn)發(fā)信息");
                          }
                          
          else if(line.equalsIgnoreCase("userlist")){
                              System.out.println(
          "客戶端想得到用戶列表");
                          }
                          
          else{
                              out.println(
          "收到:"+line);
                          }
                      }
                  }
                  
          catch(Exception ex){
                      ex.printStackTrace();
                  }
                  
          finally{
                      
          try {
                          incoming.close();
                      } 
          catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }

          可采用Telnet與上述服務(wù)器端通信,Vista上Telnet可如下方式開啟:
          1、打開控制面板;
          2、在左側(cè)選擇“經(jīng)典視圖”,然后在右側(cè)選擇“程序和功能”;
          3、在出現(xiàn)的“程序和功能”窗口左側(cè)中點(diǎn)擊“打開或關(guān)閉Windows功能”;
          4、在彈出的“Windows功能”窗口中勾選上“Telnet客戶端”;
          5、確定后退出,Windows會自動(dòng)開始配置激活;
          6、在“開始搜索”框中輸入“telnet 127.0.0.1 9999



          posted on 2010-12-23 14:19 何楊 閱讀(464) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 垦利县| 泗水县| 沁源县| 和顺县| 深圳市| 河间市| 金华市| 绍兴县| 五河县| 古田县| 公主岭市| 定安县| 富顺县| 南雄市| 原阳县| 盘锦市| 民乐县| 巴东县| 靖安县| 安国市| 云梦县| 平泉县| 临高县| 通州区| 进贤县| 安龙县| 淮南市| 中方县| 洛川县| 溧阳市| 通山县| 民和| 铜山县| 阳信县| 通渭县| 玛多县| 高雄县| 潞西市| 甘泉县| 东安县| 漳州市|