




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 计算机网络课程设计报告利用c+实现smtp协议系 别电子信息系专业名称计算机科学与技术班级学号学生姓名指导教师成 绩2011年 7月 12日 利用c+实现smtp协议1.概述smtp(simple mail transfer protocol)即简单邮件传输协议,是一种提供可靠且有效电子邮件传输的协议。smtp是建立在ftp文件传输服务上的一种邮件服务,主要用于传输系统之间的邮件信息并提供与来信有关的通知。smtp目前已是事实上的在internet传输e-mail的标准,是一个相对简单的基于文本的协议。在其之上指定了一条消息的一个或多个接收者(在大多数情况下被确定是存在的),然后消息文本就传
2、输了。可以很简单地通过telnet程序来测试一个smtp服务器,smtp使用tcp端口25。要为一个给定的域名决定一个smtp服务器,需要使用mx(mail exchange)dns。1.1设计题目及实现目标设计题目:利用c+实现smtp协议;实现目标:实现smtp协议的基本功能,包括客户机的命令与数据。1.2开发环境简介 本次开发用visual c+ 6.0作为开发环境。 vc+是微软公司开发的一个ide(集成开发环境),换句话说,就是使用c+的一个开发平台.有些软件就是这个编出来的.另外还有vb,vf.只是使用不同语言.但是,vc+是windows平台上的c+编程环境,学习vc要了解很多w
3、indows平台的特性并且还要掌握mfc、atl、com等的知识,难度比较大。windows下编程需要了解windows的消息机制以及回调(callback)函数的原理;mfc是win32api的包装类,需要理解文档视图类的结构,窗口类的结构,消息流向等等;com是代码共享的二进制标准,需要掌握其基本原理等等。vc作为一个主流的开发平台一直深受编程爱好者的喜爱,但是很多人却对它的入门感到难于上青天,究其原因主要是大家对他错误的认识造成的,严格的来说 vc+不是门语言,虽然它和c+之间有密切的关系,如果形象点比喻的话,可以c+看作为一种”工业标准”,而vc+则是某种操作系统平台下的”厂商标准”,
4、而”厂商标准”是在遵循”工业标准”的前提下扩展而来的。vc+应用程序的开发主要有两种模式2.系统设计分析2.1 协议分析smtp 独立于特定的传输子系统,且只需要可靠有序的数据流信道支持。 smtp 重要特性之一是其能跨越网络传输邮件,即“ smtp 邮件中继”。通常, 一个网络可以由公用互联网上 tcp 可相互访问的主机、防火墙分隔的 tcp/ip 网络上 tcp 可相互访问的主机,及其它 lan/wan 中的主机利用非 tcp 传输层协议组成。使用 smtp ,可实现相同网络上处理机之间的邮件传输,也可通过中继器或网关实现某处理机与其它网络之间的邮件传输。 smtp协议工作原理smtp是工
5、作在两种情况下:一是电子邮件从客户机传输到服务器:二是从某一个服务器传输到另一个服务器。smtp也是个请求/响应协议,命令和响应都是基于asc文本,并以cr和lf符结束。响应包括一个表示返回状态的三位数字代码。smtp在tcp协议25号端口监听连续请求。 连接和发送过程如下: (1)建立tcp 连接。 (2)客户端发送helo命令以标识发件人自己的身份,然后客户端发送mail命令;服务器端正希望以ok作为响应,表明准备接收。 (3)客户端发送rcpt命令,以标识该电子邮件的计划接收人,可以有多个rcpt行;服务器端则表示是否愿意为收件人接收邮件。 (4)协商结束,发送邮件,用命令data发送。
6、 (5)以“.”号表示结束输入内容一起发送出去,结束此次发送,用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:oninitdialog();/ add about.
7、 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 this automatically/ wh
8、en 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 unless you set the fo
9、cus 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(m_listener.accept(m
10、_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:onreceive() char buff655
11、36; char local_host80; int nread; memset(buff,0,65536);/清空缓冲区接收数据 nread = m_server.receive(buff, 65536); /根据读到的长度 switch (nread) case 0: m_server.close(); break; case socket_error: if (getlasterror() != wsaewouldblock) afxmessagebox (error occurred); m_server.close(); break; default: buffnread =0; /
12、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 = _t(250 message accep
13、ted 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.insertstring(-1,(lp
14、ctstr)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: )+sztemp; /m_list_smtp
15、.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; sztemp = _t(c: )+sztemp;
16、/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 = buff; sztemp = _t(c:
17、)+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; if(strnicmp(buff,quit
18、,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); isshow = true; if (strni
19、cmp(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.insertstring(-1,sztemp); sztemp
20、.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.insertstring(-1,szt
21、emp); 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(s: )+sztemp; m_list
22、_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.format(listening on port
23、%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 handler code hereboo
24、l 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() != wsaewouldblock) tchar
25、szerror256; wsprintf(szerror, 监听失败: %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.close();m_server.close
26、();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_chars = abcdefghijklmnop
27、qrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/;std:string base64:base64_encode(const std:string &s) return base64_encode(const unsigned char *)s.c_str(), s.length();/解码函数的实现/std:string base64:base64_decode(unsigned char const* encoded_string, unsigned int in_len) int i = 0; int j = 0; int in_ = 0;
28、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_array_40 4); /将六个字节和下一
29、个六字节的前两位组成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位缓冲区的用0补足再解码 for (j = i; j 4; j+) char_array_4j = 0; for (j = 0; j 4; j+) char_array_4j = base64_chars.find(char_array_4j)
30、; 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 & 0x3f; for(i = 0; (i 4)
31、; 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个字节放在8个字节的空间里 while(i+ onreceive(
32、);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 : implementation file/#incl
33、ude 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 needed by classwizard.
34、#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)(csmtpseverdlg*)m_pwnd
35、)-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_filestatic char this_fil
36、e = _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)protected:virtual vo
37、id 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)cdialog:dodataexchange(
38、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 = _t();m_subject = _t()
39、;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(cdataexchange* pdx)c
40、dialog: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_cc);ddx_text(pdx, idc
41、_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, onbuttonsetup)/afx_msg_mapend_
42、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);/ set the icon for this dialog. the fram
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 第02讲 平行线(原卷板)
- 全国公共卫生信息化建设标准与规范 (试行)
- 2025年上海市房屋租赁合同范本模板
- 2025高速公路工程造价控制与合同管理
- 2025学校食堂承包合同样本
- 2025拼多多合作伙伴供货合同
- 2025销售顾问聘用劳动合同模板
- 2025探讨附条件赠与合同
- 艺术学博士研究之旅
- 2025合同应收账款质押范本
- 2025至2030中国射频芯片市场趋势展望及需求前景研究报告
- 应急急救知识课件
- 文综中考试卷及答案解析
- 鼠伤寒沙门菌护理查房
- 2024年江苏省南京市中考物理试卷真题(含答案)
- K30自动生成及计算试验记录
- (完整)教育心理学-各章节重点学习笔记
- 建筑行业施工期间意外伤害免责协议
- 民兵国防知识教育教案
- 毒理学研究新技术应用-深度研究
- 变压器容量计算表
评论
0/150
提交评论