杰點(diǎn)

          hello java

          TCP通信

          ServerSocket類

          構(gòu)造函數(shù)
          public ServerSocket()
          public ServerSocket(int port) //默認(rèn)數(shù)量是50個(gè)
          public ServerSocket(int port,int backlog)
          public ServerSocket(int port,int backlog,
            InetAddress bindAddr)
          close()

          accept() 返回socket對(duì)象

          Socket類

          構(gòu)造方法
          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() 返回輸入流對(duì)象
          getOutputStream()返回輸出流對(duì)象

           1 //基于TCP的服務(wù)器端
           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); //創(chuàng)建服務(wù)器socket對(duì)象
          12         Socket s=ss.accept(); //等待客戶端連接,返回socket對(duì)象
          13         InputStream ips= s.getInputStream();//返回 輸入流對(duì)象
          14         OutputStream ops=s.getOutputStream();//獲得 輸出流對(duì)象
          15         
          16         ops.write("welcome,jiedian's Server\n".getBytes());
          17         
          18         BufferedReader br = new BufferedReader(new InputStreamReader(ips));
          19         //包裝網(wǎng)絡(luò)輸入流,字節(jié)流
          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 杰點(diǎn) 閱讀(294) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA


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


          網(wǎng)站導(dǎo)航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計(jì)

          留言簿

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 德惠市| 武鸣县| 天津市| 石台县| 杭锦后旗| 莒南县| 江口县| 长泰县| 郸城县| 贵溪市| 内乡县| 钦州市| 贡觉县| 堆龙德庆县| 徐闻县| 吴川市| 兴国县| 左云县| 阿拉善盟| 漳平市| 舟山市| 满城县| 河津市| 额济纳旗| 镇巴县| 绥宁县| 望奎县| 苍溪县| 营口市| 德钦县| 集贤县| 三台县| 阜平县| 临安市| 互助| 绥棱县| 绩溪县| 吉水县| 洪湖市| 邵东县| 黄冈市|