




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、 計算機網(wǎng)絡課程設計報告利用c+實現(xiàn)smtp協(xié)議系 別電子信息系專業(yè)名稱計算機科學與技術班級學號學生姓名指導教師成 績2011年 7月 12日第 33 頁 東北大學秦皇島分校課程設計 利用c+實現(xiàn)smtp協(xié)議1.概述smtp(simple mail transfer protocol)即簡單郵件傳輸協(xié)議,是一種提供可靠且有效電子郵件傳輸?shù)膮f(xié)議。smtp是建立在ftp文件傳輸服務上的一種郵件服務,主要用于傳輸系統(tǒng)之間的郵件信息并提供與來信有關的通知。smtp目前已是事實上的在internet傳輸e-mail的標準,是一個相對簡單的基于文本的協(xié)議。在其之上指定了一條消息的一個或多個接收者(在大多數(shù)
2、情況下被確定是存在的),然后消息文本就傳輸了??梢院芎唵蔚赝ㄟ^telnet程序來測試一個smtp服務器,smtp使用tcp端口25。要為一個給定的域名決定一個smtp服務器,需要使用mx(mail exchange)dns。1.1設計題目及實現(xiàn)目標設計題目:利用c+實現(xiàn)smtp協(xié)議;實現(xiàn)目標:實現(xiàn)smtp協(xié)議的基本功能,包括客戶機的命令與數(shù)據(jù)。1.2開發(fā)環(huán)境簡介 本次開發(fā)用visual c+ 6.0作為開發(fā)環(huán)境。 vc+是微軟公司開發(fā)的一個ide(集成開發(fā)環(huán)境),換句話說,就是使用c+的一個開發(fā)平臺.有些軟件就是這個編出來的.另外還有vb,vf.只是使用不同語言.但是,vc+是windows平
3、臺上的c+編程環(huán)境,學習vc要了解很多windows平臺的特性并且還要掌握mfc、atl、com等的知識,難度比較大。windows下編程需要了解windows的消息機制以及回調(callback)函數(shù)的原理;mfc是win32api的包裝類,需要理解文檔視圖類的結構,窗口類的結構,消息流向等等;com是代碼共享的二進制標準,需要掌握其基本原理等等。vc作為一個主流的開發(fā)平臺一直深受編程愛好者的喜愛,但是很多人卻對它的入門感到難于上青天,究其原因主要是大家對他錯誤的認識造成的,嚴格的來說 vc+不是門語言,雖然它和c+之間有密切的關系,如果形象點比喻的話,可以c+看作為一種”工業(yè)標準”,而vc
4、+則是某種操作系統(tǒng)平臺下的”廠商標準”,而”廠商標準”是在遵循”工業(yè)標準”的前提下擴展而來的。vc+應用程序的開發(fā)主要有兩種模式2.系統(tǒng)設計分析2.1 協(xié)議分析smtp 獨立于特定的傳輸子系統(tǒng),且只需要可靠有序的數(shù)據(jù)流信道支持。 smtp 重要特性之一是其能跨越網(wǎng)絡傳輸郵件,即“ smtp 郵件中繼”。通常, 一個網(wǎng)絡可以由公用互聯(lián)網(wǎng)上 tcp 可相互訪問的主機、防火墻分隔的 tcp/ip 網(wǎng)絡上 tcp 可相互訪問的主機,及其它 lan/wan 中的主機利用非 tcp 傳輸層協(xié)議組成。使用 smtp ,可實現(xiàn)相同網(wǎng)絡上處理機之間的郵件傳輸,也可通過中繼器或網(wǎng)關實現(xiàn)某處理機與其它網(wǎng)絡之間的郵件
5、傳輸。 smtp協(xié)議工作原理smtp是工作在兩種情況下:一是電子郵件從客戶機傳輸?shù)椒掌鳎憾菑哪骋粋€服務器傳輸?shù)搅硪粋€服務器。smtp也是個請求/響應協(xié)議,命令和響應都是基于asc文本,并以cr和lf符結束。響應包括一個表示返回狀態(tài)的三位數(shù)字代碼。smtp在tcp協(xié)議25號端口監(jiān)聽連續(xù)請求。 連接和發(fā)送過程如下: (1)建立tcp 連接。 (2)客戶端發(fā)送helo命令以標識發(fā)件人自己的身份,然后客戶端發(fā)送mail命令;服務器端正希望以ok作為響應,表明準備接收。 (3)客戶端發(fā)送rcpt命令,以標識該電子郵件的計劃接收人,可以有多個rcpt行;服務器端則表示是否愿意為收件人接收郵件。 (4)
6、協(xié)商結束,發(fā)送郵件,用命令data發(fā)送。 (5)以“.”號表示結束輸入內容一起發(fā)送出去,結束此次發(fā)送,用quit命令退出。3.程序核心代碼3.1服務器端相關代碼:1)、相關核心代碼如下:/ smtpseverdlg.cpp : implementation file#include stdafx.h#include smtpsever.h#include smtpseverdlg.h#include picture.h#include / csmtpseverdlg message handlersbool csmtpseverdlg:oninitdialog()cdialog:oninitd
7、ialog();/ add about. menu item to system menu./ idm_aboutbox must be in the system command range.assert(idm_aboutbox & 0xfff0) = idm_aboutbox);assert(idm_aboutbox appendmenu(mf_separator);psysmenu-appendmenu(mf_string, idm_aboutbox, straboutmenu);/ set the icon for this dialog. the framework does th
8、is automatically/ when the applications main window is not a dialogseticon(m_hicon, true);/ set big iconseticon(m_hicon, false);/ set small icon/ todo: add extra initialization hereisdatacontent = false ;isshow = false ;m_listener.setparent(this);m_server.setparent(this);return true; / return true u
9、nless you set the focus to a controlvoid csmtpseverdlg:onsyscommand(uint nid, lparam lparam)if (nid & 0xfff0) = idm_aboutbox)caboutdlg dlgabout;dlgabout.domodal();elsecdialog:onsyscommand(nid, lparam);void csmtpseverdlg:onaccept()cstring str; str.format(* 收到連接請求); m_list_smtp.insertstring(-1,str);if
10、(m_listener.accept(m_server)str.format(* 建立連接);m_list_smtp.insertstring(-1,str); str = _t(220 simple mail sever ready for mailrn); m_server.send(lpctstr)str,str.getlength();str = _t(s: )+str;m_list_smtp.insertstring(-1,str); m_server.asyncselect(fd_read); elsem_server.close();void csmtpseverdlg:onre
11、ceive() char buff65536; char local_host80; int nread; memset(buff,0,65536);/清空緩沖區(qū)接收數(shù)據(jù) nread = m_server.receive(buff, 65536); /根據(jù)讀到的長度 switch (nread) case 0: m_server.close(); break; case socket_error: if (getlasterror() != wsaewouldblock) afxmessagebox (error occurred); m_server.close(); break; defa
12、ult: buffnread =0; /terminate the string cstring sztemp; if(isdatacontent) while(strstr(buff,rn.rn)=null) str+=buff;memset(buff,0,65536);nread = m_server.receive(buff, 65536); str+=buff;cstring temp ;temp.format(%srnrn,(lpctstr)str.c_str();m_edit_con.setwindowtext(temp);base64_decode(temp);sztemp =
13、_t(250 message accepted for deliveryrn);m_server.send(sztemp,sztemp.getlength();sztemp = _t(s: )+sztemp;m_list_smtp.insertstring(-1,sztemp);sztemp.empty();isdatacontent=false; if(strnicmp(buff,helo,4)=0) sztemp = buff; sztemp = _t(c: )+sztemp; /m_list_smtp.insertstring(-1,(lpctstr)buff); m_list_smtp
14、.insertstring(-1,(lpctstr)sztemp); gethostname(local_host,80); sztemp.format(_t(250 ok %srn),local_host); m_server.send(lpctstr)sztemp,sztemp.getlength(); sztemp = _t(s: )+sztemp; m_list_smtp.insertstring(-1,sztemp); sztemp.empty(); if(strnicmp(buff,mail from:,10)=0) sztemp = buff; sztemp = _t(c: )+
15、sztemp; /m_list_smtp.insertstring(-1,(lpctstr)buff); m_list_smtp.insertstring(-1,(lpctstr)sztemp); sztemp = _t(250 sender okrn); m_server.send(sztemp,sztemp.getlength(); sztemp = _t(s: )+sztemp; m_list_smtp.insertstring(-1,sztemp); sztemp.empty(); if(strnicmp(buff,rcpt to:,8)=0) sztemp = buff; sztem
16、p = _t(c: )+sztemp; /m_list_smtp.insertstring(-1,(lpctstr)buff); m_list_smtp.insertstring(-1,(lpctstr)sztemp); sztemp = _t(250 receiver okrn); m_server.send(sztemp,sztemp.getlength(); sztemp = _t(s: )+sztemp; m_list_smtp.insertstring(-1,sztemp); sztemp.empty(); if(strnicmp(buff,data,4)=0) sztemp = b
17、uff; sztemp = _t(c: )+sztemp; /m_list_smtp.insertstring(-1,(lpctstr)buff); m_list_smtp.insertstring(-1,(lpctstr)sztemp); sztemp = _t(354 go ahead. end withrn); m_server.send(sztemp,sztemp.getlength(); sztemp = _t(s: )+sztemp; m_list_smtp.insertstring(-1,sztemp); sztemp.empty(); isdatacontent=true; i
18、f(strnicmp(buff,quit,4)=0) sztemp = buff; sztemp = _t(c: )+sztemp; /m_list_smtp.insertstring(-1,(lpctstr)buff); m_list_smtp.insertstring(-1,(lpctstr)sztemp); sztemp = _t(221 quit,goodbye !rn); m_server.send(sztemp,sztemp.getlength(); sztemp = _t(s: )+sztemp; m_list_smtp.insertstring(-1,sztemp); issh
19、ow = true; if (strnicmp(buff,auth login,10)=0) sztemp = buff; sztemp = _t(c: )+sztemp; /m_list_smtp.insertstring(-1,(lpctstr)buff); m_list_smtp.insertstring(-1,(lpctstr)sztemp); sztemp = _t(334 dxnlcm5hbwu6rn); m_server.send(sztemp,sztemp.getlength(); sztemp = _t(s: )+sztemp; m_list_smtp.insertstrin
20、g(-1,sztemp); sztemp.empty(); if (strnicmp(buff,bwfu,4)=0) sztemp = buff; sztemp = _t(c: )+sztemp; /m_list_smtp.insertstring(-1,(lpctstr)buff); m_list_smtp.insertstring(-1,(lpctstr)sztemp); sztemp = _t(334 ugfzc3dvcmq6rn); m_server.send(sztemp,sztemp.getlength(); sztemp = _t(s: )+sztemp; m_list_smtp
21、.insertstring(-1,sztemp); sztemp.empty(); if (strnicmp(buff,bgfp,4)=0) sztemp = buff; sztemp = _t(c: )+sztemp; /m_list_smtp.insertstring(-1,(lpctstr)buff); m_list_smtp.insertstring(-1,(lpctstr)sztemp); sztemp = _t(235 authentication successfulrn); m_server.send(sztemp,sztemp.getlength(); sztemp = _t
22、(s: )+sztemp; m_list_smtp.insertstring(-1,sztemp); sztemp.empty(); bool csmtpseverdlg:destroywindow() / todo: add your specialized code here and/or call the base classm_listener.close();m_server.close();return cdialog:destroywindow();void csmtpseverdlg:onclose()m_server.close();cstring str;str.forma
23、t(listening on port %d, 25);m_list_smtp.insertstring(-1,str); void csmtpseverdlg:onbuttonclose() / todo: add your control notification handler code herem_listener.close();m_server.close();m_list_smtp.insertstring(-1,s: 服務器關閉成功);void csmtpseverdlg:onbuttonopen() / todo: add your control notification
24、handler code herebool bflag = m_listener.create(25,sock_stream); if(!bflag)if (getlasterror() != wsaewouldblock) tchar szerror256; wsprintf(szerror, socket建立失敗: %d, getlasterror();m_listener.close(); afxmessagebox(szerror);afxmessagebox(wrong);return ;if(!m_listener.listen(1)if (getlasterror() != ws
25、aewouldblock) tchar szerror256; wsprintf(szerror, 監(jiān)聽失敗: %d, getlasterror(); m_listener.close(); afxmessagebox(szerror);return ;m_list_smtp.insertstring(-1,* 服務器準備好 *);m_list_smtp.insertstring(-1,*);void csmtpseverdlg:onbuttonquit() / todo: add your control notification handler code herem_listener.cl
26、ose();m_server.close();cdialog:destroywindow();/ base64.cpp: implementation of the base64 class.#include stdafx.h#include smtpsever.h#include base64.h#ifdef _debug#undef this_filestatic char this_file=_file_;#define new debug_new#endifbase64:base64()base64:base64()static const std:string base64_char
27、s = abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/;std:string base64:base64_encode(const std:string &s) return base64_encode(const unsigned char *)s.c_str(), s.length();/解碼函數(shù)的實現(xiàn)/std:string base64:base64_decode(unsigned char const* encoded_string, unsigned int in_len) int i = 0; int
28、 j = 0; int in_ = 0;unsigned char char_array_44, char_array_33;std:string ret; while (in_len- & ( encoded_stringin_ != =) & is_base64(encoded_stringin_) char_array_4i+ = encoded_stringin_; in_+; if (i =4) for (i = 0; i 4; i+) char_array_4i = base64_chars.find(char_array_4i); char_array_30 = (char_ar
29、ray_40 4); /將六個字節(jié)和下一個六字節(jié)的前兩位組成8位解碼 char_array_31 = (char_array_41 & 0xf) 2); char_array_32 = (char_array_42 & 0x3) 6) + char_array_43; for (i = 0; (i 3); i+) ret += char_array_3i; i = 0; if (i)/不足24位緩沖區(qū)的用0補足再解碼 for (j = i; j 4; j+) char_array_4j = 0; for (j = 0; j 4; j+) char_array_4j = base64_chars
30、.find(char_array_4j); char_array_30 = (char_array_40 4); char_array_31 = (char_array_41 & 0xf) 2); char_array_32 = (char_array_42 & 0x3) 6) + char_array_43; for (j = 0; (j 2; /右移 char_array_41 = (char_array_30 & 0x03) 4); char_array_42 = (char_array_31 & 0x0f) 6); char_array_43 = char_array_32 & 0x3
31、f; for(i = 0; (i 4) ; i+) ret += base64_charschar_array_4i; i = 0; if (i) for(j = i; j 2; char_array_41 = (char_array_30 & 0x03) 4); char_array_42 = (char_array_31 & 0x0f) 6); char_array_43 = char_array_32 & 0x3f; for (j = 0; (j i + 1); j+) ret += base64_charschar_array_4j; /這個字符串是 亂碼把6個字節(jié)放在8個字節(jié)的空間里
32、 while(i+ onreceive();asyncselect(fd_read);casyncsocket:onreceive(nerrorcode);void cserversocket:onclose(int nerrorcode) / todo: add your specialized code here and/or call the base classif (nerrorcode = 0)(csmtpseverdlg*)m_pwnd)-onclose();casyncsocket:onclose(nerrorcode);/ listenersocket.cpp : imple
33、mentation file/#include stdafx.h#include smtpsever.h#include listenersocket.h#include smtpseverdlg.h#ifdef _debug#define new debug_new#undef this_filestatic char this_file = _file_;#endifclistenersocket:clistenersocket()clistenersocket:clistenersocket()/ do not edit the following lines, which are ne
34、eded by classwizard.#if 0begin_message_map(clistenersocket, casyncsocket)/afx_msg_map(clistenersocket)/afx_msg_mapend_message_map()#endif/ 0/ clistenersocket member functionsvoid clistenersocket:setparent(cdialog *pwnd)m_pwnd = pwnd;void clistenersocket:onaccept(int nerrorcode) if (nerrorcode = 0)(c
35、smtpseverdlg*)m_pwnd)-onaccept();casyncsocket:onaccept(nerrorcode);3.2客戶端相關代碼: 1)、相關核心代碼如下:/ emaildlg.cpp : implementation file#include stdafx.h#include email.h#include emaildlg.h#include .emaillibsmtp.h#include .emaillibmimemessage.h#include setup.h#ifdef _debug#define new debug_new#undef this_file
36、static char this_file = _file_;#endifstatic tchar _szfilter = text(all files0*.*0text files (*.txt)0*.txt0);/class caboutdlg : public cdialogpublic:caboutdlg();/ dialog data/afx_data(caboutdlg)enum idd = idd_aboutbox ;/afx_data/ classwizard generated virtual function overrides/afx_virtual(caboutdlg)
37、protected:virtual void dodataexchange(cdataexchange* pdx); / ddx/ddv support/afx_virtual/ implementationprotected:/afx_msg(caboutdlg)/afx_msgdeclare_message_map();caboutdlg:caboutdlg() : cdialog(caboutdlg:idd)/afx_data_init(caboutdlg)/afx_data_initvoid caboutdlg:dodataexchange(cdataexchange* pdx)cdi
38、alog:dodataexchange(pdx);/afx_data_map(caboutdlg)/afx_data_mapbegin_message_map(caboutdlg, cdialog)/afx_msg_map(caboutdlg)/ no message handlers/afx_msg_mapend_message_map()/ cemaildlg dialogcemaildlg:cemaildlg(cwnd* pparent /*=null*/): cdialog(cemaildlg:idd, pparent)/afx_data_init(cemaildlg)m_to = _
39、t();m_subject = _t();m_from = _t();m_content = _t();m_cc = _t();m_bcc = _t();/afx_data_init/ note that loadicon does not require a subsequent destroyicon in win32 m_bissetup = false;/m_hicon = afxgetapp()-loadicon(idr_mainframe);m_hicon = afxgetapp()-loadicon(idi_mail);void cemaildlg:dodataexchange(
40、cdataexchange* pdx)cdialog:dodataexchange(pdx);/afx_data_map(cemaildlg)ddx_control(pdx, idc_list_file, m_listbox);ddx_text(pdx, idc_edit_to, m_to);ddx_text(pdx, idc_edit_subject, m_subject);ddx_text(pdx, idc_edit_from, m_from);ddx_text(pdx, idc_edit_content, m_content);ddx_text(pdx, idc_edit_cc, m_c
41、c);ddx_text(pdx, idc_edit_bcc, m_bcc);/afx_data_mapbegin_message_map(cemaildlg, cdialog)/afx_msg_map(cemaildlg)on_wm_syscommand()on_wm_paint()on_wm_querydragicon()on_bn_clicked(idc_button_add_file, onbuttonaddfile)on_bn_clicked(idc_button_send, onbuttonsend)on_bn_clicked(idc_button_setup, onbuttonse
42、tup)/afx_msg_mapend_message_map()/ cemaildlg message handlersbool cemaildlg:oninitdialog()cdialog:oninitdialog();/ add about. menu item to system menu./ idm_aboutbox must be in the system command range.assert(idm_aboutbox & 0xfff0) = idm_aboutbox);assert(idm_aboutbox appendmenu(mf_separator);psysmenu-appendmenu(mf_string, idm_aboutbox, straboutmenu);/
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025成都租房合同簡化版
- 2025私人借款擔保協(xié)議合同
- 2025試用期勞動合同協(xié)議書
- 2025保險合同權益轉讓協(xié)議(轉讓)
- 2025養(yǎng)殖場租賃合同范本
- 2025混凝土澆筑工程合同
- 2025春季學期國家開放大學專科《高等數(shù)學基礎》一平臺在線形考(形考任務一至四)試題及答案
- 2025室內設計合作合同范本
- 2025二手車買賣合同二手車買賣合同范本
- 2025網(wǎng)絡設備采購合同(標準范本)
- 2022-2023學年天津市部分區(qū)高二(下)期中數(shù)學試卷及答案解析
- 青霉素的生產工藝流程專家講座
- 12j912-2常用設備用房
- 醫(yī)院侵害未成年人案件強制報告制度培訓課件
- 內蒙古曹四夭鉬礦床原生暈特征及深部找礦預測
- 小學生讀書知識競賽題
- GB/T 601-2016化學試劑標準滴定溶液的制備
- GB/T 28799.2-2020冷熱水用耐熱聚乙烯(PE-RT)管道系統(tǒng)第2部分:管材
- GB/T 20203-2006農田低壓管道輸水灌溉工程技術規(guī)范
- GB/T 14216-2008塑料膜和片潤濕張力的測定
- 新型節(jié)能型建筑材料的發(fā)展方向論文
評論
0/150
提交評論