計算機(jī)網(wǎng)絡(luò)原理實驗報告_第1頁
計算機(jī)網(wǎng)絡(luò)原理實驗報告_第2頁
計算機(jī)網(wǎng)絡(luò)原理實驗報告_第3頁
計算機(jī)網(wǎng)絡(luò)原理實驗報告_第4頁
計算機(jī)網(wǎng)絡(luò)原理實驗報告_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

多線程Web服務(wù)器1實驗?zāi)康模?用JAVA語言開發(fā)一個多線程的WEB服務(wù)器,它能并行服務(wù)于多個請求。發(fā)送網(wǎng)頁文件,讓網(wǎng)頁文件能夠通過在URL中制定端口號來被瀏覽器使用。2實驗代碼及截圖classConnectionThreadextendsThread{ Socketclient;intcounter; publicConnectionThread(Socketcl,intc){ client=cl; counter=c; } publicvoidrun()//線程體 { try{ StringdestIP=client.getInetAddress().toString();//客戶機(jī)IP地址 intdestport=client.getPort();//客戶機(jī)端口號 System.out.println("Connection"+counter+":connectedto"+destIP+"onport"+destport+"."); PrintStreamoutstream=newPrintStream(client.getOutputStream()); DataInputStreaminstream=newDataInputStream(client.getInputStream()); Stringinline=instream.readLine();//讀取Web瀏覽器提交的請求信息 System.out.println("Received:"+inline); if(getrequest(inline)){//如果是GET請求 Stringfilename=getfilename(inline); Filefile=newFile(filename); if(file.exists()){//若文件存在,則將文件送給Web瀏覽器 System.out.println(filename+"requested."); outstream.println("HTTP/1.0200OK"); outstream.println("MIME_version:1.0"); outstream.println("Content_Type:text/html"); intlen=(int)file.length(); outstream.println("Content_Length:"+len); outstream.println(""); sendfile(outstream,file);//發(fā)送文件 outstream.flush(); }else{//文件不存在時 Stringnotfound="<html><head><title>NotFound</title></head><body><h1>Error404-filenotfound</h1></body></html>"; outstream.println("HTTP/1.0404nofound"); while(true){ client=server.accept();//接受客戶機(jī)的連接請求 newConnectionThread(client,i).start(); i++; } }catch(Exceptione){ System.out.println(e);}}}3實驗軟硬件環(huán)境eclipseWindowsxpIE瀏覽器 4實驗步驟(1)連接:Web瀏覽器與Web服務(wù)器建立連接,打開一個稱為socket(套接字)的虛擬文件,此文件的建立標(biāo)志著連接建立成功。(2)請求:Web瀏覽器通過socket向Web服務(wù)器提交請求。HTTP的請求一般是GET或POST命令(POST用于FORM參數(shù)的傳遞)。GET命令的格式為:GET路徑/文件名HTTP/1.1文件名指出所訪問的文件,HTTP/1.1指出Web瀏覽器使用的HTTP版本。(3)應(yīng)答:Web瀏覽器提交請求后,通過HTTP協(xié)議傳送給Web服務(wù)器。Web服務(wù)器接到后,進(jìn)行事務(wù)處理,處理結(jié)果又通過HTTP傳回給Web瀏覽器,從而在Web瀏覽器上顯示出所請求的頁面。為了告知Web瀏覽器傳送內(nèi)容的類型,Web服務(wù)器首先傳送一些HTTP頭信息,然后傳送具體內(nèi)容(即HTTP體信息),HTTP頭信息和HTTP體信息之間用一個空行分開。(4)關(guān)閉連接:當(dāng)應(yīng)答結(jié)束后,Web瀏覽器與Web服務(wù)器必須斷開,以保證其它Web瀏覽器能夠與Web服務(wù)器建立連接。5實驗心得Java中實現(xiàn)多線程有兩種途徑:繼承Thread類或者實現(xiàn)Runnable接口。此處使用了接口的方式生成線程,因為接口可以實現(xiàn)多繼承,況且Runnable只有一個run方法,很適合繼承。在使用Thread的時候只需繼承Thread,并且new一個實例出來,調(diào)用start()方法即可以啟動一個線程。在本次試驗中,通過用java語言開發(fā)一個多線程的web服務(wù)器,能并行服務(wù)于多個請求,來掌握套接字編程技術(shù),了解并運用http協(xié)議的作用原理,實現(xiàn)多線程web服務(wù)器設(shè)計。6參考文獻(xiàn):,1計算機(jī)網(wǎng)絡(luò):自頂向下方法(原書第4版)/(美)庫羅斯(Kurose,J.F.)等著;陳鳴譯--北京:機(jī)械工業(yè)出版社,2008.122java從入門到精通:李鐘尉,馬文強,陳丹丹等編著;--清華大學(xué)出版社,2008.93實驗指導(dǎo)書郵件客戶機(jī)1實驗?zāi)康模?為發(fā)送者提供一個圖形界面,其中包含:發(fā)送電子郵件地址、接受者電子郵件地址、郵件主題和本身。開發(fā)一個Internet上的使用STMP協(xié)議的網(wǎng)絡(luò)服務(wù)器的郵件客戶端,在WindowsXP,Windows7系統(tǒng)下,使用JAVA語言開發(fā),并最終運行該程序。2實驗部分代碼及截圖:在發(fā)件人框中填寫相應(yīng)的信息,點擊發(fā)送按鈕即可向目標(biāo)郵箱發(fā)送郵件。publicclassMailMessage{ publicstaticvoidmain(String[]args){ EventQueue.invokeLater(newRunnable(){ publicvoidrun(){ try{ SendFrameframe=newSendFrame(); frame.setVisible(true); }catch(Exceptione){ e.printStackTrace();} /** *Createtheframe. */ publicSendFrame(){ thisFrame=this; setTitle("JavaMailclient"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100,100,450,328); contentPane=newJPanel(); contentPane.setBorder(newEmptyBorder(5,5,5,5)); setContentPane(contentPane); contentPane.setLayout(null); JLabellblFrom=newJLabel("from:"); lblFrom.setBounds(10,10,54,22); contentPane.add(lblFrom); JLabellblTo=newJLabel("To:"); lblTo.setBounds(10,42,42,22); contentPane.add(lblTo); JLabellblSubject=newJLabel("Subject:"); lblSubject.setBounds(10,74,54,22); contentPane.add(lblSubject); txt_From=newJTextField(); txt_From.setEditable(false); txt_From.setText("1025674623@"); txt_From.setBounds(49,11,383,21); contentPane.add(txt_From); txt_From.setColumns(10); txt_To=newJTextField(); txt_To.setText("1025674623@"); txt_To.setColumns(10); txt_To.setBounds(49,42,383,21); contentPane.add(txt_To); text_Subject=newJTextField(); text_Subject.setText("作業(yè)2:郵件客戶機(jī)"); text_Subject.setColumns(10); text_Subject.setBounds(66,73,366,21); contentPane.add(text_Subject); JLabellblMassage=newJLabel("Massage:"); lblMassage.setBounds(10,101,64,15); contentPane.add(lblMassage); JButtonbtnQuit=newJButton("Quit"); btnQuit.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ thisFrame.dispose(); } }); btnQuit.setBounds(295,271,137,23); contentPane.add(btnQuit); JScrollPanescrollPane=newJScrollPane(); scrollPane.setBounds(10,229,422,-101); contentPane.add(scrollPane); Panelpanel=newPanel(); panel.setBounds(10,115,422,156); contentPane.add(panel); panel.setLayout(null); ScrollPanescrollPane_1=newScrollPane(); scrollPane_1.setBounds(0,0,422,156); panel.add(scrollPane_1); finalTextAreaSend_TextArea=newTextArea(); Send_TextArea.setText("你好!這是一封測試郵件"); Send_TextArea.setBounds(0,0,440,170); scrollPane_1.add(Send_TextArea); JButtonbtnSend=newJButton("Send"); btnSend.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ Stringtxtfrom=txt_From.getText(); Stringtxtto=txt_To.getText(); Stringtxtsubject=text_Subject.getText(); Stringsendtextarea=Send_TextArea.getText(); try{ MailMessagemessage=newMailMessage(); message.setFrom(txtfrom);//發(fā)件人 message.setTo(txtto);//收件人 Stringserver="";//郵件服務(wù)器 message.setSubject(txtsubject);//郵件主題 message.setContent(sendtextarea);//郵件內(nèi)容 message.setDatafrom(txtfrom);//發(fā)件人,在郵件的發(fā)件人欄目中顯示 message.setDatato(txtto);//收件人,在郵件的收件人欄目中顯示 message.setUser("1025674623");//登陸郵箱的用戶名 message.setPassword("zjr*******(保密)");//登陸郵箱的密碼 SendFramesmtp=newSendFrame(server,25); booleanflag; flag=smtp.sendMail(message,server); if(flag){ JOptionPane.showMessageDialog(null,"信息已成功發(fā)送!","提示",JOptionPane.INFORMATION_MESSAGE); } else{ JOptionPane.showMessageDialog(null,"郵件發(fā)送失?。?,"提示",JOptionPane.INFORMATION_MESSAGE); } //System.out.println("iuhfihulaeihba"); }catch(UnknownHostExceptione1){ //TODOAuto-generatedcatchblock e1.printStackTrace(); }catch(IOExceptione1){ //TODOAuto-generatedcatchblock e1.printStackTrace(); } // JOptionPane.showMessageDialog(null,"信息已成功發(fā)送!","提示",JOptionPane.INFORMATION_MESSAGE);// System.out.println(txtfrom+"\n"+txtto+"\n"+txtsubject+"\n"+sendtextarea); } }); btnSend.setBounds(10,271,144,23); contentPane.add(btnSend); JButtonbtnClear=newJButton("Clear"); btnClear.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ txt_To.setText(""); text_Subject.setText(""); Send_TextArea.setText(""); JOptionPane.showMessageDialog(null,"信息刪除成功!","提示",JOptionPane.INFORMATION_MESSAGE); } }); btnClear.setBounds(149,271,150,23); contentPane.add(btnClear); } privatebooleandebug=true; BASE64Encoderencode=newBASE64Encoder();//用于加密后發(fā)送用戶名和密碼 privateSocketsocket; publicSendFrame(Stringserver,intport)throwsUnknownHostException,IOException{ try{ socket=newSocket(server,25); }catch(SocketExceptione){// System.out.println(e.getMessage()); }catch(Exceptione){ e.printStackTrace(); }finally{// System.out.println("已經(jīng)建立連接!"); } } //注冊到郵件服務(wù)器 publicvoidhelo(Stringserver,BufferedReaderin,BufferedWriterout)throwsIOException{ intresult; result=getResult(in); //連接上郵件服務(wù)后,服務(wù)器給出220應(yīng)答 if(result!=220){ thrownewIOException("連接服務(wù)器失敗"); } result=sendServer("HELO"+server,in,out); //HELO命令成功后返回250 if(result!=250) { thrownewIOException("注冊郵件服務(wù)器失??!"); } } privateintsendServer(Stringstr,BufferedReaderin,BufferedWriterout)throwsIOException{ out.write(str); out.newLine(); out.flush(); if(debug) {// System.out.println("已發(fā)送命令:"+str); } returngetResult(in); } publicintgetResult(BufferedReaderin){ Stringline=""; try{ line=in.readLine(); if(debug){// System.out.println("服務(wù)器返回狀態(tài):"+line); } }catch(Exceptione){ e.printStackTrace(); } //從服務(wù)器返回消息中讀出狀態(tài)碼,將其轉(zhuǎn)換成整數(shù)返回 StringTokenizerst=newStringTokenizer(line,""); returnInteger.parseInt(st.nextToken()); } publicvoidauthLogin(MailMessagemessage,BufferedReaderin,BufferedWriterout)throwsIOException{ intresult; result=sendServer("AUTHLOGIN",in,out); if(result!=334){ thrownewIOException("用戶驗證失??!"); } result=sendServer(encode.encode(message.getUser().getBytes()),in,out); if(result!=334){ thrownewIOException("用戶名錯誤!"); } result=sendServer(encode.encode(message.getPassword().getBytes()),in,out); if(result!=235){ thrownewIOException("驗證失?。?); } } //開始發(fā)送消息,郵件源地址 publicvoidmailfrom(Stringsource,BufferedReaderin,BufferedWriterout)throwsIOException{ intresult; result=sendServer("MAILFROM:<"+source+">",in,out); if(result!=250){ thrownewIOException("指定源地址錯誤"); } } //設(shè)置郵件收件人 publicvoidrcpt(Stringtouchman,BufferedReaderin,BufferedWriterout)throwsIOException{ intresult; result=sendServer("RCPTTO:<"+touchman+">",in,out); if(result!=250){ thrownewIOException("指定目的地址錯誤!"); } } //郵件體 //退出 publicvoidquit(BufferedReaderin,BufferedWriterout)throwsIOException{ intresult; result=sendServer("QUIT",in,out); } //發(fā)送郵件主程序 publicbooleansendMail(MailMessagemessage,Stringserver){ try{ BufferedReaderin=newBufferedReader(newInputStreamReader(socket.getInputStream())); BufferedWriterout=newBufferedWriter(newOutputStreamWriter(socket.getOutputStream())); helo(server,in,out);//HELO命令 authLogin(message,in,ou

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論