您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > 使用socket编写pop3客户端
使用socket编写pop3客户端课程名称基础实践专业计科班级111111学号111111姓名11111撰写时间:2015年9月9日项目分析设计文档【项目构思】使用socket编写pop3客户端【项目设计】#includewinsock2.h#includestdio.h#includestdlib.h#includeconio.h#pragmacomment(lib,ws2_32.lib)#defineMAX_SIZE128//定义可以接收的最大字符串长度main(intargc,char*argv[]){//变量声明WSADATAwsaData;intsock;charhostName[128];//服务器IPstructhostent*pHost;structsockaddr_inservAddr;//服务器端地址intbyteRcvd;//recv函数接收到的字节数charrespondFromServer[MAX_SIZE];charresString[MAX_SIZE];charcommand[MAX_SIZE];charmailBuf[MAX_SIZE];inti;//加载WinsockDLLif(WSAStartup(MAKEWORD(2,2),&wsaData)!=0){printf(初始化winsock失败\n);exit(0);}//建立Socketsock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);if(sock==INVALID_SOCKET){printf(创建socket失败\n);WSACleanup();exit(0);}//从控制台输入服务器端的域名地址printf(请输入pop3服务器的域名地址:\n);scanf(%s,hostName);pHost=gethostbyname(hostName);//构造服务器端的地址结构,用于与服务器的连接memset(&servAddr,0,sizeof(servAddr));//结构体清零servAddr.sin_family=AF_INET;//servAddr.sin_addr.s_addr=inet_addr(hostIP);servAddr.sin_addr.s_addr=(*(structin_addr*)pHost-h_addr).s_addr;servAddr.sin_port=htons(110);//连接服务器if(connect(sock,(structsockaddr*)&servAddr,sizeof(servAddr))==SOCKET_ERROR){printf(连接建立失败\n);closesocket(sock);WSACleanup();exit(0);}//接收服务器响应byteRcvd=recv(sock,respondFromServer,MAX_SIZE,0);if(byteRcvd==SOCKET_ERROR){printf(连接响应失败\n);closesocket(sock);WSACleanup();exit(0);}else{if(memcmp(respondFromServer,-ERR,4)==0){printf(连接错误\n);closesocket(sock);WSACleanup();exit(0);}else{printf(服务器响应:);for(i=0;ibyteRcvd;i++){printf(%c,respondFromServer[i]);}printf(\n);}}//验证用户名memset(resString,0,sizeof(resString));//信息清空memset(command,0,MAX_SIZE);printf(请输入用户名:\n);scanf(%s,resString);memcpy(command,USER,strlen(USER));memcpy(command+strlen(USER),resString,strlen(resString));memcpy(command+strlen(USER)+strlen(resString),\r\n,2);send(sock,command,strlen(command),0);byteRcvd=recv(sock,respondFromServer,MAX_SIZE,0);if(byteRcvd==SOCKET_ERROR){printf(socket错误\n);closesocket(sock);WSACleanup();exit(0);}else{if(memcmp(respondFromServer,-ERR,4)==0){printf(用户名错误\n);closesocket(sock);WSACleanup();exit(0);}else{printf(服务器响应:);for(i=0;ibyteRcvd;i++){printf(%c,respondFromServer[i]);}printf(\n);}}//验证密码memset(resString,0,sizeof(resString));//信息清空memset(command,0,MAX_SIZE);printf(请输入密码:\n);i=0;while(resString[i]=getch()){if(resString[i]==13){printf(\n);break;}if(resString[i]!='\b'){printf(*);i++;}else{printf(\b\b);i--;}}resString[i]='\0';memcpy(command,PASS,strlen(PASS));memcpy(command+strlen(PASS),resString,strlen(resString));memcpy(command+strlen(PASS)+strlen(resString),\r\n,2);send(sock,command,strlen(command),0);byteRcvd=recv(sock,respondFromServer,MAX_SIZE,0);if(byteRcvd==SOCKET_ERROR){printf(socket错误\n);closesocket(sock);WSACleanup();exit(0);}else{if(memcmp(respondFromServer,-ERR,4)==0){printf(密码错误\n);closesocket(sock);WSACleanup();exit(0);}else{printf(服务器响应:);for(i=0;ibyteRcvd;i++){printf(%c,respondFromServer[i]);}printf(\n);}}//显示邮件数目memset(resString,0,sizeof(resString));//信息清空memset(command,0,MAX_SIZE);memcpy(command,STAT,strlen(STAT));memcpy(command+strlen(STAT),\r\n,2);printf(%s,command);send(sock,command,strlen(command),0);byteRcvd=recv(sock,respondFromServer,MAX_SIZE,0);if(byteRcvd==SOCKET_ERROR){printf(socket错误\n);closesocket(sock);WSACleanup();exit(0);}else{if(memcmp(respondFromServer,-ERR,4)==0){printf(应答错误\n);closesocket(sock);WSACleanup();exit(0);}else{printf(服务器响应:);for(i=0;ibyteRcvd;i++){printf(%c,respondFromServer[i]);}printf(\n);}}//返回指定邮件的内容/*memset(resString,0,sizeof(resString));//信息清空memset(command,0,MAX_SIZE);memcpy(command,RETR,strlen(RETR));memcpy(command+strlen(RETR),4\r\n,3);printf(%s,command);send(sock,command,strlen(command),0);byteRcvd=recv(sock,respondFromServer,MAX_SIZE,0);if(byteRcvd==SOCKET_ERROR){printf(socket错误\n);closesocket(sock);WSACleanup();exit(0);}else{if(memcmp(respondFromServer,-ERR,4)==0){printf(应答错误\n);closesocket(sock);WSACleanup();exit(0);}else{printf(服务器响应:);for(i=0;ibyteRcvd;i++){printf(%c,respondFromServer[i]);}printf(\n);}}while(1){memset(mailBuf,0,MAX_SIZE);byteRcvd=recv(sock,mailBuf,MAX_SIZE,0);if(byteRcvd==SOCKET_ERROR){printf(socket错误\n);closesocket(sock);WSACleanup();exit(0);}printf(%s\n,mailBuf);if(strstr(mailBuf,\r\n.\r\n))break;}*/closesocket(sock);WSACleanup();/*清理Winsock*/exit(0);}教师评阅得分
本文标题:使用socket编写pop3客户端
链接地址:https://www.777doc.com/doc-2687123 .html