JAVA程序設(shè)計(jì):第15章 網(wǎng)絡(luò)編程_第1頁(yè)
JAVA程序設(shè)計(jì):第15章 網(wǎng)絡(luò)編程_第2頁(yè)
JAVA程序設(shè)計(jì):第15章 網(wǎng)絡(luò)編程_第3頁(yè)
JAVA程序設(shè)計(jì):第15章 網(wǎng)絡(luò)編程_第4頁(yè)
JAVA程序設(shè)計(jì):第15章 網(wǎng)絡(luò)編程_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論