基于Linux下的Socket通信(操作系統(tǒng)課程設(shè)計(jì))_第1頁
基于Linux下的Socket通信(操作系統(tǒng)課程設(shè)計(jì))_第2頁
基于Linux下的Socket通信(操作系統(tǒng)課程設(shè)計(jì))_第3頁
基于Linux下的Socket通信(操作系統(tǒng)課程設(shè)計(jì))_第4頁
基于Linux下的Socket通信(操作系統(tǒng)課程設(shè)計(jì))_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、基于linux下的socket通信開發(fā)平臺(tái):linux開發(fā)語言:java開發(fā)工具:eclispe開發(fā)人員:闞廣穩(wěn)(安徽理工大學(xué)計(jì)算機(jī)學(xué)院09-2班)i. 系統(tǒng)描述:本系統(tǒng)含有一個(gè)服務(wù)器(server.class)和多個(gè)客戶端(clinet.class),可以通過每 個(gè)客戶端查看和下載服務(wù)器端共享文件夾中的文件。ii. 功能描述:a. 查看服務(wù)器端共享文件夾列表操作:在 linux 終端下輸入 java clinet listfileso 參數(shù)說明:listfiles是固定參數(shù)。 結(jié)果:列出所有共享文件。b. 下載服務(wù)器端共享文件夾屮的文件操作:在 linux 終端下輸入 java clinet

2、 download filename dirpatho 參數(shù)說明:download是固定參數(shù),filename是想要下載的文件名,dirpath是 下載文件保存的路徑。結(jié)果:下載文件filename到地址dirpathoiii. 功能分析以及實(shí)現(xiàn):a.問題描述:如何創(chuàng)建可以用于多個(gè)客戶端連接的服務(wù)器?分析解決:因?yàn)閖ava語言提供了對(duì)多線程的支持,所以我們可以把服務(wù)器 設(shè)計(jì)為多線程的,對(duì)于每個(gè)客戶端的連接單獨(dú)開-條線程與之交 互。主要實(shí)現(xiàn)代碼:服務(wù)器端: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問題描述:如何把服務(wù)器端的文件列表發(fā)送到客戶端分析解決:服務(wù)器端如果取得一個(gè)文件名就發(fā)給客戶端也是可行的,但當(dāng)文 件較多的時(shí)顯得不清晰,如果可以把所有文件名組成的文件列表保存起來, 再統(tǒng)一發(fā)送到客戶端,客戶端再統(tǒng)一解析文件列表就顯得更合理。這其中也 體現(xiàn)了軟件設(shè)

4、計(jì)過程中封裝的思想。幸運(yùn)的是強(qiáng)大的網(wǎng)絡(luò)編程語言java就 可以很好的解決這一問題。遍歷服務(wù)器端的共享文件夾,把所有文件的文件 名以及它們的存儲(chǔ)地址存放到map中,在通過對(duì)象流的方式發(fā)送到客戶端, 客戶端解析對(duì)象流就可以獲取文件列表了。主耍實(shí)現(xiàn)代碼:服務(wù)器端: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;/把服務(wù)器端的文件列表傳到客戶端objectoutputsobjectoutputstream=newobjectoutputstream(outputstream);objectoutputstream.writeobject(map);objectoutputstream.close();c.問題描述:如何下載服務(wù)器端的文件分析解決:由于服務(wù)器端已經(jīng)把共享文件及地址的目錄保存在map中,所以

6、只要檢查用戶輸入的文件名(filename)是否存在map中就可以得知所下載的 文件是否存在,如果存在就可以通過map獲得該文件的存儲(chǔ)地址,服務(wù)器再 找到該文件,把它發(fā)向客戶端,客戶端就可以把該文件存放在指定的目錄 (dirpath) o 主要實(shí)現(xiàn)代碼:服務(wù)器端:if(map.containskey(filename)/把client請(qǐng)求的文件發(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端的寫停止了(既關(guān)閉輸出流),client端的讀才能停止,否 則一直等待讀取server發(fā)來的數(shù)據(jù)while(buf=stream.read()!= -1)fileoutputstream.write(buf);iv. 測試需對(duì)java clinet后面的參數(shù)進(jìn)行詳細(xì)的檢查可能錯(cuò)誤:1.參數(shù)個(gè)數(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 ()/服務(wù)器共亨文件存放的地址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”) /把服務(wù)器端的文件列表傳到客戶端objectoutputstream objectoutputstream=newobjectoutpu

14、tstream(outputstream);objectoutputstream.writeobject(map);objectoutputstream< close();elseif(map.containskey(filename)/把client請(qǐng)求的文件發(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;/獲得服務(wù)器端的文件列表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 ("服務(wù)器端文件列表如下:;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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論