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

          常用鏈接

          留言簿(6)

          我參與的團隊

          隨筆分類(88)

          隨筆檔案(92)

          文章分類(142)

          文章檔案(182)

          天基成員

          學習園

          我的海角

          搜索

          •  

          積分與排名

          • 積分 - 183507
          • 排名 - 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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 揭阳市| 察雅县| 封丘县| 蒲城县| 小金县| 成武县| 二手房| 青冈县| 砚山县| 宁陕县| 精河县| 读书| 疏勒县| 镇沅| 晋中市| 巴林左旗| 桦川县| 凤城市| 辉南县| 清徐县| 峨山| 广汉市| 泌阳县| 四平市| 北流市| 青阳县| 西和县| 谷城县| 昆山市| 贡觉县| 达拉特旗| 天津市| 资阳市| 农安县| 即墨市| 合川市| 当雄县| 洛南县| 旬邑县| 泸西县| 张家界市|