1 / 5

Applet 與網路通訊

Applet 與網路通訊. 因為安全的因素 , 導致由瀏覽器下載的 Applet 不能對使用者的機器動手腳 ( 做任何 IO 動作 ), 也不能私自連線到其他的電腦 , 那 Applet 不就是玩具了嗎 ? 只能秀秀資料 , 那用 Flash 就好了又快又好寫 ! 那為何要用 applet ? 其實 Applet 雖然不可以與其他的電腦連線 , 卻可以與下載端的 Web Server 電腦連線 . 藉由這個中央電腦 , 我們將可以製作出類似 MSN Messsager 功能的 Applet 軟體. 井民全 製作.

Télécharger la présentation

Applet 與網路通訊

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Applet 與網路通訊 因為安全的因素, 導致由瀏覽器下載的Applet 不能對使用者的機器動手腳 (做任何 IO動作),也不能私自連線到其他的電腦,那 Applet 不就是玩具了嗎? 只能秀秀資料, 那用 Flash 就好了又快又好寫! 那為何要用 applet ? 其實 Applet 雖然不可以與其他的電腦連線, 卻可以與下載端的Web Server 電腦連線. 藉由這個中央電腦, 我們將可以製作出類似 MSN Messsager 功能的 Applet 軟體 井民全 製作

  2. 簡單的 Server 程式 建立伺服器:五步驟 1. 建立Server專用的socket 2. 等待 client 連線 3. 建立輸出到 client 的串流 4. 建立讀取串流 5. Server 讀取 Client的資料 6. Server 輸出到 Client

  3. import java.net.*; import java.io.*; public class Server { public static void main(String args[]) throws IOException { //1. 建立Server專用的socket ServerSocket serverSocket = new ServerSocket(4444); //2. 等待 client 連線 System.out.println("等待連線中..."); Socket clientSocket = serverSocket.accept(); //3. 建立輸出到 client 的串流 PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); //4. 建立讀取串流 BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream()) ); //5. Server 讀取 Client的資料 String ClientString=in.readLine(); System.out.println(ClientString); //6. Server 輸出到 Client out.println("我相信你會成功的"); } } SingleTalkServer Server.java

  4. 簡單的 client 程式 private void TalkToServer() throws Exception{ String strIP="localhost"; //1. 建立與 Server 連線的socket Socket kkSocket = new Socket(strIP, 4444); //2. 建立輸出/輸入串流 PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); //3. Send 資料到 Server out.println("我會加油的 by Jing"); //4. Client 取得來自 Server 的資料 String ComeFromServer; ComeFromServer=in.readLine(); //5. Client 秀出來自 Server 的回應 System.out.println(ComeFromServer); }

  5. 把 client 程式放到 JApplet 中 public class MyApplet extends JApplet implements ActionListener{ } 剛剛寫的 talktoServer method public void init(){ } public void actionPerformed(ActionEvent e){ talktoServer(); } 完整範例: SingleTalkClientApplet

More Related