imap客户端 - 如何撰写正确的请求?

时间:2012-12-08 10:07:04

标签: c++ sockets openssl imap winsock

我正在尝试使用winsock / OpenSSL创建简单的IMAP客户端,这可能没有额外的库吗?如果是这样,如何发送请求并获得响应?服务器只响应一次,即使没有请求,之后我发送请求和应用程序卡在SSL_read(..)函数,而不是超时和BYE。如何提出正确的请求? enter image description here

bool RequestQueue(const char* serverName)
{
    SOCKET      hSocket = INVALID_SOCKET;
    char        receiveBuf[512];
    ZeroMemory(receiveBuf,512); 
    char        requestBuf[512];
    ZeroMemory(requestBuf,512); 
    sockaddr_in sockAddr = {0};
    bool        bSuccess = true;

    //SSL
    SSL* ssl;
    SSL_CTX* ctx;

    try
    {       
        //Look up hostname and fill sockaddr_in structure
        cout<< "Looking up hostname "<<serverName<<"...";
        FillSockAddr(&sockAddr,serverName,IMAP_SERVER_PORT);
        cout<< "found.\n";

        //creating socket
        cout<<"Creating socket..";
        if((hSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == INVALID_SOCKET)
            throw exception("could not create socket!");
        cout<<"created.\n";

            //Connect to server
        cout<<"Attempting to connect to "<< inet_ntoa(sockAddr.sin_addr)
            <<":"<<IMAP_SERVER_PORT<<" ...";
        if(connect(hSocket,(sockaddr*)(&sockAddr),sizeof(sockAddr))!= 0)
            throw exception("could not connect!");

        cout<<"connected\n";

        ctx = SSL_CTX_new(SSLv23_client_method());
        if(!ctx)
            throw exception("SSL_CTX_new error");

        ssl = SSL_new(ctx);
        SSL_CTX_free(ctx);
        if(!ssl)
            throw exception("ssl initializing error");

        SSL_set_fd(ssl,hSocket);
        if(SSL_connect(ssl) != 1)
            throw exception("SSL_connect error");

        int reqLen;
        int retLen;
        cout<<"==============\n";
        cout<<"====begin=====\n";
        cout<<"==============\n";

        retLen = SSL_read(ssl,receiveBuf,sizeof(receiveBuf));
        if(retLen<0)
            throw exception("SSL_read error.");
        cout<<"S: "<<receiveBuf;

        strcpy(requestBuf,"a001 CAPABILITY");
        reqLen = strlen(requestBuf);
        SSL_write(ssl,requestBuf,reqLen);
        cout<<"C: "<<requestBuf<<endl;  

        ZeroMemory(receiveBuf,sizeof(receiveBuf));
        retLen = SSL_read(ssl,receiveBuf,sizeof(receiveBuf));
        if(retLen<0)
            throw exception("SSL_read error.");

        cout<<"S: "<<receiveBuf;                    
    }
    catch(exception e)
    {
        cout<<"Error : "<<e.what()<<endl;
    }
    if(hSocket != INVALID_SOCKET)
    {
        SSL_shutdown(ssl);
        closesocket(hSocket);
        SSL_free(ssl);      
    }

    return bSuccess;
}

1 个答案:

答案 0 :(得分:3)

我没有深入分析你的代码,但我可以看到一个明显的问题。 根据{{​​3}}:

  

客户端和服务器传输的所有交互都是以   行,即以CRLF结尾的字符串。协议接收器   IMAP4rev1客户端或服务器正在读取一行,或者是   读取具有已知计数后跟一行的八位字节序列。

在您的代码中,您不会使用CRLF终止命令。尝试替换

strcpy(requestBuf,"a001 CAPABILITY");

strcpy(requestBuf,"a001 CAPABILITY\r\n");

此外,您最好不使用SSL并在以后添加它 - 这将简化调试(即使用RFC 3501)。