隨筆 - 64  文章 - 9  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(6)

          我參與的團隊

          隨筆分類(88)

          隨筆檔案(92)

          文章分類(142)

          文章檔案(182)

          天基成員

          學習園

          我的海角

          搜索

          •  

          積分與排名

          • 積分 - 183404
          • 排名 - 319

          最新評論

          TCP

          TCPServer.java
          import java.net.*;
          import java.io.*;

          public class TCPServer {
           public static void main(String[] args) throws Exception {
            ServerSocket ss = new ServerSocket(6666);//時刻監聽,等//待client(客戶端)連接,6666是端口號
            while(true) {
             Socket s = ss.accept();//Socket是client端的連//接,插座用于接收       accept()
          System.out.println("a client connect!");
             DataInputStream dis = new DataInputStream(s.getInputStream());
             System.out.println(dis.readUTF());//從流 in 中讀取用 UTF-8 修改版格式編碼的 Unicode 字符格式的字符串
             dis.close();
             s.close();
            }
            
           }
          }


          TCPClient.java

          import java.net.*;
          import java.io.*;

          public class TCPClient {
           public static void main(String[] args) throws Exception {
            Socket s = new Socket("127.0.0.1", 6666);//插座  用于連接//server服務端,127.0.0.1本機ip, 6666端口號
            OutputStream os = s.getOutputStream();//通過輸出管道說話
            DataOutputStream dos = new DataOutputStream(os);//包裝輸出管道,應用程序以適當方式將基本 Java 數據類型寫入輸出流
            //或者InputStream is = ss.getInputStream();
                   DataInputStream dis = new DataInputStream(is);
            Thread.sleep(30000);//   線程狀態
            dos.writeUTF("hello server!");//writeUTF()將表示長度信息的兩個字節寫入輸出流
            dos.flush();//刷新此 Image 對象正在使用的所有資源
            dos.close();
            s.close();
           }
          }


          posted on 2009-04-30 09:35 鵬凌 閱讀(114) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 布拖县| 西贡区| 文成县| 郁南县| 镇安县| 光泽县| 忻城县| 宜兰市| 石首市| 布尔津县| 长乐市| 昌宁县| 云南省| 正定县| 惠东县| 历史| 涿鹿县| 化隆| 呼图壁县| 杂多县| 曲水县| 普定县| 荆门市| 肥东县| 聂拉木县| 渝北区| 南平市| 巩留县| 察雅县| 阿鲁科尔沁旗| 聊城市| 山阴县| 遵义县| 宁城县| 水城县| 荃湾区| 赞皇县| 宁河县| 双辽市| 长汀县| 汕尾市|