// 111111.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <WinSock.h> #include <iostream> #include <string> #pragma comment(lib,"WSock32.Lib") class ZBase64 { public: std::string Encode(const unsigned char* Data, int DataByte); std::string Decode(const char* Data, int DataByte, int& OutByte); }; std::string ZBase64::Encode(const unsigned char* Data, int DataByte) { //编码表 const char EncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; //返回值 std::string strEncode; unsigned char Tmp[4] = { 0 }; int LineLength = 0; for (int i = 0; i<(int)(DataByte / 3); i++) { Tmp[1] = *Data++; Tmp[2] = *Data++; Tmp[3] = *Data++; strEncode += EncodeTable[Tmp[1] >> 2]; strEncode += EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F]; strEncode += EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F]; strEncode += EncodeTable[Tmp[3] & 0x3F]; if (LineLength += 4, LineLength == 76) { strEncode += "\r\n"; LineLength = 0; } } //对剩余数据进行编码 int Mod = DataByte % 3; if (Mod == 1) { Tmp[1] = *Data++; strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2]; strEncode += EncodeTable[((Tmp[1] & 0x03) << 4)]; strEncode += "=="; } else if (Mod == 2) { Tmp[1] = *Data++; Tmp[2] = *Data++; strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2]; strEncode += EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)]; strEncode += EncodeTable[((Tmp[2] & 0x0F) << 2)]; strEncode += "="; } return strEncode; } std::string ZBase64::Decode(const char* Data, int DataByte, int& OutByte) { //解码表 const char DecodeTable[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, // '+' 0, 0, 0, 63, // '/' 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9' 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z' 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z' }; //返回值 std::string strDecode; int nValue; int i = 0; while (i < DataByte) { if (*Data != '\r' && *Data != '\n') { nValue = DecodeTable[*Data++] << 18; nValue += DecodeTable[*Data++] << 12; strDecode += (nValue & 0x00FF0000) >> 16; OutByte++; if (*Data != '=') { nValue += DecodeTable[*Data++] << 6; strDecode += (nValue & 0x0000FF00) >> 8; OutByte++; if (*Data != '=') { nValue += DecodeTable[*Data++]; strDecode += nValue & 0x000000FF; OutByte++; } } i += 4; } else// 回车换行,跳过 { Data++; i++; } } return strDecode; } //=============================== class SocketInit { public: SocketInit() { WSADATA wsaData; int err; err = WSAStartup(MAKEWORD(2,2), &wsaData); if (err) { std::cerr << "socket init error " << err << std::endl; exit(err); } } ~SocketInit() { WSACleanup(); } }; SocketInit gSockInit; const int BUF_MAX_LEN = 1024*10; const char* strUser = ""; //邮箱用户名 const char* strPassWord = ""; //邮箱密码 int main() { // base64 解码示例 std::string mailStr(R"(PGRpdiBzdHlsZT0ibGluZS1oZWlnaHQ6MS43O2NvbG9yOiMwMDAwMDA7Zm9udC1zaXplOjE0cHg7 Zm9udC1mYW1pbHk6QXJpYWwiPjxzcGFuIHN0eWxlPSJjb2xvcjogcmdiKDQ0LCA0NCwgNDQpOyBm b250LWZhbWlseTogy87M5SwgJnF1b3Q7QXJpYWwgTmFycm93JnF1b3Q7LCBhcmlhbCwgc2VyaWY7 IGxpbmUtaGVpZ2h0OiAyOHB4OyI+16LS4snPw+bK1dPKvP61xLn9s8yyorK70uLOttfF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DJvrP9seq8x6Osy/nS1MTjsrvQ6NKqzOG5qdPKvP7L99L9usW4+NXiuPbD/MHu oaMmbmJzcDs8L3NwYW4+PC9kaXY+PGJyPjxicj48c3BhbiB0aXRsZT0ibmV0ZWFzZWZvb3RlciI+ PGRpdiBpZD0ibmV0ZWFzZV9tYWlsX2Zvb3RlciI+PGRpdiBzdHlsZT0iYm9yZGVyLXRvcDojQ0ND IDFweCBzb2xpZDtwYWRkaW5nOjEwcHggNXB4O2ZvbnQtc2l6ZToxNXB4O2NvbG9yOiM3Nzc7bGlu ZS1oZWlnaHQ6MjJweCI+08rP5Ljvw/zAtMHLo6Gjoc340tcxNjOhojEyNtPKz+S/ydLUt6LLzTNH 0tTJz7OstPPOxLz+o6E8L2Rpdj48L2Rpdj48L3NwYW4+)"); ZBase64 t; int i; std::string sOut = t.Decode(mailStr.c_str(), mailStr.size(), i); std::cout << sOut << std::endl; //========================================== SOCKET mySock = socket(AF_INET, SOCK_STREAM, 0);//创建了可识别套接字 struct hostent *remoteHost; struct in_addr addr; if ((remoteHost = gethostbyname("pop.126.com")) == NULL) exit(1); if (remoteHost->h_addrtype == AF_INET) { addr.s_addr = *(u_long *)remoteHost->h_addr_list[0]; printf("IP Address #%d: %s\n", 0, inet_ntoa(addr)); } SOCKADDR_IN popServer; popServer.sin_addr = addr; popServer.sin_family = AF_INET; popServer.sin_port = htons(110); // step 1 connect int ret = connect(mySock, (SOCKADDR*)&popServer, sizeof(SOCKADDR)); if (ret) exit(ret); char receiveBuf[BUF_MAX_LEN] = {0}; ret = recv(mySock, receiveBuf, BUF_MAX_LEN,0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf ; // step 2 sendUser std::string SendMessage("USER "); SendMessage += strUser; SendMessage += "\r\n"; ret = send(mySock, SendMessage.c_str(), SendMessage.size(),0); if (SOCKET_ERROR == ret) exit(ret); ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf; //stecp 3 sendPass SendMessage.clear(); SendMessage +="PASS "; SendMessage += strPassWord; SendMessage += "\r\n"; ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0); if (SOCKET_ERROR == ret) exit(ret); ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf; //stecp 4 test LIST SendMessage.clear(); SendMessage += "LIST\r\n"; ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0); if (SOCKET_ERROR == ret) exit(ret); ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); //std::cout << receiveBuf; std::cout << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl; /* //stecp 5 test DELE SendMessage.clear(); SendMessage += "DELE 2\r\n"; ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0); if (SOCKET_ERROR == ret) exit(ret); //ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf; //ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); //if (SOCKET_ERROR == ret) //exit(ret); //std::cout << receiveBuf; */ //stecp 6 test RETR SendMessage.clear(); SendMessage += "RETR 1\r\n"; ret = send(mySock, SendMessage.c_str(), SendMessage.size(), 0); if (SOCKET_ERROR == ret) exit(ret); ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf; std::cout << std::endl; ret = recv(mySock, receiveBuf, BUF_MAX_LEN, 0); if (SOCKET_ERROR == ret) exit(ret); std::cout << receiveBuf; std::cout << std::endl; closesocket(mySock); return 0; }
参考此博客
http://yerik.blog.51cto.com/1662422/743998