隨筆-57  評論-202  文章-17  trackbacks-0
                下面是我的一個簡單的網(wǎng)絡(luò)服務(wù)器端的程序,程序的流程是監(jiān)聽ACCEPT事件,然后往客戶端輸出一串字符串。是不是很簡單。

          package sample.nio;

          import java.io.
          *;
          import java.net.
          *;
          import java.nio.channels.
          *;
          import java.util.
          *;

          /**
           * <p>Title: </p>
           *
           * <p>Description: </p>
           *
           * <p>Copyright: Copyright (c) 2005</p>
           *
           * <p>Company: </p>
           *
           * @author George Hill
           * @version 1.0
           
          */


          public class Server {

            
          private int port;

            
          public Server(int port) {
              
          this.port = port;
            }


            
          public void startServer() throws IOException {
              
          // 創(chuàng)建ServerSocketChannel并且綁定到指定的端口
              ServerSocketChannel ssc = ServerSocketChannel.open();
              InetSocketAddress address 
          = new InetSocketAddress(InetAddress.getLocalHost(), port);
              ssc.socket().bind(address);
              ssc.configureBlocking(
          false);

              
          // 創(chuàng)建Selector,并且注冊ACCEPT事件
              Selector selector = Selector.open();
              SelectionKey skey 
          = ssc.register(selector, SelectionKey.OP_ACCEPT);

              boolean stop 
          = false;
              
          int n = 0;

              System.
          out.println("Server Start");

              
          // 輪詢
              while (!stop) {
                
          // 獲取Selector返回的時間值
                n = selector.select();

                
          // 當(dāng)傳回的值大于0事,讀時間發(fā)生了
                if (n > 0{
                  Set 
          set = selector.selectedKeys();
                  Iterator it 
          = set.iterator();

                  
          while (it.hasNext()) {
                    skey 
          = (SelectionKey) it.next();
                    it.remove();

                    
          if (skey.isAcceptable()) {
                      
          // 從channel()中取得剛剛注冊的Channel
                      Socket socket = ((ServerSocketChannel) skey.channel()).accept().socket();

                      PrintWriter writer 
          = new PrintWriter(socket.getOutputStream(), true);

                      
          // 將"Hello, World"寫入
                      writer.write("Hello, World!\n");

                      
          // 睡眠3秒
                      try {
                        Thread.sleep(
          3000);
                      }
           catch (InterruptedException ie) {
                      }


                      
          // 將"EXIT"寫入Buffer
                      writer.write("EXIT");

                      
          // 退出程序
                      writer.close();
                      
          // stop = true;
                    }

                  }

                }

              }


              ssc.close();
              System.
          out.println("Server Stop");
            }


            
          public static void main(String[] args) throws Exception {
              Server server 
          = new Server(5000);
              server.startServer();
            }

          }

          posted on 2005-05-18 12:21 小米 閱讀(734) 評論(0)  編輯  收藏 所屬分類: Java
          小米,生活在深圳,專注于Java,主要從事數(shù)據(jù)庫和網(wǎng)頁編程。現(xiàn)在在學(xué)習(xí)著Hibernate和Spring。喜歡游戲、音樂和臺球。聯(lián)系方式:georgehill@21cn.com
          <2005年5月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(27)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          我的朋友們

          我的鏈接

          搜索

          •  

          積分與排名

          • 積分 - 233786
          • 排名 - 247

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 石林| 高邑县| 武隆县| 凉山| 大同市| 东丽区| 贵港市| 马鞍山市| 康马县| 北票市| 定安县| 平南县| 巫溪县| 汉寿县| 邵阳县| 楚雄市| 耿马| 元朗区| 四子王旗| 蒙城县| 奉化市| 香港| 西盟| 固阳县| 吴堡县| 高密市| 保德县| 麟游县| 闽清县| 榆林市| 嘉禾县| 江城| 濮阳县| 阿坝| 修武县| 黎平县| 泰来县| 潢川县| 鹤庆县| 靖宇县| 武鸣县|