![2023年計算機網絡原理實驗報告_第1頁](http://file4.renrendoc.com/view/18d14871ac8eda54c23df696d592e12a/18d14871ac8eda54c23df696d592e12a1.gif)
![2023年計算機網絡原理實驗報告_第2頁](http://file4.renrendoc.com/view/18d14871ac8eda54c23df696d592e12a/18d14871ac8eda54c23df696d592e12a2.gif)
![2023年計算機網絡原理實驗報告_第3頁](http://file4.renrendoc.com/view/18d14871ac8eda54c23df696d592e12a/18d14871ac8eda54c23df696d592e12a3.gif)
![2023年計算機網絡原理實驗報告_第4頁](http://file4.renrendoc.com/view/18d14871ac8eda54c23df696d592e12a/18d14871ac8eda54c23df696d592e12a4.gif)
![2023年計算機網絡原理實驗報告_第5頁](http://file4.renrendoc.com/view/18d14871ac8eda54c23df696d592e12a/18d14871ac8eda54c23df696d592e12a5.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
多線程Web服務器1實驗目的:用JAVA語言開發(fā)一個多線程的WEB服務器,它能并行服務于多個請求。發(fā)送網頁文獻,讓網頁文獻可以通過在URL中制定端標語來被瀏覽器使用。2實驗代碼及截圖?且http://localhost:8081/index.htmZ收藏賣骨亂6.11e6.9且http--WWW.baidu4首頁)http://localhost:8081/index.htm145646-456口收藏夾一:且6,118.6.9亂http-www.baidu?苜爽/涼交通大學好音InternetExplorer無法顯示該網頁InternetExplorer無法顯示該網頁您可以嘗試以下操作:[診斷連接問題I9更多信息classConnectionThreadextendsThread{Socketc1ient;intcounter;提示1_區(qū)一①信息已成功發(fā)送!確定publicclassMaiIMessage{publicstaticvoidmain(String[]args){oEventQucue.invokcLater(newRunnable(){opublicvoidrun(){try{9。SendFrameframe=newSendFrame();ggframe.setVisib1e(true);g。}catch(Exceptione){§e.printStackTrace();}/**。*Createtheframe.叫pub1icSendFrame(){0。thisFrame=this;asetTitle("JavaMailclient");sctDcfaultC1oscOpcration(JFrame.EXIT_ON_CLOSE);selBounds(100,100,450,328);contentPane=newJPane1();ooconten(Pane.setBorder(newEmptyBorder(5,5,5,5));wsetContentPane(contentPane);?contentPane.setLayoul(null);3JLabelIblFrom=newJLabel("from:");MblFrom.setBounds(10,10,54,22);ocontentPane.add(lblFrom);0odLabeIIblTo=newJLabel("To:");?1blTo.setBounds(I0,42,42,22);contentPane.add(IblTo);。JLabe1lb1Subject=newJLabel(*'Subject:11);dbISubject.setBounds(10,74,54,22);aocontentPane.add(lblSubject);txt_From=newJTextField();8txt_From.sctEditable(fa1se);Mx(_From.setText("");4xt_From.setBounds(49,11,383,21):^contentPane.add(txl_From);。txt_From.setColumns(10);?txt_To=newJTextField();txt_To.setText("M);stxt_To.se(Columns(10);3txt_To.setBounds(49,42,383,21);contentPane,add(txt_To);8text_Subject=newJTextFie1d();otext_Subject.selText("作業(yè)2:郵件客戶機)ext_Subject.setColumns(10);ootext_Subject.setBounds(66,73,366,21);contentPane.add(text_Subjecl);sJLabelIblMassage=newJLabel("Massage:");olblMassage.setBounds(10,101,64,15);contentPane.add(IblMassage);0000gjButtonbtnQuit=newJButton("Quit");?btnQuit.addActionListener(newActionListener(){opublievoidactionPcrformed(ActionEventc)(4hisFrame.dispose();,});0btnQuit.setBounds(295,271,137,23);contentPane.add(binQuit);0oJScro11PanescrollPane=newJScro11Pane();scrollPane.setBounds(10,229,422,-101);contentPane.add(scroUPanc);3Pane1pane1=newPanel();。paneLsetBounds(10,115,422,156);?contentPane.add(pane1);?pane1.setLayout(null);?ScrollPanescro11Pane_1=newScroIlPane();。scrollPane_1.setBounds(0,0,422,156);opanel.add(scro1IPane_1);gfina1TextAreaSend_TextArea=newTextArea();nd_TextArea.seiText("你好!這是一封測試郵件)。Send_TextArea.setBounds(0,0,440,170);scroIIPane_l.add(Scnd_TcxtArea);JButtonbtnSend=newJButton("Scnd");btnSend.addActionListener(newActionListener(){§pub1icvoidactionPerformed(ActionEvente){o?Stringtxtfrom=txt_From.getText();。。tringtxtto=txt_To.getText();aStringtxtsubject=text_Subject.geiText();gStringsendtextarea=Send_TextArea.getText();gtry(gMaiIMessagemessage=ncwMai1Messagc();8。。message.setFrom(txtfrom);//發(fā)件人3gmessage.setTo(txtto)^/收件人a。。Stringserver="";〃郵件服務器$。gmessage.selSubject(ixtsubject);〃郵件主題。。。message.setContent(sendtextarea);〃郵件內容3message.setDatafrom(txtfrom);〃發(fā)件人,在郵件的發(fā)件人欄目中顯示。a。message.setDatato(txlto);〃收件人,在郵件的收件人欄目中顯示。。message.setUser("");//登陸郵箱的用戶名message,setPassword("zjr*******(保密)");〃登陸郵箱的密碼“SendFramesmtp=newSendFrame(server,25);。booleanflag;03。f1ag=smtp.sendMail(message,server);agif(flag){爾。J0ptionPanc.showMessageDialog(nu11,"信息己成功發(fā)送!”,提醒”,JOptionPane.INF0RMAT10N_MESSAGE);3gO}geise{。。JOptionPane_showMessagcDia1og(nu11,"郵件發(fā)送失敗!”,提醒",JOptionPane.INFORMATION.MESSAGE);°}a//System.out.println("iuhfihulaeihba");。。?}catch(UnknownHostExceptionel){TODOAuto-gencratcdcatchblock"1.printStackTrace();8。。}catch(IOExceptione1){。。。//TODOAuto—generatedcatchbiockg?>e1.printStackTrace();//?時0ptionPane.showMessageDialog(null,"信息己成功發(fā)送!","提醒",JOptionPane.INF0RMATION_MESSAGE);/A?System.out.print1n(txtfrom+"\n"+txtto+"\n"+txtsubject+"\n"+sendtextarea);g}“});tnSend.setBounds(10,271,144,23);?contcnIPanc.add(btnScnd);sJButtonbtnC1car=newJBu11onC'Clcar°);bmCIear.addActionListener(newActionLislener(){。apublievoidactionPerformed(ActionEvente){。txt_To.setTextC*");text_Subject.setText(n");gSend_TextArea.selText("");,dOptionPane.showMessageDialog(nu11,”信息刪除成功!”,“提醒”,J0ptionPane.INFORMATION.MESSAGE);00|。});btnClear.se(Bounds(149?271,150,23);。conteniPane.add(btnClear);Jprivatebooleandebug=true;aBASE64Encoderencode=newBASE64Encoder();〃用于加密后發(fā)送用戶名和密碼。privateSocketsoeket;publicSendFrame(Stringserver,intport)throwsUnknownIIoslException,lOException{0try{?socket=newSocket(server,25);o}catch(SocketExceptione){//System,out.print1n(e.getMessage());o}catch(Exceptione){e.printStackTrace();}finally{//System.out.println("已經建立連接!))a)。//注冊到郵件服務器。publicvoidhelo(Stringserver,BufferedReaderin,BufferedWriterout)throwsIOException(ointresu1t;§result=getResuk(in);。〃連接上郵件服務后,服務器給出22。應答if(result!=220){thrownew10Exception("連接服務器失敗");°}。result=sendServer("HELO"+server,in,out);s//HELO命令成功后返回250oif(result!=250)a{。thrownewIOException("注冊郵件服務器失敗!”);))。privateintsendServer(Stringstr,BufferedReaderin,BufferedWriterout)throwsIOExccption{out.write(str):sout.ncwLinc();oout.flushO;。if(debug),(//?System.out.println("已發(fā)送命令:"+str);)returngetResult(in);°}publicintgetResuit(BufferedReaderin){oString1inc="iry(line=in.readLine();if(debug){//System.out.println("服務器返回狀態(tài):"4-line);。))catch(Exceptione){ae.printStackTrace();°}o//從服務器返回消息中讀出狀態(tài)碼,將其轉換成整數返I可。StringTokenizerst=newStringTokenizer(1ine,"");,returnInteger.parselnt(st.nextToken());)0。publicvoidauthLogin(MailMessagemessage,BufferedReaderin,BufferedWriterout)throwsIOException{intresult;oresu1t=sendScrvcr("AUTHLOGIN",in,out);if(resu1t!=334){thrownewIOException("用戶驗證失?。?):0)。result=sendServer(encode.encode(message.getUser().getBytes()),in,out):sif(result!=334){。thrownewI0Exception("用戶名錯誤廠);resuIt=sendServer(encode,encode(message.getPassword().geiByles()),in,out);if(result!=235)(。thrownewIOException("驗證失敗!");)I//開始發(fā)送消息,郵件源地址publicvoidmai1from(Stringsource.BufferedReaderin,BuffercdWriterout)throws10Exception{sintresuIt;result=sendServer("MAILFROM:<"+source+">",in,out):if(result!=25O){。thrownewlOException("指定源地址錯誤");3)°}。//設立郵件收件人publicvoidrcpt(Slringtouchman,BufferedReaderin,BufferedWriterout)throwsIOException{3intresu1t;3result=scndServer("RCPTT0:<"+touchman+">',,in,out);if(result!=250){thrownewIOException("指定目的地址錯誤!");//郵件體pub1icConnectionThread(Socketcl,intc){。。。dient=cl;。counter=c;0°}0opublicvoidrun()//線程體°{-try{00。StringdestlP=client.getInetAddress().toString();//客戶機工P地址。intdestport=c1ient.getPort();//客戶機端標語。System.out.println("Connection"+counter+:connectedto"+destIP+"onportH+destport+".");?oPrintStreamoutstream=newPrintStream(c1ient.get0utputStream());。。DatalnputStreaminstream=newDataInputStream(client.getInputStream());。oStringinIine=instream.readLine();//讀取Web瀏覽器提交的請求信息。System,out.print1n("Received:"+inline);O00o。。if(getrequest(inline)){//假如是GET請求Stringfilename=getfilename(in1ine);〃退出publievoidquit(BufferedReaderin,BufferedWriterout)throwsIOException{。intresult;result=sendServer("QUIT",in,out);Id〃發(fā)送郵件主程序pub1icboo1eansendMail(MailMessagemessage,Stringserver){,try{oBufferedReaderin=newBufferedReader(newInputStreamReader(socket.getInputStream()));。BufferedWriterout=newBufferedWriter(newOutputStreamWriter(socket.gctOutputStream()));he1o(server,in,out);//HELO命令§authLogin(message.in,out);//AUTHLOGIN命令◎mailfrom(message.ge(From(),in.oul);//MAILFROMrept(message.getTo(),in,out);//RCPT3data(message.getDatafrom(),message.gelDatato(),message.getSubject(),message.getContent(),in,out);//DATAquit(in,out);//QUIT3}catch(Exceptione){e.printStackTrace();returnfaIse;returntrue;。程序實現與服務器之間建立連接,根據服務器端發(fā)送的響應來完畢建立連接的過程,然后根據服務器發(fā)送回的響應信息,根據相應的模式,發(fā)送相應的信息,從而建立起與郵件服務器端的連接以發(fā)送郵件。3實驗軟硬件環(huán)境Es1ipseWINDOWSxp4實驗環(huán)節(jié)(1)創(chuàng)建和解析內容::Message類是創(chuàng)建和解析郵件核心API,它的實例對象代表一封郵件。。(2)發(fā)送郵件:Transport類是發(fā)送郵件的核心API,它的實例對象實現了某個郵件發(fā)送協議的郵件發(fā)送對象,例如SMTP協議。(3)接受郵件的:St。re類是接受郵件的核心API,它的是實例實現了郵件接受協議的郵件接受對象,例如POP3協議。5實驗心得通過本次實驗我加深對網絡通信協議、網絡編程接口等基本概念、基本原理的理解,大體了解了各種應用各種網絡通信協議和網絡編程接口編寫網絡應用程序的方法,拓寬了知識領域,鍛煉了實踐技能。比如郵件服務器按通訊協議可以劃分為兩種類型:STMP服務器。是SimpleMailTransferProtcol的簡寫,用于替用戶發(fā)送郵件和接受外面發(fā)送給本地用戶的郵件。是遵循SMTP協議的發(fā)送郵件服務器,,基于TCP服務的應用層協議。POP3服務器。是PostOfficeProtoco1的簡寫,用于替用戶幫助用戶讀取SMTP服務器接受進來的該用戶的郵件。它定義了郵件客戶端軟件與POP3服務器的通訊規(guī)則。不同的郵箱也許相應不同的服務器,應當選擇不同的協議,否則會報錯。6參考文獻:1計算機網絡:自頂向下方法(原書第4版)/(美)庫羅斯(Kurose,J.F.)等著;陳鳴譯-一北京:機械工業(yè)出版社,2023.122java從入門到精通:李鐘尉,馬文強,陳丹丹等編著;--清華大學出版社,2023.93實驗指導書。。。Fi1efile=newFi1e(filename);。。。訐(file.exists()){//若文獻存在,則將文獻送給Web瀏覽器。。System.out.println(filename+"requested.");°。。*outstream.println('*HTTP/1.0200OK");。。。outstream.printIn("MIME_version:1.0"。。。outstream.print1n("Content_Type:text/htm1");。3ooointlen=(int)file.length();。?outstream.printin("Content_Length:"+1en);?oo?outstream.println("");°°°°°°sendfile(outsiream,file);//發(fā)送文獻0。。outstream.flush();。o}else{II文獻不存在時。Stringnotfound="<htm1><headxtitle>NotFound</title></headxbody><hl>Error404-filenotfound</hl></body></html>";。ooutstream.printin("HTTP/1.0404nofound");ooutstream.println("Content_Type:text/html");。oooutstrearn.println("Content_Length:"+notfound.length()+2);outstream,println("");。。。?outstream.println(notfound);。outstream.flush();TOC\o"1-5"\h\z0000}0000}…longml=l;。while(ml<11100000)6{o0ml++;。。}〃延時。client.close();000}catch(lOExceptione){0。System.out.print1n("Exception:"+e);0)}/*獲取請求類型是否為“get”*/boo1eangetrequest(Strings){if(s.length()>0){。if(s.substring(0,3).equaIslgnoreCase("GET"))returntrue;returnfalse;/*獲取要訪問的文獻名*/Stringgetfilename(Strings){0。Stringf=s.substring(s.indexOf('')+1);。f=f.substring(0?f.indexOf(''));°try{if(f.charAt(0)=='/')。。f=f.substring(l);。}catch(StringIndexOutOfBoundsExceptione){System.out.println("Exception:"+e);)0if(f.equals(""))f="index.html";。returnf;)/火把指定文獻發(fā)送給Web瀏覽器*/voidsendfile(PrintStreamouts.Filefile){try(。。DatalnputStreamin=newDatalnputStream(newFileinputStneam(fi1e));。int1en=(int)fi1e.length();osbytebuf[]=newbyte[1en];。in.readFul1y(buf);outs,write(bu千,0,len);outs.flush();。3in.close();。}catch(Exceptione){o。0System.out.println("Errorretrievingfile.")。System.exit(1);0°})}pub1icclasswebsever{◎publicstaticvoidmain(String[]args){。//TODOAuto—generatedmethodstubinti=l,P0RT=8081;ServerSocketserver=nul1;。Socketdient=nul1;。。try(。。server=newServerSocket(PORT);。?System.out.print1n("WebServerislisteningonport"+
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 農機產品收購合同范例
- 2025年度家具市場調研與推廣服務合同
- 公用汽車維修合同范例
- 個人委托購買公寓合同范例
- 2025年度家政月嫂服務合同規(guī)范文本
- 債權無償轉讓合同范例
- 體能器材出租合同范本
- 冷庫材料采購合同范本
- 個人與單位合作合同范例
- ui外包合同范本
- JB-T 14509-2023 反滲透海水淡化設備技術規(guī)范
- 【藥用低密度聚乙烯袋探究4500字(論文)】
- 鄉(xiāng)鎮(zhèn)林長制培訓課件
- 茶葉運營方案
- 企業(yè)法律顧問方案
- 哈佛大學住房研究聯合中心:2024年美國房屋租賃報告
- 農商銀行貴金屬分析報告
- 人教版英語八年級下冊全冊教案教學設計及教學反思
- 軟件確認報告-模板
- 馬克思主義的誕生(何)
- 《紅樓夢第五回》課件
評論
0/150
提交評論