




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
多線程Web服務(wù)器1實(shí)驗(yàn)?zāi)康模?用JAVA語(yǔ)言開發(fā)一個(gè)多線程的WEB服務(wù)器,它能并行服務(wù)于多個(gè)請(qǐng)求。發(fā)送網(wǎng)頁(yè)文件,讓網(wǎng)頁(yè)文件能夠通過(guò)在URL中制定端口號(hào)來(lái)被瀏覽器使用。2實(shí)驗(yàn)代碼及截圖classConnectionThreadextendsThread{ Socketclient;intcounter; publicConnectionThread(Socketcl,intc){ client=cl; counter=c; } publicvoidrun()//線程體 { try{ StringdestIP=client.getInetAddress().toString();//客戶機(jī)IP地址 intdestport=client.getPort();//客戶機(jī)端口號(hào) System.out.println("Connection"+counter+":connectedto"+destIP+"onport"+destport+"."); PrintStreamoutstream=newPrintStream(client.getOutputStream()); DataInputStreaminstream=newDataInputStream(client.getInputStream()); Stringinline=instream.readLine();//讀取Web瀏覽器提交的請(qǐng)求信息 System.out.println("Received:"+inline); if(getrequest(inline)){//如果是GET請(qǐng)求 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{//文件不存在時(shí) 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ī)的連接請(qǐng)求 newConnectionThread(client,i).start(); i++; } }catch(Exceptione){ System.out.println(e);}}}3實(shí)驗(yàn)軟硬件環(huán)境eclipseWindowsxpIE瀏覽器 4實(shí)驗(yàn)步驟(1)連接:Web瀏覽器與Web服務(wù)器建立連接,打開一個(gè)稱為socket(套接字)的虛擬文件,此文件的建立標(biāo)志著連接建立成功。(2)請(qǐng)求:Web瀏覽器通過(guò)socket向Web服務(wù)器提交請(qǐng)求。HTTP的請(qǐng)求一般是GET或POST命令(POST用于FORM參數(shù)的傳遞)。GET命令的格式為:GET路徑/文件名HTTP/1.1文件名指出所訪問的文件,HTTP/1.1指出Web瀏覽器使用的HTTP版本。(3)應(yīng)答:Web瀏覽器提交請(qǐng)求后,通過(guò)HTTP協(xié)議傳送給Web服務(wù)器。Web服務(wù)器接到后,進(jìn)行事務(wù)處理,處理結(jié)果又通過(guò)HTTP傳回給Web瀏覽器,從而在Web瀏覽器上顯示出所請(qǐng)求的頁(yè)面。為了告知Web瀏覽器傳送內(nèi)容的類型,Web服務(wù)器首先傳送一些HTTP頭信息,然后傳送具體內(nèi)容(即HTTP體信息),HTTP頭信息和HTTP體信息之間用一個(gè)空行分開。(4)關(guān)閉連接:當(dāng)應(yīng)答結(jié)束后,Web瀏覽器與Web服務(wù)器必須斷開,以保證其它Web瀏覽器能夠與Web服務(wù)器建立連接。5實(shí)驗(yàn)心得Java中實(shí)現(xiàn)多線程有兩種途徑:繼承Thread類或者實(shí)現(xiàn)Runnable接口。此處使用了接口的方式生成線程,因?yàn)榻涌诳梢詫?shí)現(xiàn)多繼承,況且Runnable只有一個(gè)run方法,很適合繼承。在使用Thread的時(shí)候只需繼承Thread,并且new一個(gè)實(shí)例出來(lái),調(diào)用start()方法即可以啟動(dòng)一個(gè)線程。在本次試驗(yàn)中,通過(guò)用java語(yǔ)言開發(fā)一個(gè)多線程的web服務(wù)器,能并行服務(wù)于多個(gè)請(qǐng)求,來(lái)掌握套接字編程技術(shù),了解并運(yùn)用http協(xié)議的作用原理,實(shí)現(xiàn)多線程web服務(wù)器設(shè)計(jì)。6參考文獻(xiàn):,1計(jì)算機(jī)網(wǎng)絡(luò):自頂向下方法(原書第4版)/(美)庫(kù)羅斯(Kurose,J.F.)等著;陳鳴譯--北京:機(jī)械工業(yè)出版社,2008.122java從入門到精通:李鐘尉,馬文強(qiáng),陳丹丹等編著;--清華大學(xué)出版社,2008.93實(shí)驗(yàn)指導(dǎo)書郵件客戶機(jī)1實(shí)驗(yàn)?zāi)康模?為發(fā)送者提供一個(gè)圖形界面,其中包含:發(fā)送電子郵件地址、接受者電子郵件地址、郵件主題和本身。開發(fā)一個(gè)Internet上的使用STMP協(xié)議的網(wǎng)絡(luò)服務(wù)器的郵件客戶端,在WindowsXP,Windows7系統(tǒng)下,使用JAVA語(yǔ)言開發(fā),并最終運(yùn)行該程序。2實(shí)驗(yàn)部分代碼及截圖:在發(fā)件人框中填寫相應(yīng)的信息,點(diǎn)擊發(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("你好!這是一封測(cè)試郵件"); 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)建立連接!"); } } //注冊(cè)到郵件服務(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("注冊(cè)郵件服務(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("用戶驗(yàn)證失??!"); } result=sendServer(encode.encode(message.getUser().getBytes()),in,out); if(result!=334){ thrownewIOException("用戶名錯(cuò)誤!"); } result=sendServer(encode.encode(message.getPassword().getBytes()),in,out); if(result!=235){ thrownewIOException("驗(yàn)證失??!"); } } //開始發(fā)送消息,郵件源地址 publicvoidmailfrom(Stringsource,BufferedReaderin,BufferedWriterout)throwsIOException{ intresult; result=sendServer("MAILFROM:<"+source+">",in,out); if(result!=250){ thrownewIOException("指定源地址錯(cuò)誤"); } } //設(shè)置郵件收件人 publicvoidrcpt(Stringtouchman,BufferedReaderin,BufferedWriterout)throwsIOException{ intresult; result=sendServer("RCPTTO:<"+touchman+">",in,out); if(result!=250){ thrownewIOException("指定目的地址錯(cuò)誤!"); } } //郵件體 //退出 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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 蕭山區(qū)綠植租賃管理辦法
- 融媒體中心素材管理辦法
- 衡水市小區(qū)收費(fèi)管理辦法
- 裝修管理辦法規(guī)定第六條
- 西安市大氣分類管理辦法
- 規(guī)范出讓金管理暫行辦法
- 證券業(yè)務(wù)員管理辦法規(guī)定
- 課堂教學(xué)管理辦法教務(wù)處
- 財(cái)政部現(xiàn)金清算管理辦法
- 貴州省危險(xiǎn)房屋管理辦法
- 2025年醫(yī)療器械管理培訓(xùn)考試試卷及答案
- 海洋通信網(wǎng)絡(luò)完善
- 機(jī)關(guān)反食品浪費(fèi)活動(dòng)方案
- 酒店消防安全管理制度完整
- 膀胱癌護(hù)理小講課比賽
- 福建廈門雙十中學(xué)2024~2025學(xué)年高一下冊(cè)第一次月考數(shù)學(xué)試題
- 2024年四川省甘孜縣林業(yè)局公開招聘試題帶答案詳解
- 中醫(yī)推拿知識(shí)培訓(xùn)課件
- 團(tuán)播培訓(xùn)直播課件
- 天津市和平區(qū)二十一中2025年英語(yǔ)七年級(jí)第二學(xué)期期末考試試題含答案
- 2025至2030中國(guó)電茶爐行業(yè)市場(chǎng)發(fā)展現(xiàn)狀及競(jìng)爭(zhēng)格局與投資發(fā)展報(bào)告
評(píng)論
0/150
提交評(píng)論