《安卓網路編程》之第七篇 java編寫Service程式

來源:互聯網
上載者:User

標籤:

 

 

  1 import java.io.*;  2 import java.awt.*;  3 import java.awt.event.*;  4 import javax.swing.*;  5 import java.net.*;  6 import java.util.*;  7   8 class Conn extends Thread{  9     private JTextArea txt; 10     private Socket st; 11     private String msg = null; 12     private BufferedReader br = null; 13     private PrintStream ps; 14     public Conn(Socket st,JTextArea txt){ 15         this.st = st; 16         this.txt = txt; 17         start(); 18     } 19     public void run(){ 20         try{ 21             br = new BufferedReader(new InputStreamReader(st.getInputStream())); 22             ps = new PrintStream(new DataOutputStream(st.getOutputStream())); 23         }catch(Exception e){ 24             System.err.println("input failed"); 25         } 26         while(true){ 27             try{ 28                 msg = br.readLine();//阻塞方法 29                 txt.append("從用戶端收到資訊:"+msg+‘\n‘); 30                 txt.append("資訊接受時間是:"+new Date()+"\n"); 31                 //建立讀取檔案線程,並將讀取到的內容通過伺服器發送出去 32                 FileThread ft = new FileThread(); 33                 Thread t = new Thread(ft); 34                 t.start(); 35                 while(ft.flag); 36                 System.out.println(ft.readData); 37                 Server.send(ft.readData); 38                  39             }catch(Exception e){ 40                 System.err.println("connection closed"); 41                 break; 42             } 43         } 44     } 45     public void send(String msg){ 46       ps.println(msg); 47     } 48      49 } 50  51  52 class FileThread implements Runnable{ 53     public String readData = ""; 54     public static boolean flag = true; 55     public void run() {     56         readData = readFileByLines("D:\\Hello.txt"); 57         flag = false; 58         System.out.println(readData); 59     } 60      61     /** 62      * 以行為單位讀取檔案,常用於讀面向行的格式檔案 63      */ 64     public String readFileByLines(String fileName) { 65         File file = new File(fileName); 66         BufferedReader reader = null; 67         String tempString = null; 68         String readData = ""; 69         try { 70             System.out.println("以行為單位讀取檔案內容,一次讀一整行:"); 71             reader = new BufferedReader(new FileReader(file)); 72             int line = 1; 73             74             // 一次讀入一行,直到讀入null為檔案結束 75             while ((tempString = reader.readLine()) != null) { 76                 // 顯示行號 77 //                System.out.println("line " + line + ": " + tempString); 78 //                line++; 79                 readData = readData.concat(tempString);//字串拼接推薦函數 80             } 81             reader.close(); 82         } catch (IOException e) { 83             e.printStackTrace(); 84         } finally { 85             if (reader != null) { 86                 try { 87                     reader.close(); 88                 } catch (IOException e1) { 89                 } 90             } 91         } 92          93         return  readData; 94     } 95      96 } 97  98  99 public class Server extends JFrame{100     private JTextArea txt;101     private ServerSocket ss;102     public String tempString = null;103     private static java.util.List<Conn> conns = new ArrayList<Conn>();104     public Server(){105        txt = new JTextArea();106        this.setTitle("伺服器");107        this.setLayout(new BorderLayout());108        this.add(new JScrollPane(txt),BorderLayout.CENTER);109        this.setSize(500,300);110        this.setVisible(true);111        112        run();113     }114     public void run(){115         try{116             ss = new ServerSocket(3000);117         }catch(Exception e){118             System.err.println("open socket failed");119         }120         txt.append("伺服器已經啟動!"+"\n");121         while(true){122             try{123                 Socket st=ss.accept();124                 conns.add(new Conn(st,txt));125             }126             catch(IOException ex){127                 System.err.println(ex);128             }129         }130     }131     public static void send(String msg){132       for(Conn c:conns)133         c.send(msg);134     }135     136     public static void main(String args[]){137         138         Server myserver=new Server();    139      }140      141 }

 

 

另外一種寫法:

 

《安卓網路編程》之第七篇 java編寫Service程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.