版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第十五章網(wǎng)絡(luò)編程 本章內(nèi)容使用包中的類在程序中實(shí)現(xiàn)網(wǎng)絡(luò)通信URL類及其用法Socket通信URLURL(Uniform Resource Locator)-統(tǒng)一資源定位器,表示Internet上某一資源的地址。URL組成: 協(xié)議名和資源名protocol:resourceNameURL舉例:/http:/home/welcome.htmlhttp:/developers/.URL類常用構(gòu)造方法public URL(String spec);URL u1 = new URL(“/home/”);public URL(URL context, String spec);URL u2 = new U
2、RL(u1, “welcome.html”);public URL(String protocol, String host, String file); URL u3 = new URL(“http”, “”, “developers/index.html” );public URL (String protocol, String host, int port, String file);URL u4 = new URL(“http”, “”, 80, “developers/index.html” );URL類應(yīng)用舉例(1)import java.io.*;import .*;publi
3、c class URLReaderpublic static void main(String args)tryURL tirc = new URL(/);BufferedReader in = new BufferedReader(new InputStreamReader(tirc.openStream();String s;while(s = in.readLine()!=null)System.out.println(s);in.close();catch(MalformedURLException e) System.out.println(e);catch(IOException
4、e)System.out.println(e); URL類應(yīng)用舉例(2)程序URLReader.java輸出結(jié)果:清華大學(xué)網(wǎng)站首頁(yè)Socket兩個(gè)Java應(yīng)用程序可通過一個(gè)雙向的網(wǎng)絡(luò)通信連接實(shí)現(xiàn)數(shù)據(jù)交換,這個(gè)雙向鏈路的一端稱為一個(gè)socket。socket通常用來實(shí)現(xiàn)client-server連接。包中定義的兩個(gè)類Socket和ServerSocket,分別用來實(shí)現(xiàn)雙向連接的client和server端建立連接時(shí)所需的尋址信息遠(yuǎn)程計(jì)算機(jī)的機(jī)器名或IP地址試圖連接的端口號(hào)(Port number)Socket通信模型ServerServerSocket s (port #)s.accept()/
5、等待連接Socket()OutputStreamInputStreamsocket.close()ClientSocket (host, port #)(Attempt to connect)OutputStreamInputStreamsocket.close()網(wǎng)絡(luò)編程的四個(gè)基本步驟創(chuàng)建socket;打開連接到socket的輸入/輸出流;按照一定的協(xié)議對(duì)socket進(jìn)行讀/寫操作;關(guān)閉socket;創(chuàng)建socketSocket/ServerSocket類的構(gòu)造方法Socket(InetAddress address, int port);Socket(InetAddress address
6、, int port, boolean stream);Socket(String host, int port);Socket(String host, int port, boolean stream);ServerSocket(int port);ServerSocket(int port, int count);客戶端Socket的建立try Socket socket=new Socket(”,2000);catch(IOException e) System.out.println(Error:+e);服務(wù)器端Socket的建立ServerSocket server=null;tr
7、y server=new ServerSocket(2000);catch(IOException e) System.out.println(can not listen to :+e);Socket socket=null;try socket=server.accept();catch(IOException e) System.out.println(Error:+e);打開輸入/出流PrintStream os=new PrintStream(new BufferedOutputStream(socket.getOutputStream();DataInputStream is=ne
8、w DataInputStream(socket.getInputStream();關(guān)閉Socketos.close();is.close();socket.close();簡(jiǎn)單的client/server程序import .*;import java.io.*;public class TestServer public static void main(String args) ServerSocket s = null;try s = new ServerSocket(8888); catch (IOException e) while (true) try Socket s1 = s.
9、accept();OutputStream os = s1.getOutputStream();DataOutputStream dos = new DataOutputStream(os);dos.writeUTF(Hello,bye-bye!);dos.close();s1.close(); catch (IOException e) 簡(jiǎn)單的client/server程序import .*;import java.io.*;public class TestClient public static void main(String args) try Socket s1 = new Socket(, 8888);InputStream is = s1.getInputStream();DataInputStream dis = new DataInputStream(is);System.out.println(dis.readUTF
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 44690-2024人造板及其制品揮發(fā)性有機(jī)化合物釋放量分級(jí)
- 2024-2025學(xué)年小學(xué)科學(xué)一年級(jí)上冊(cè)(2024)人教鄂教版(2024)教學(xué)設(shè)計(jì)合集
- 2024-2025學(xué)年初中信息技術(shù)(信息科技)九年級(jí)上冊(cè)粵教清華版教學(xué)設(shè)計(jì)合集
- 四 長(zhǎng)方體(二)(教學(xué)設(shè)計(jì))2023-2024學(xué)年五年級(jí)下冊(cè)數(shù)學(xué)北師大版
- 2024年灶具油煙機(jī)項(xiàng)目投資申請(qǐng)報(bào)告代可行性研究報(bào)告
- 5《人應(yīng)當(dāng)堅(jiān)持正義》教案統(tǒng)編版選擇性必修中冊(cè)
- 黑龍江省哈爾濱市2025年高三入學(xué)調(diào)研化學(xué)試題(4)試卷含解析
- 遼師大版七年級(jí)上冊(cè)信息技術(shù)教案 2.3注重細(xì)節(jié)精加工-編輯單元格
- 華中師大版七下信息技術(shù) 2.5熱鬧的運(yùn)動(dòng)會(huì)-圖片的插入 教案
- 人教版三上信息科技 第8課 簡(jiǎn)簡(jiǎn)單單畫幅畫 教學(xué)設(shè)計(jì)
- 《醫(yī)療廢物分類目錄》(2021年版)試題+答案
- 廉政合同完整參考
- 抽油機(jī)曲柄軸扭矩及電機(jī)功率計(jì)算課件
- 高三畢業(yè)綜評(píng)研究性學(xué)習(xí)及創(chuàng)新成果
- 2 化學(xué)熱力學(xué)基礎(chǔ)
- 社會(huì)學(xué)理論專題風(fēng)險(xiǎn)社會(huì)理論
- 環(huán)氧樹脂結(jié) 構(gòu) 與 性 能
- 初中英語(yǔ)單詞大全分類(帶音標(biāo)完美打印版)
- 貯水花盆案例總結(jié)-2015天津中心修改
- 內(nèi)陸漁業(yè)船舶證書申請(qǐng)和年審業(yè)務(wù)審批表
- 新人教版二年級(jí)數(shù)學(xué)上冊(cè)第一單元解決問題
評(píng)論
0/150
提交評(píng)論