首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于epoll的非阻塞ssl_read()卡在循环中

基于epoll的非阻塞ssl_read()卡在循环中
EN

Stack Overflow用户
提问于 2014-05-07 12:38:22
回答 2查看 2.9K关注 0票数 1

我使用以下代码作为指南,并修改了下面的代码。ssl/server.c ssl/client.c

我修改了服务器端代码如下:

代码语言:javascript
复制
do {
count = SSL_read (ssl,  buf, sizeof(buf)); // get request
switch (SSL_get_error (ssl, count) ) {
    case SSL_ERROR_NONE: 
            buf[count] = 0;
            printf("Client msg: \"%s\"\n", buf);
            sprintf(reply, HTMLecho, buf);   // construct reply
            SSL_write(ssl, reply, strlen(reply)); // send reply
            break;
    case SSL_ERROR_WANT_READ:
    case SSL_ERROR_WANT_WRITE:
            continue;
    case SSL_ERROR_ZERO_RETURN:         
            ERR_print_errors_fp(stderr);
            printf("Performing exchange Error 2.\n");
            done = 1;
            break;
    default:
            ERR_print_errors_fp(stderr);
            printf("Performing exchange Error 3.\n");
            done = 1;
            break;
  }
} while ( ssl && count > 0 );  // SSL_pending(ssl) seems unreliable

在客户端,我有如下代码:

代码语言:javascript
复制
SSL_library_init();
ctx = InitCTX();
LoadCertificates(ctx, CertFile, KeyFile);
server = OpenConnection(hostname, atoi(portnum));
ssl = SSL_new(ctx); /* create new SSL connection state */
SSL_set_fd(ssl, server); /* attach the socket descriptor */
    if ( SSL_connect(ssl) == FAIL ) /* perform the connection */
    {
        ERR_print_errors_fp(stderr);
    } else {
    while(1){
        char *msg = "Hello??? are you there. lolololololololoooooooooooooooooooooooooooo";
        printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
        ShowCerts(ssl); /* get any certs */
        SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */
        bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */
        buf[bytes] = 0;
        printf("Received: \"%s\"\n", buf);
        sleep(1);
        }
        SSL_free(ssl); /* release connection state */
    }
close(server); /* close socket */
SSL_CTX_free(ctx); /* release context */

我观察到服务器端的循环被卡住了。如有任何指导,将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-12 03:31:25

下面的代码对我是有用的。

代码语言:javascript
复制
                count = SSL_read(ssl, buf, sizeof(buf)); // get request
                int32_t ssl_error = SSL_get_error (ssl, count);
                switch (ssl_error) {
                case SSL_ERROR_NONE: 
                            printf("SSL_ERROR_NONE\n");
                            break;
                case SSL_ERROR_WANT_READ:
                            printf("SSL_ERROR_WANT_READ\n");
                            break;
                case SSL_ERROR_WANT_WRITE:
                            printf("SSL_ERROR_WANT_WRITE\n");
                            break;
                case SSL_ERROR_ZERO_RETURN:
                            printf("SSL_ERROR_ZERO_RETURN\n");  
                            break;
                default:
                            break;
                 }

                if (( count > 0 ) && (ssl_error == SSL_ERROR_NONE))
                {
                    buf[count] = 0;
                    printf("count > 0 Client msg: \"%s\"\n", buf);
                    sprintf(reply, HTMLecho, buf);   // construct reply
                    SSL_write(ssl, reply, strlen(reply)); // send reply
                } else if ((count < 0)  && (ssl_error == SSL_ERROR_WANT_READ)){
                    printf("count < 0 \n");
                    if (errno != EAGAIN)
                    {
                        printf("count < 0 errno != EAGAIN \n");
                        perror ("read");
                        done = 1;
                    }
                    break;
                } else if (count==0){
                    ERR_print_errors_fp(stderr);
                    printf("count == 0 Client Disconnected.\n");
                    done = 1;
                    break;
                }
票数 0
EN

Stack Overflow用户

发布于 2014-05-07 12:49:56

你的循环很明显。如果没有任何可读取的内容,则SSL_read返回零,错误代码为SSL_ERROR_WANT_READ,然后执行返回到ssl_readcontinue

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23518079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档