![《Java基本網(wǎng)絡類》課件_第1頁](http://file4.renrendoc.com/view/29afe3a2a59943f425b260d0ce9d3583/29afe3a2a59943f425b260d0ce9d35831.gif)
![《Java基本網(wǎng)絡類》課件_第2頁](http://file4.renrendoc.com/view/29afe3a2a59943f425b260d0ce9d3583/29afe3a2a59943f425b260d0ce9d35832.gif)
![《Java基本網(wǎng)絡類》課件_第3頁](http://file4.renrendoc.com/view/29afe3a2a59943f425b260d0ce9d3583/29afe3a2a59943f425b260d0ce9d35833.gif)
![《Java基本網(wǎng)絡類》課件_第4頁](http://file4.renrendoc.com/view/29afe3a2a59943f425b260d0ce9d3583/29afe3a2a59943f425b260d0ce9d35834.gif)
![《Java基本網(wǎng)絡類》課件_第5頁](http://file4.renrendoc.com/view/29afe3a2a59943f425b260d0ce9d3583/29afe3a2a59943f425b260d0ce9d35835.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、Advanced Network ProgrammingRen JiansirenjsvAdvanced Network ProgrammingReNetworking BasicsApplications LayerStandard appsHTTPFTPTelnetUser appsTransport LayerTCPUDPProgramming Interface:SocketsNetwork LayerIPLink LayerDevice driversTCP/IP StackApplication(http,ftp,telnet,)Transport(TCP, UDP,.)Netwo
2、rk(IP,.)Link(device driver,.)Networking BasicsApplications Networking BasicsTCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.Example applications:HTTPFTPTelnetTCP/IP StackApplication(http,ftp,telnet,)Transport(TCP, UDP,.)N
3、etwork(IP,.)Link(device driver,.)Networking BasicsTCP (TransporNetworking BasicsUDP (User Datagram Protocol) is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. Example applications:Clock serverPingTCP/IP StackApplica
4、tion(http,ftp,telnet,)Transport(TCP, UDP,.)Network(IP,.)Link(device driver,.)Networking BasicsUDP (User DatJava基本網(wǎng)絡類本章介紹了Java基本的網(wǎng)絡類(InetAddress、URL、URLConnection和Socket)的使用,介紹如何利用Socket通信機制實現(xiàn)客戶/服務器方式的一對一的通信,實現(xiàn)瀏覽器/服務器方式的網(wǎng)絡聊天。Java基本網(wǎng)絡類本章介紹了Java基本的網(wǎng)絡類(InetA1 Java網(wǎng)絡進程通信概述 網(wǎng)絡編程一般指利用不同層次的通信協(xié)議提供的接口實現(xiàn)網(wǎng)絡進程安全
5、通信和應用的編程。 網(wǎng)絡進程就是網(wǎng)點機(連入網(wǎng)絡的計算機)上運行的程序。網(wǎng)絡進程在通信協(xié)議中用端口標識(port),它駐留的網(wǎng)點機用其IP地址或域名來標識。 網(wǎng)絡進程同步通信是指通信的發(fā)起者向接收者發(fā)出服務請求后,必須得到對方回應,才繼續(xù)運行。如果通信的發(fā)起者向接收者發(fā)出服務請求后,可以繼續(xù)運行,需要時再獲取接收者的響應,這叫異步通信。1 Java網(wǎng)絡進程通信概述 網(wǎng)絡協(xié)議工程就是利用軟件工程的方法(分層次、分模塊等技術)來解決復雜網(wǎng)絡進程通信的問題。網(wǎng)絡進程往往按照應用的需要,采用不同層次的協(xié)議進行通信。2 Java網(wǎng)絡基本類2.1 InetAddress類InetAddress類用來描述I
6、nternet地址。1.獲取本機的IP地址例1 用InetAddress類獲取本機的IP地址。import .*;import java.io.*; 網(wǎng)絡協(xié)議工程就是利用軟件工程的方法(分層public class GetLocalHost public static void main(String arg) InetAddress myIp=null; try myIp=InetAddress.getLocalHost(); catch (UnknownHostException e) System.out.println(myIp); 2.根據(jù)域名獲得IP地址例2 根據(jù)域名自動到DNS上
7、查找IP地址。public class GetLocalHost import .*;import java.io.*;public class GetIP public static void main(String args) InetAddress cug=null; try cug=InetAddress.getByName(); catch (UnknownHostException e) System.out.println(cug); import .*;getByName()public static InetAddress getByName(String host) thr
8、ows UnknownHostExceptionInetAddress utopia, duke;try utopia = InetAddress.getByName(); duke = InetAddress.getByName(2);catch (UnknownHostException e) System.err.println(e); getByName()public static InetA2.2 URL類 URL(Uniform Resource Locator)是WWW資源統(tǒng)一資源定位器的簡寫,它規(guī)范了WWW資源網(wǎng)絡定位地址的表示方法。 WWW資源包括Web頁、文本文件、圖形文
9、件、聲頻片段等。1. URL類簡介 URL類定義了WWW資源的特征及讀其內(nèi)容的方法。其基本表示格式: protocol:/hostname:/resourcename#anchor 其中, protocol:使用的協(xié)議,它可以是http,ftp,news,telnet等;2.2 URL類 hostname:主機名,指定域名服務器(DNS)能訪問到的WWW服務的計算機,如:; port:端口號,是可選的,表示所連的端口號,如默認,將連接到協(xié)議默認的端口。 resourcename:資源名,是主機上能訪問到的目錄或文件; anchor:標記,也是可選的,它指定文件內(nèi)的有特定標記的位置。 URL例子
10、: (1)/demoweb/url-primer.htm hostname:主機名,指定域名服務器(DNS)能訪問到 (2):8080/demoweb/url-primer.htm (3)ftp:/local/demo/information#myinfo (4)file:/local/demo/readme.txt 第(2)個URL把標準Web服務器端口80指定為8080端口,第(3)個URL加上符號“#”,用于指定在文件information中標記為myinfo的部分。2. URL類對象 URL類的構造方法有如下四種。(1)URL(URL absoluteURL)例: URL myUni=
11、URL(“/”) (2)(2)URL(URL url ,String relativeURL)例: URL myUni=URL(“/”); URL mydoc=URL(myUni,”mydoc.html”)(3) URL(String protocol,String host,String resourcename )例: URL myUni=URL(“http”,”,”/mydoc.html”); 等價于:URL myUni=URL(“http:/mydoc.html”);(4) URL(String protocol,String host,int port,String resource
12、name ) (2)URL(URL url ,String relativ例: URL myUni=URL(“http”,”,80,”/mydoc.html”); 等價于: URL myUni=URL(“http:/:80/mydoc.html”);3. URL類獲取其特征的主要方法 String getProtocol();返回URL的協(xié)議名。 String getHost();返回URL的主機名。 Int getPort();返回URL的端口號, 如果沒有設置端口號返回值為1)。 String getFile();返回URL的文件名及路徑。 String getRef();返回URL的標記
13、。例: URL myUni=URL4. 用URL獲取網(wǎng)上HTML文件 分為三步:(1)構造URL的對象: url=new URL(“”);(2) 將DataInputStream類對象與url的 openStrean()流對象綁定: DataInputStream din= new DataInputStream(url.openStrean()(3)利用din 讀HTML文件。例3 用URL獲取網(wǎng)絡上資源的HTML文件:4. 用URL獲取網(wǎng)上HTML文件例3 用URL獲取網(wǎng)絡import .*;import java.io.*;public class ReadURL static publ
14、ic void main(String args) try URL url=new URL(args0);DataInputStream din= new DataInputStream(url.openStream(); String inputLine;while(inputLine=din.readLine()!=null) import .*;System.out.println(inputLine);din.close();catch(MalformedURLException me)catch(IOException ioe)運行下列dos命令:java ReadURL 就可以看到
15、中國地質大學主頁的HTML文件。System.out.println(inputLine);5. 用URL獲取文本和圖像 文本數(shù)據(jù)源可以是網(wǎng)上或者本機上的任何文本文件。如果要用URL來獲取圖像數(shù)據(jù),要使用方法getImage(URL)。這個方法會立即生成一個Image對象,并返回程序對象的引用,但這并不意味著圖像文件的數(shù)據(jù)已經(jīng)讀到了內(nèi)存之中,而是系統(tǒng)與此同時產(chǎn)生一個線程去讀取圖像文件的數(shù)據(jù)。因此,就可能存在程序已經(jīng)執(zhí)行到了getImage()后面的語句部分,而系統(tǒng)還正在讀圖像文件數(shù)據(jù)的情形。 例4 用URL來獲取文本文件(.txt)和圖像文件(.jpeg,.gif)。5. 用URL獲取文本和圖
16、像import .*;import java.awt.*;import java.io.*;public class GetDataByURL extends FrameMenuBar menuBar;boolean drawImage=false;DataInputStream dataInputStream;int i=0;String line_str;boolean first=true;Font font;import .*;public GetDataByURL () menuBar=new MenuBar();setMenuBar(menuBar);Menu display=ne
17、w Menu(display);menuBar.add(display);MenuItem beauty_display= new MenuItem(display beauty);MenuItem text_display= new MenuItem(display text);display.add(beauty_display);display.add(text_display);setBackground(Color.white);public GetDataByURL () font=new Font(System,Font.BOLD,20);setTitle(sample:use
18、URL get data);resize(400,300);show();public boolean action(Event evt,Object what) if(evt.target instanceof MenuItem) String message=(String)what; if(message=display beauty)drawImage=true; doDrawImage(); font=new Font(System,Font.BOelse drawImage=false; first=true; if(message=display text) doWrite(fi
19、le:/d:/plbackup/tt.txt); return true;else public boolean handleEvent(Event evt) switch(evt.id)case Event.WINDOW_DESTROY: dispose(); System.exit(0); default: return super.handleEvent(evt);static public void main(String args) new GetDataByURL();public boolean handleEvent(Evepublic void paint(Graphics
20、g) if(drawImage) try URL image_URL= new URL(file:/D:/plbackup/zy4.jpeg); Toolkit object_Toolkit= Toolkit.getDefaultToolkit(); Image object_Image= object_Toolkit.getImage(image_URL); g.setColor(Color.white); g.fillRect(0,0,300,400);public void paint(Graphics g) g.drawImage(object_Image,40,80,160,200,
21、this); catch(MalformedURLException e) else if(first)first=false;g.setColor(Color.white);g.fillRect(0,0,400,300);g.drawImage(object_Image,40,80g.setFont(font);if(line_str!=null) g.drawString(line_str,10,i*20);i+;private void doDrawImage() drawImage=true;repaint();g.setFont(font);private void doWrite(
22、String url_str) try URL url=new URL(url_str); dataInputStream= new DataInputStream(url.openStream(); tryi=1;line_str=dataInputStream.readLine();while(line_str!=null) private void doWrite(String ur paint(getGraphics();line_str=dataInputStream.readLine();catch(IOException e)dataInputStream.close();cat
23、ch(MalformedURLException el)catch(IOException e2) paint(getGraphics();2.3 URLConnection()類 URL類僅提供讀取地址為URL的Web服務器內(nèi)容的方法。如果不僅要讀取其內(nèi)容,而且要向URL對象發(fā)送服務請求及參數(shù),那么必須要使用URLConnection()類。利用URL類提供的openConnection()方法,可以建立一個URLConnection類對象。然后,可以使用這個URLConnection類對象綁定的數(shù)據(jù)輸入流讀URL的內(nèi)容,使用這個URLConnection類對象綁定的數(shù)據(jù)輸出流發(fā)送服務請求及
24、參數(shù)。 URLConnection類是一個以http為中心的類,同時支持get和post兩種方法,默認情況下為post 方法。 URL和URLConnection的區(qū)別在于前者代表一個資源的位置,后者代表一種連接。2.3 URLConnection()類1. 讀取URL對象內(nèi)容例5 利用URLConnection類獲取網(wǎng)絡上的HTML文件。import .*;import java.io.*;public class ReadURLConnection static public void main(String args) try URL cumtURL= new URL(/);1. 讀取U
25、RL對象內(nèi)容URLConnection cumtConnection= cumtURL.openConnection();DataInputStream din=New DataInputStream (cumtConnection .getInputStream(); String inputLine;while(inputLine=din.readLine()!=null) System.out.println(inputLine); din.close();catch(MalformedURLException me)catch(IOException ioe)URLConnection
26、 cumtConnection=2. 與URL對象交互 利用URLConnection類對象向URL對象發(fā)送服務請求及參數(shù),這里以Java程序與服務器端的CGI(Commen Gateway Interface,公共網(wǎng)關)交互為例,具體實現(xiàn)步驟如下。(1)創(chuàng)建URL(包括CGI文件名)對象。(2)打開一個到該URL的連接,建立相應的URLConnection對象。(3)從URLConnection對象獲取其綁定的輸出流。這個輸出流就是連接到服務器上CGI的標準輸入流(stdin)。(4)向這個輸出流中寫數(shù)據(jù),并關閉這個輸出流.2. 與URL對象交互(5)從URLConnection對象獲取其綁
27、定的輸入流。這個輸入流就是連接到服務器上CGI的標準輸出流(stdout);從這個輸入流中讀服務結果。關閉這個輸入流。例6 通過URLConnection對象運行上一個叫backwards的CGI,backwards將其反轉后寫到標準輸出。這個CGI需要下面這種格式的輸入string=string_to_reverse,這里string_to_reverse參數(shù)就是要反轉的字符串。CGI backwards 可執(zhí)行文件從標準輸入讀取字符串,將其轉換成大寫字符串寫到標準輸出。這種標準輸入格式為:“string= string_to_reverse”,這里string_to_reverse 參數(shù)就是要轉換成大寫的字符串,如圖2所示。(5)從URLConnection對
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國花魚數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國手板式啟閉機數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國包裝管理軟件數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國助航燈光全自動監(jiān)控系統(tǒng)數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國全扇出射頻路由矩陣系統(tǒng)數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國PVC-U帶口彎頭數(shù)據(jù)監(jiān)測研究報告
- 家禽飼養(yǎng)業(yè)信息化管理與數(shù)據(jù)挖掘考核試卷
- 2025-2030年呼吸訓練輔助機械行業(yè)跨境出海戰(zhàn)略研究報告
- 2025-2030年微生物資源數(shù)據(jù)庫行業(yè)跨境出海戰(zhàn)略研究報告
- 2025-2030年可擴展式硬盤籠設計行業(yè)跨境出海戰(zhàn)略研究報告
- 高考地理一輪復習學案+區(qū)域地理填圖+亞洲
- 全新車位轉讓協(xié)議模板下載(2024版)
- 高中數(shù)學必修一試卷及答案
- 砌筑工考試卷及答案
- 呼吸治療師進修匯報
- 智慧港口和自動化集裝箱碼頭
- 2024年江西電力職業(yè)技術學院單招職業(yè)技能測試題庫及答案解析
- 天合儲能:2024儲能專用電芯白皮書
- 2024年度醫(yī)患溝通課件
- 【真題】2023年常州市中考道德與法治試卷(含答案解析)
- 光伏項目安全培訓課件
評論
0/150
提交評論