




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、基于linux下的socket通信開發(fā)平臺:linux開發(fā)語言:java開發(fā)工具:eclispe開發(fā)人員:闞廣穩(wěn)(安徽理工大學計算機學院09-2班)i. 系統(tǒng)描述:本系統(tǒng)含有一個服務器(server.class)和多個客戶端(clinet.class),可以通過每 個客戶端查看和下載服務器端共享文件夾中的文件。ii. 功能描述:a. 查看服務器端共享文件夾列表操作:在 linux 終端下輸入 java clinet listfileso 參數(shù)說明:listfiles是固定參數(shù)。 結果:列出所有共享文件。b. 下載服務器端共享文件夾屮的文件操作:在 linux 終端下輸入 java clinet
2、 download filename dirpatho 參數(shù)說明:download是固定參數(shù),filename是想要下載的文件名,dirpath是 下載文件保存的路徑。結果:下載文件filename到地址dirpathoiii. 功能分析以及實現(xiàn):a.問題描述:如何創(chuàng)建可以用于多個客戶端連接的服務器?分析解決:因為java語言提供了對多線程的支持,所以我們可以把服務器 設計為多線程的,對于每個客戶端的連接單獨開-條線程與之交 互。主要實現(xiàn)代碼:服務器端:serversocket serversocket=new serversocket(5678);socket socket;while (t
3、rue_socket=serversocketaccept ();new serverthread(socket) .start ();class serverthread extends threadsocket socket;public serverthread(socket socket)this.socket=socket;b問題描述:如何把服務器端的文件列表發(fā)送到客戶端分析解決:服務器端如果取得一個文件名就發(fā)給客戶端也是可行的,但當文 件較多的時顯得不清晰,如果可以把所有文件名組成的文件列表保存起來, 再統(tǒng)一發(fā)送到客戶端,客戶端再統(tǒng)一解析文件列表就顯得更合理。這其中也 體現(xiàn)了軟件設
4、計過程中封裝的思想。幸運的是強大的網(wǎng)絡編程語言java就 可以很好的解決這一問題。遍歷服務器端的共享文件夾,把所有文件的文件 名以及它們的存儲地址存放到map中,在通過對象流的方式發(fā)送到客戶端, 客戶端解析對象流就可以獲取文件列表了。主耍實現(xiàn)代碼:服務器端:public static map<string, string> getallfiles (file file, map<string, string> map) if (fileisdirectory ()file files = file listfiles ();for (int i = 0; i <
5、files.length; i+)getaufiles (files i r map); else map.put(file.getname(), file.getpath();return map;/把服務器端的文件列表傳到客戶端objectoutputsobjectoutputstream=newobjectoutputstream(outputstream);objectoutputstream.writeobject(map);objectoutputstream.close();c.問題描述:如何下載服務器端的文件分析解決:由于服務器端已經(jīng)把共享文件及地址的目錄保存在map中,所以
6、只要檢查用戶輸入的文件名(filename)是否存在map中就可以得知所下載的 文件是否存在,如果存在就可以通過map獲得該文件的存儲地址,服務器再 找到該文件,把它發(fā)向客戶端,客戶端就可以把該文件存放在指定的目錄 (dirpath) o 主要實現(xiàn)代碼:服務器端:if(map.containskey(filename)/把client請求的文件發(fā)過去file file2=new file(map.get(filename);filelnputstream fi1einputstream=newfileinputstream(file2);int buf=0;while(buf=fileinpu
7、tstream.read()!= -1)outputstream.write(buf);sys tem. out .pri nt in (,!transfer success ! ,!); filelnputstream.close ();outputstream.close();inputstream.close();else/告訴client端文件不存在output stream, write (,ferror11 getby tes (); outputstream.close ();客戶端:fileoutputstream fileoutputstream=newfileoutputs
8、tream(new file(dirpath);int buf=0;/server端的寫停止了(既關閉輸出流),client端的讀才能停止,否 則一直等待讀取server發(fā)來的數(shù)據(jù)while(buf=stream.read()!= -1)fileoutputstream.write(buf);iv. 測試需對java clinet后面的參數(shù)進行詳細的檢查可能錯誤:1.參數(shù)個數(shù)不正確2 客戶端文件存放地址不合法或不存在3 要求的固定參數(shù)不正確 檢測代碼如下:if (args .丄qngth=l && args 0 equals ("listf iles'*) )
9、 /.elseif (args.length!=3 i i !args0 .equals(ndownloadn)i i !new file (args2) .exists () system. out .printin (,ferror parameters ! n );system.out.printin("the right parameters are like1 download filename destdir 1 or 1 listfiles 1 ,f); system.exit(-1);v. 完整代碼如下:serverjavaimport java.io.file;im
10、port java.io.fileinputstream;import javaioioexception;import javaioinputstream;import javaioobjectoutputstream;import javaio.outputstream;import .serversocket;import javanetsocket;import javautilhashmap;import java.util.map;public class server public static void main(string args) serversocket server
11、socket;socket socket;try serversocket = new serversocket (5678); while (true) socket = serversocketaccept();system. out .pri(,fconnected fromn+socketgetremotesocketaddress()tostring(); new serverthread(socket) start (); catch (ioexception e) e.printstacktrace ();class serverthread extends thread soc
12、ket socket;outputstream outputstream;inputstream inputstream;public serverthread(socket socket) thissocket = socket;try outputstream = socket.getoutputstream (); inputstream=socketgetlnputstream(); catch (ioexception e) e.printstacktrace ();public void run ()/服務器共亨文件存放的地址file file = new file (共亨文件夾路
13、徑);map<string, string> map=new hashmap<string, string>(); raap=getahfiles (file, map);byte info=new byte8192;try inputstream.read(info);string filename=new string(info)trim();if (f ilename . equals ('* 1 istf iles”) /把服務器端的文件列表傳到客戶端objectoutputstream objectoutputstream=newobjectoutpu
14、tstream(outputstream);objectoutputstream.writeobject(map);objectoutputstream< close();elseif(map.containskey(filename)/把client請求的文件發(fā)過去file file2=new file(map.get(filename);fileinputstream fi1einputstream=newfileinputstream(file2);int buf=0;while(buf=filelnputstream.read()!= 一1)outputstream.write(
15、buf);system, out .printin (,ftransfer success ! 11); fileinputstream.close ();outputstream.close(); inputstream.close();else/告訴client端文件不存在out put st ream, write ( nerror11. getbytes (); outputstream.close (); catch (ioexception e) eprintstacktrace ();public statiu mapcstring, string> getaufiles
16、(file file map<stringa string> map) if (file.isdirectory() file files = file.listfiles ();for (int i = 0; i < files.length; i+)getaufiles (files i , map); else map<put(file.getn且me(), file.getpth ();return map;client javaimport javaiofile;import java io . fileoutputstrsm;import javaioioe
17、xception;import javaioinputstream;import java.io.objectinputstream;import javaiooutputstreem;import javanetsocket;import javanetunknownhostexception;import java.util.map;import java.util.map.entry;public class client public static void main(string args) throws unknownhostexception, ioexception, clas
18、snotfoundexception socket socket;/獲得服務器端的文件列表if (args length=l && args 0 equals (11 list files n ) ) socket=new socket ( nlocalhost11 z 5678);outputstream outputstream=socket.getoutputstream();byte info=args0 .getbytes ();outputstream.write(info);objectinputstream objectinputstream=newobject
19、lnputstream(socketgetlnputstream();suppresswarningsunchecked”)map<string, string> map=(map<string, string>)objectinputstreem.readobject();system, out .printin ("服務器端文件列表如下:;for(entry<string, string> entry:mapentryset()sys tem. out. prin t j_n (en try get key ();elseif (args.length!=3 | |!args0 .equals("download”) | |!newfile (args2) .exists() system.out.printin("error parameters!n);system.out.printinthe right parameters are like1 download filename destdir' or 1listfiles1n);system.exit(-1);socket=new socket (uocalhost j 5678
溫馨提示
- 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 五年級下冊數(shù)學教案-4.1 容積|北師大版
- 2025年機動車典當合同
- 六年級上冊數(shù)學教案-總復習 你會算嗎-求陰影部分的面積|北師大版
- 2025年科研合作協(xié)議
- 第四單元驕人祖先 燦爛文化整體教學設計-2024-2025學年道德與法治五年級上冊統(tǒng)編版
- Unit 3 I'm more outgoing than my sister. SectionB 1a-1e教學設計2024-2025學年人教版英語八年級上冊
- 2025年湖北三峽職業(yè)技術學院單招職業(yè)適應性測試題庫匯編
- 2024年全腦開發(fā)項目資金申請報告代可行性研究報告
- 2025年嘉興南洋職業(yè)技術學院單招職業(yè)技能測試題庫完整
- 2025年共青科技職業(yè)學院單招職業(yè)適應性測試題庫完整版
- 校園食品安全和膳食經(jīng)費管理方案3篇
- TSGD7002-2023-壓力管道元件型式試驗規(guī)則
- 九年級化學下冊 第12單元 化學與生活教案 (新版)新人教版
- 金融服務消費糾紛調(diào)解工作規(guī)范
- 后腹腔鏡下輸尿管切開取石術
- 二手車購買收據(jù)合同范本
- 《國際貿(mào)易實務(英文版)》(英文課件) - Ch 1 Introduction to International Trade Practices-Ch 5 Price
- 2022版義務教育英語課程標準整體解讀課件
- 2024精美復工復產(chǎn)安全培訓
- 01 H5入門知識課件
- 2024年企業(yè)管理咨詢服務費簡單合同范本
評論
0/150
提交評論