服務(wù)端:
           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         //實(shí)現(xiàn)多線程
          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             //創(chuàng)建網(wǎng)絡(luò)服務(wù)器接受客戶請求
          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             //創(chuàng)建網(wǎng)絡(luò)輸出流并提供數(shù)據(jù)包裝器
          53             OutputStream netOut = client.getOutputStream ();
          54             OutputStream doc = new DataOutputStream (new BufferedOutputStream (netOut));
          55             //創(chuàng)建文件讀取緩沖區(qū)
          56             byte[] buf = new byte[2048];
          57             int num = fos.read (buf);
          58             while(num!=(-1))
          59             {
          60                 doc.write (buf,0,num);//把文件數(shù)據(jù)寫出網(wǎng)絡(luò)緩沖區(qū)
          61                 doc.flush ();//刷新緩沖區(qū)把數(shù)據(jù)寫往客戶端
          62                 num=fos.read (buf);//繼續(xù)從文件中讀取數(shù)據(jù)
          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         //使用本地文件系統(tǒng)接受網(wǎng)絡(luò)數(shù)據(jù)并存為新文件
          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("執(zhí)行刪除!");
          23                     
          24         }
          25         file.createNewFile();
          26         RandomAccessFile raf = new RandomAccessFile(file,"rw");
          27         // 通過Socket連接服務(wù)器
          28         Socket server = new Socket("127.0.0.1",8099);
          29         
          30 ///////////////////////////////////////////////////////////////////////////////
          31         //創(chuàng)建向服務(wù)器傳送流對象
          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         //創(chuàng)建網(wǎng)絡(luò)接受流接受服務(wù)器文件數(shù)據(jù)
          39         InputStream netIn = server.getInputStream();
          40         InputStream in = new DataInputStream(new BufferedInputStream(netIn));
          41         //創(chuàng)建緩沖區(qū)緩沖網(wǎng)絡(luò)數(shù)據(jù)
          42         byte[] buf = new byte[2048];
          43         int num = in.read(buf);
          44         while(num!=(-1))
          45         {    //是否讀完所有數(shù)據(jù)
          46             raf.write(buf,0,num);//將數(shù)據(jù)寫往文件
          47             raf.skipBytes(num);//順序?qū)懳募止?jié)
          48             num=in.read(buf);//繼續(xù)從網(wǎng)絡(luò)中讀取文件
          49         }
          50         in.close();
          51         raf.close();
          52     }
          53  }
          54 


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


          FeedBack:
          # re: SOCKET編程
          2007-02-13 14:13 | αβγ
          多線程的實(shí)現(xiàn)范例:(程序有修改)
          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 ());
          }

          }  回復(fù)  更多評論
            

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網(wǎng)站

          關(guān)注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 916109
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 隆昌县| 唐山市| 璧山县| 彩票| 海伦市| 蓝山县| 巨鹿县| 武宁县| 黄骅市| 嘉荫县| 莒南县| 象州县| 陆丰市| 曲麻莱县| 福清市| 鄂托克前旗| 定襄县| 黑龙江省| 大田县| 滕州市| 固安县| 河池市| 江西省| 新乡县| 崇仁县| 宣化县| 东丽区| 高唐县| 桦南县| 衡阳县| 连山| 长兴县| 无极县| 靖江市| 万州区| 兰州市| 宜兰县| 屏东县| 航空| 密山市| 营口市|