杰點

          hello java

          TCP通信

          ServerSocket類

          構造函數
          public ServerSocket()
          public ServerSocket(int port) //默認數量是50個
          public ServerSocket(int port,int backlog)
          public ServerSocket(int port,int backlog,
            InetAddress bindAddr)
          close()

          accept() 返回socket對象

          Socket類

          構造方法
          public Socket()
          public Socket(String host,int port)
          public Socket(InetAddress address,int port)
          public Socket(String host,int port,
           InetAddress localAddr,int localPort)
          public Socket(InetAddress address,int port,
            InetAddress localAddr,int localPort)
          方法
          getInputStream() 返回輸入流對象
          getOutputStream()返回輸出流對象

           1 //基于TCP的服務器端
           2 import java.net.*;
           3 import java.io.*;
           4 public class TcpServer {
           5 
           6     /**
           7      * @param args
           8      */
           9     public static void main(String[] args) throws Exception {
          10         // TODO Auto-generated method stub
          11         ServerSocket ss=new ServerSocket(8001); //創建服務器socket對象
          12         Socket s=ss.accept(); //等待客戶端連接,返回socket對象
          13         InputStream ips= s.getInputStream();//返回 輸入流對象
          14         OutputStream ops=s.getOutputStream();//獲得 輸出流對象
          15         
          16         ops.write("welcome,jiedian's Server\n".getBytes());
          17         
          18         BufferedReader br = new BufferedReader(new InputStreamReader(ips));
          19         //包裝網絡輸入流,字節流
          20         System.out.println(br.readLine());
          21         /*
          22         byte[] buf=new byte[1024];
          23         int length=ips.read(buf);
          24         System.out.println(new String(buf,0,length));
          25         */
          26         
          27         br.close();
          28         //ips.close();
          29         ops.close();
          30         s.close();
          31         ss.close();
          32     }
          33 
          34 }
          35 

          posted on 2010-12-29 12:46 杰點 閱讀(293) 評論(0)  編輯  收藏 所屬分類: JAVA


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


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          留言簿

          文章分類

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 福泉市| 曲阜市| 梁山县| 金川县| 武宣县| 蚌埠市| 灵川县| 海门市| 永福县| 托里县| 天祝| 咸丰县| 德令哈市| 缙云县| 定南县| 仙居县| 太仓市| 白山市| 突泉县| 高平市| 尼勒克县| 延川县| 城固县| 津市市| 遂川县| 海门市| 永平县| 林州市| 吉安市| 正定县| 沅陵县| 油尖旺区| 宜川县| 温州市| 宾阳县| 西乌珠穆沁旗| 乌拉特中旗| 京山县| 湄潭县| 皋兰县| 泰宁县|