1 / 13

網路程式設計 -2

網路程式設計 -2. 1. 簡易網路程式 2. 簡易線上溝通程式 3. 網路應用程式 4.Servlet 程式設計 5. 1. 簡易網路程式. 1.1 顯示本地端電腦網路狀態 建立 InetAddress 物件,取得本地端資訊 InetAddress intel = InetAddress.getLocalHost(); 取得電腦名稱 String name = intel.getHostName(); 取得 IP 位址 String ipe = intel.getHostAddress();. 1. 簡易網路程式. 1.2 查詢連線主機電腦網路狀態

Download Presentation

網路程式設計 -2

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. 網路程式設計-2 1.簡易網路程式 2.簡易線上溝通程式 3.網路應用程式 4.Servlet程式設計5

  2. 1.簡易網路程式 1.1 顯示本地端電腦網路狀態 • 建立InetAddress物件,取得本地端資訊 InetAddress intel = InetAddress.getLocalHost(); • 取得電腦名稱 String name = intel.getHostName(); • 取得IP位址 String ipe = intel.getHostAddress();

  3. 1.簡易網路程式 1.2 查詢連線主機電腦網路狀態 • 建立InetAddress物件,使用 domain name InetAddress intel = InetAddress.getByName(“tw.stock.yahoo.com”); • 建立InetAddress物件,使用 ip位址 InetAddress intel = InetAddress.getByName(“203.67.233.138”); • 取得主機名稱 String name = intel.getHostName(); • 取得IP位址 String ipe = intel.getHostAddress();

  4. 2.簡易線上溝通程式 • Winsock使用的通訊協定(Protocol)有兩種 • TCP: Transmission Control Protocol • 是一種可靠度較高之通訊協定,當與特定電腦溝通時,必須取得該電腦之回應,某則會產生錯誤信息而繼續溝通。使用TCP之類別有URL, URLConnection, Socket和ServerSocket 。 • UDP: User DataGram Protocol • 是一種可靠度較低之通訊協定,當與特定電腦溝通時,並不要求接收端的任何檢查動作,是一種較為簡便之通訊協定。使用UDP之類別有DatagramPacket, DatagramSocket,和MulticastSockett。

  5. 2.1 使用UDP協定建立連線 • 首先分別指定通訊電腦之收信息和傳遞信息之連接阜,例如接收用901,傳送用902。 • 雖然連接阜之編號使用並無特殊規定,但最好避開下列阜號: (FTP: 21, TELNET: 23, SMTP: 25, GOPHER: 70, HTTP: 80, POP3: 110, NNTP: 119, HTTPS: 443)

  6. 2.1 使用UDP協定建立連線類別程式 • Server 端建立一個等待連線物件 • DatagramSocket sk1 = new DatagramSocket(901); • Client端建立一個連線物件,不需指定阜號 • DatagramSocket sk1 = new DatagramSocket(); • Client傳送信息時之物件建立 • DatagramPacket pk1 = new DatagramPacket(buf, buf.length, addr, 901); 其中buf是位元組陣列變數,buf.length是資料長度,addr為Server端地址,阜號使用901。 • Addr可用 InetAddress addr = InetAddress.getByName(servername);取得地址資訊

  7. 2.1 使用UDP協定建立連線類別程式 • Client端之信息傳送使用send()方法 • Sk1.send(pk1); • Server接收信息時之物件建立 • DatagramPacket pk2 = new DatagramPacket(buf, buf.length); • 使用receive()方法接收來自Client端之資料 sk1.receive(pk2); str1 = new String(pk2.getData()); 可用 int port =pk1.getPort(); 取得阜號

  8. 2.1 Server端連線範例程式 c6_02_02a

  9. 2.1 Client端連線範例程式 c6_02_02b

  10. 2.1 使用UDP協定建立連線類別程式 • 注意事項 在使用receive()方法接收資料時,惠一直等待接收信息,若通訊雙方同時都使用此方法,則兩台電腦將一直處於互等狀態,為了避免此Bug發生,Srrver端可先觸發receive(), 接著在觸發send();而Client()則先觸發send(), 接著在觸發receive() 。

  11. 2.2 使用TCP協定建立連線 • Server端建立連線物件(等待連線,必須指定阜號) • ServerSocket sk1 = new ServerSocket(9999); • Socket insk1 = sk1.accept(); 使伺服端進入等候狀態 • Server端建立連線物件(要求連線,必須指定Server端IP和阜號) • ServerSocket sk2 = new ServerSocket(“server_IP”, 9999);

  12. 2.2 使用TCP協定建立連線 • Client端傳送信息,使用PrintWriter建立輸出串流物件 PrintWriter out1 = new PrintWriter(sk2.getOutputStream(), true); • 使用write()和flush()傳送資料 out1.write(str1+”\n”); out1.flush();

  13. 2.2 使用TCP協定建立連線 • Server端接收信息,使用BufferedReader建立輸入串流物件 BufferedReader in1 = new BufferedReader(new InputStreamReader(sk1.get.InputStream())); • 使用readLine()接收資料 String str1 =in1.readLine();

More Related