1 / 15

Android Socket 網路通訊

Android Socket 網路通訊. 鄧姚文 http://www.ywdeng.idv.tw. TCP/IP 網路. Application (http, ftp, telnet,…). 應用. Transport (TCP, UDP,…). 傳輸. Network (IP,…). 網路. Link (Device Driver,…). 鏈結. TCP. Transmission Control Protocol 兩個應用程式之間建立一個通道 , 互相傳送訊息 一部主機可以同時執行許多應用程式 以通訊埠( PORT )區別

novia
Download Presentation

Android Socket 網路通訊

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. Android Socket網路通訊 鄧姚文 http://www.ywdeng.idv.tw

  2. TCP/IP 網路 Application (http, ftp, telnet,…) 應用 Transport (TCP, UDP,…) 傳輸 Network (IP,…) 網路 Link (Device Driver,…) 鏈結

  3. TCP • Transmission Control Protocol • 兩個應用程式之間建立一個通道,互相傳送訊息 • 一部主機可以同時執行許多應用程式 • 以通訊埠(PORT)區別 • Connection-Oriented • 3-Way Handshake(三方交握) • 保證送達、保證封包順序 • Sliding Windows • Flow Control

  4. UDP User Datagram Protocol Connection-Less 傳送分別獨立的封包資料,不保證資料是否送達,也不保證封包順序 速度快!

  5. 通訊埠(PORT) • The latest IANA port assignments can be gotten from • http://www.iana.org/assignments/port-numbers • The Well Known Ports are those from 0 through 1023. • The Registered Ports are those from 1024 through 49151 • The Dynamic and/or Private Ports are those from 49152 through 65535

  6. 常見通訊埠(Well-Known Ports) ftp 21/tcp ssh22/tcp telnet 23/tcp smtp25/tcp domain 53/tcp http 80/tcp pop3 110/tcp ntp 123/udp

  7. Java 提供的程式庫 • java.net • TCP • URL, URLConnection, Socket, and ServerSocket • UDP • DatagramPacket, DatagramSocket, and MulticastSocket

  8. 什麼是URL? • Uniform Resource Locator • 唯一識別網路上的資源 • 例如: • http://java.sun.com • http:通訊協定 • java.sun.com資源名稱

  9. 建立URL 以字串表示URL http://www.cust.edu.tw URL cust = new URL("http://www.cust.edu.tw"); 可以使用絕對路徑也可以使用相對路徑

  10. URL連線過程 try { URL cust = new URL("http://www.cust.edu.tw/"); URLConnectioncustConn= cust.openConnection(); } catch (MalformedURLException e) { // new URL() failed . . . . } catch (IOException e) { // openConnection() failed . . . . }

  11. socket URL 和 URLConnection提供高階的存取機制 Socket 提供低階的存取機制

  12. Socket 連線方式 • Server 在特定 Port 上設定一個 Socket 等待 Client 連線 • java.net.ServerSocket • Client 必須知道 Server 的主機名稱(或IP)以及 Server 提供服務的 Port • Client 建立 Socket 連接到 Server • java.net.Socket

  13. 在 Android 裡使用 Socket <uses-permission android:name="android.permission.INTERNET" /> 必須在 AndroidManifest.xml 之中加入

  14. 範例:多人聊天室 Server 程式以一般的 JAVA Application 撰寫 Client 程式以 Android 應用程式撰寫 Server 接受 Client 的連線,建立 Client 清單 Client 將訊息送給 Server,Server 依據 Client 清單將訊息轉送給每一個 Client

  15. 網路架構

More Related