服務端:
           1 package com.socket;
           2 
           3 import java.io.*;
           4 import java.net.*;
           5 public class Server
           6 {
           7     public Server ()
           8     {
           9     }
          10     static ServerSocket ss;
          11     static   Socket client;
          12     public static void main (String args[])
          13     {
          14         try
          15         {
          16             ss = new ServerSocket (8099);
          17             client = ss.accept ();
          18         }
          19         catch (Exception ex)
          20         {
          21             ex.printStackTrace ();
          22         }
          23         //實現多線程
          24         new ServerThread (ss,client).start ();
          25     }
          26 }
          27 class ServerThread extends Thread
          28 {
          29     ServerSocket ss;
          30     Socket client;
          31     
          32     public ServerThread (ServerSocket bb,Socket aa)
          33     {
          34         ss = bb;
          35         client = aa;
          36     }
          37     public void run ()
          38     {
          39         try
          40         {
          41             File  file = new File ("test2.jpg");
          42             //創建網絡服務器接受客戶請求
          43             /////////////////////////////////////////////////////////////////////
          44             //得到客戶輸入的信息,這樣可以處理不同請求
          45             BufferedReader in = new BufferedReader (new InputStreamReader (client.getInputStream ()));
          46             if((in.readLine ()).equals ("1"))
          47             {
          48                 file = new File ("test1.jpg");
          49             }
          50             ///////////////////////////////////////////////////////////////////////
          51             FileInputStream fos = new FileInputStream (file);
          52             //創建網絡輸出流并提供數據包裝器
          53             OutputStream netOut = client.getOutputStream ();
          54             OutputStream doc = new DataOutputStream (new BufferedOutputStream (netOut));
          55             //創建文件讀取緩沖區
          56             byte[] buf = new byte[2048];
          57             int num = fos.read (buf);
          58             while(num!=(-1))
          59             {
          60                 doc.write (buf,0,num);//把文件數據寫出網絡緩沖區
          61                 doc.flush ();//刷新緩沖區把數據寫往客戶端
          62                 num=fos.read (buf);//繼續從文件中讀取數據
          63             }
          64             fos.close ();
          65             doc.close ();
          66         }
          67         catch (FileNotFoundException ex)
          68         {
          69             ex.printStackTrace ();
          70         }
          71         catch (IOException ex)
          72         {
          73             ex.printStackTrace ();
          74         }
          75     }
          76 }
          77 
          客戶端:
           1 package com.socket;
           2 
           3 
           4 import java.io.*;
           5 import java.net.*;
           6 
           7 public class Client
           8 {
           9     
          10     public Client ()
          11     {
          12         
          13     }
          14     public static void main(String[] args)throws Exception
          15     {
          16         //使用本地文件系統接受網絡數據并存為新文件
          17         File file = new File("newFile.jpg"); 
          18         if(file.length()!= 0)
          19         {
          20             file.delete();
          21             System.out.println(file.getPath());
          22             System.out.println("執行刪除!");
          23                     
          24         }
          25         file.createNewFile();
          26         RandomAccessFile raf = new RandomAccessFile(file,"rw");
          27         // 通過Socket連接服務器
          28         Socket server = new Socket("127.0.0.1",8099);
          29         
          30 ///////////////////////////////////////////////////////////////////////////////
          31         //創建向服務器傳送流對象
          32         OutputStream out = server.getOutputStream();
          33         PrintWriter netout = new PrintWriter(out,true);
          34         BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
          35         netout.println(line.readLine());
          36 ///////////////////////////////////////////////////////////////////////////////
          37         
          38         //創建網絡接受流接受服務器文件數據
          39         InputStream netIn = server.getInputStream();
          40         InputStream in = new DataInputStream(new BufferedInputStream(netIn));
          41         //創建緩沖區緩沖網絡數據
          42         byte[] buf = new byte[2048];
          43         int num = in.read(buf);
          44         while(num!=(-1))
          45         {    //是否讀完所有數據
          46             raf.write(buf,0,num);//將數據寫往文件
          47             raf.skipBytes(num);//順序寫文件字節
          48             num=in.read(buf);//繼續從網絡中讀取文件
          49         }
          50         in.close();
          51         raf.close();
          52     }
          53  }
          54 


          posted on 2007-02-08 13:48 -274°C 閱讀(17896) 評論(1)  編輯  收藏 所屬分類: JAVA計算機綜合


          FeedBack:
          # re: SOCKET編程
          2007-02-13 14:13 | αβγ
          多線程的實現范例:(程序有修改)
          public static void main (String args[])
          {
          try
          {
          server = new ServerSocket (10888);
          while(true)
          {
          client = server.accept ();
          new Server (server,client).start ();
          }
          }
          catch(Exception e)
          {
          System.out.println (e.toString ());
          }

          }  回復  更多評論
            

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914107
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 南通市| 博罗县| 本溪| 古丈县| 云浮市| 怀宁县| 万山特区| 万盛区| 兴安盟| 翁源县| 化隆| 博客| 安远县| 长汀县| 长岛县| 泰和县| 石门县| 汉寿县| 武宣县| 宁乡县| 金坛市| 涿鹿县| 武威市| 德昌县| 梓潼县| 石城县| 中超| 岐山县| 上犹县| 松原市| 隆昌县| 连云港市| 湘西| 通山县| 大厂| 普陀区| 莱阳市| 小金县| 苏尼特右旗| 洛南县| 吉木乃县|