




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、 = import java.io.*; import .*; public class RemoteFileClient protected String hostIp; protected int hostPort; protected BufferedReader socketReader; protected PrintWriter socketWriter; public static void main(String args) RemoteFileClient remoteFileClient = new RemoteFileClient(127.0.0.1, 3000); re
2、moteFileClient.setUpConnection(); String fileContents =remoteFileClient.getFile(E:RemoteFile.txt); remoteFileClient.tearDownConnection(); System.out.println(fileContents); /= public RemoteFileClient(String aHostIp, int aHostPort) hostIp = aHostIp; hostPort = aHostPort; /= public void setUpConnection
3、() try Socket client = new Socket(hostIp, hostPort); socketReader = new BufferedReader( new InputStreamReader(client.getInputStream(); socketWriter = new PrintWriter(client.getOutputStream(); catch (UnknownHostException e) System.out.println(Error setting up socket connection: unknown host at + host
4、Ip + : + hostPort); catch (IOException e) System.out.println(Error setting up socket connection: + e); /= public String getFile(String fileNameToGet) StringBuffer fileLines = new StringBuffer(); try socketWriter.println(fileNameToGet); socketWriter.flush(); String line = null; while (line = socketRe
5、ader.readLine() != null) fileLines.append(line + n); catch (IOException e) System.out.println(Error reading from file: + fileNameToGet); return fileLines.toString(); /= public void tearDownConnection() try socketWriter.close(); socketReader.close(); catch (IOException e) System.out.println(Error tea
6、ring down socket connection: + e); /= = 服務(wù)器端 = import java.io.*; import .*; public class RemoteFileServer protected int listenPort = 3000; public static void main(String args) RemoteFileServer server = new RemoteFileServer(); server.acceptConnections(); public void acceptConnections() try ServerSock
7、et server = new ServerSocket(listenPort); Socket incomingConnection = null; while (true) incomingConnection = server.accept(); handleConnection(incomingConnection); catch (BindException e) System.out.println(Unable to bind to port + listenPort); catch (IOException e) System.out.println(Unable to ins
8、tantiate a ServerSocket on port: + listenPort); public void handleConnection(Socket incomingConnection) try OutputStream outputToSocket = incomingConnection.getOutputStream(); InputStream inputFromSocket = incomingConnection.getInputStream(); BufferedReader streamReader = new BufferedReader(new Inpu
9、tStreamReader(inputFromSocket); FileReader fileReader = new FileReader(new File(streamReader.readLine(); BufferedReader bufferedFileReader = new BufferedReader(fileReader); PrintWriter streamWriter = new PrintWriter(incomingConnection.getOutputStream(); String line = null; while (line = bufferedFile
10、Reader.readLine() != null) streamWriter.println(line); fileReader.close(); streamWriter.close(); streamReader.close(); catch (Exception e) System.out.println(Error handling a client: + e); = 輸出結(jié)果: -看!我讀取了E:RemoteFile.txt??! = Wrapping up the server Before we move on to another, more practical example
11、, lets review the steps to create and use a ServerSocket: 1. Instantiate a ServerSocket with a port on which you want it to listen for incoming client connections (throws an Exception if theres a problem). 2. Call accept() on the ServerSocket to block while waiting for connection. 3. Get the streams
12、 on that underlying Socket for reading and writing. 4. Wrap the streams as necessary to simplify your life. 5. Read from and write to the Socket. 6. Close your open streams (and remember, never close your Reader before your Writer). You can find the complete code listing for RemoteFileServer at Code
13、 listing for RemoteFileServer on page 33. = Wrapping up the server Before we move on to another, more practical example, lets review the steps to create and use a ServerSocket: 1. Instantiate a ServerSocket with a port on which you want it to listen for incoming client connections (throws an Exception if theres a problem). 2. Call accept() on the ServerSocket to block while waiting for connection. 3. Get the streams on that underlying So
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 大客戶銷售崗位面試問題及答案
- 福建省泉港市泉港一中2025年高一化學(xué)第二學(xué)期期末質(zhì)量檢測模擬試題含解析
- 2025屆河北省石家莊市藁城第五中學(xué)高二下化學(xué)期末經(jīng)典試題含解析
- 吉林省白城市通榆縣一中2025年高二化學(xué)第二學(xué)期期末檢測試題含解析
- 2025屆北京專家化學(xué)高二下期末學(xué)業(yè)水平測試模擬試題含解析
- 四川省雅安市2025屆化學(xué)高一下期末聯(lián)考模擬試題含解析
- 醫(yī)用設(shè)備銷售管理辦法
- 華為營運資金管理辦法
- 辦公區(qū)域門禁管理辦法
- 村級電站確權(quán)管理辦法
- 七八年級的英語單詞
- 舞臺使用合同范例
- 2024年面向社會公開招聘警務(wù)輔助人員報名信息表
- 《地區(qū)智能電網(wǎng)調(diào)度技術(shù)支持系統(tǒng)應(yīng)用功能規(guī)范》
- 2024中國類風(fēng)濕關(guān)節(jié)炎診療指南
- 11294營銷管理-國家開放大學(xué)2023年1月至7月期末考試真題及答案(共2套)
- 12-重點幾何模型-手拉手模型-專題訓(xùn)練
- 2024年河北省張家口市“三支一扶”招聘130人(高頻重點復(fù)習(xí)提升訓(xùn)練)共500題附帶答案詳解
- 2023年遂寧市船山區(qū)選調(diào)教師考試真題
- CJJT259-2016 城鎮(zhèn)燃?xì)庾詣踊到y(tǒng)技術(shù)規(guī)范
- 合伙人散伙分家協(xié)議書范文
評論
0/150
提交評論