版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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); /= = 服務器端 = 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); = 輸出結果: -看!我讀取了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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年消防器材智能化改造升級服務合同2篇
- 2024租賃合同簽訂程序及條件
- 2025年拓展訓練合同范本大全:企業(yè)團隊凝聚力提升計劃3篇
- 二零二四年度2024年三人健身產業(yè)合作合同6篇
- 2025年洗車場車輛停放管理及承包合同3篇
- 2025版航空航天專用鋁合金采購合同書4篇
- 二零二四年云服務器租賃與智能運維合同3篇
- 個人汽車租賃合同樣本 2024年版版B版
- 2025年度臨時臨時設施租賃合同標準范本4篇
- 2025年無償使用政府辦公樓場地舉辦會議合同范本3篇
- 非誠不找小品臺詞
- 2024年3月江蘇省考公務員面試題(B類)及參考答案
- 患者信息保密法律法規(guī)解讀
- 老年人護理風險防控PPT
- 充電樁采購安裝投標方案(技術方案)
- 醫(yī)院科室考勤表
- 鍍膜員工述職報告
- 春節(jié)期間化工企業(yè)安全生產注意安全生產
- 保險行業(yè)加強清廉文化建設
- Hive數(shù)據(jù)倉庫技術與應用
- 數(shù)字的秘密生活:最有趣的50個數(shù)學故事
評論
0/150
提交評論