Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何建立无阻塞的OpenSSL连接?_C_Openssl_Nonblocking - Fatal编程技术网

C 如何建立无阻塞的OpenSSL连接?

C 如何建立无阻塞的OpenSSL连接?,c,openssl,nonblocking,C,Openssl,Nonblocking,我想建立一个非阻塞的OpenSSL连接 connection *sslConnect (void) { connection *c; c = malloc (sizeof (connection)); c->sslHandle = NULL; c->sslContext = NULL; c->socket = tcpConnect (); if (c->socket) { // Register the error s

我想建立一个非阻塞的OpenSSL连接

connection *sslConnect (void)
{
   connection *c;

   c = malloc (sizeof (connection));
   c->sslHandle = NULL;
   c->sslContext = NULL;

   c->socket = tcpConnect ();
   if (c->socket)
   {
    // Register the error strings for libcrypto & libssl
    SSL_load_error_strings ();
    // Register the available ciphers and digests
    SSL_library_init ();

    // New context saying we are a client, and using SSL 2 or 3
    c->sslContext = SSL_CTX_new (SSLv23_client_method ());
    if (c->sslContext == NULL)
    ERR_print_errors_fp (stderr);

    // Create an SSL struct for the connection
    c->sslHandle = SSL_new (c->sslContext);
    if (c->sslHandle == NULL)
    ERR_print_errors_fp (stderr);

    // Connect the SSL struct to our connection
    if (!SSL_set_fd (c->sslHandle, c->socket))
    ERR_print_errors_fp (stderr);

    // Initiate SSL handshake
    if (SSL_connect (c->sslHandle) != 1)
    ERR_print_errors_fp (stderr);
    }
    else
    {
     perror ("Connect failed");
    }

    return c;
}
在此连接上-如果没有可读取的数据,则整个程序执行流在SSL_read()上停止。我想这样,如果没有数据可供读取,给我返回值,如want_read,我知道没有更多可用数据

char *sslRead (connection *c)
{
const int readSize = 1024;
char *rc = NULL;
int r;
int received, count = 0;
int ReallocSize = 0;
char buffer[1024];

if (c)
{
    while (1)
    {
        if (!rc)
        {
            rc = malloc (readSize + 1); 
            if (rc == NULL)
                printf("the major error have happen. leave program\n");
        }
        else
        {
            ReallocSize = (count + 1) * (readSize + 1);
            rc = realloc (rc, ReallocSize);
        }

        // if i have no data available for read after reading data, 
        // this call will not return anything and wait for more data

        // i want change this non blocking connections
        received = SSL_read (c->sslHandle, buffer, readSize);

        buffer[received] = '\0';


        if (received <= 0)
        {
            printf(" received equal to or less than 0\n");
            switch (SSL_get_error(c->sslHandle, r))
            {
            case SSL_ERROR_NONE:
                printf("SSL_ERROR_NONE\n");
                break;   
            case SSL_ERROR_ZERO_RETURN: 
                printf("SSL_ERROR_ZERO_RETURN\n");
                break;   
            case SSL_ERROR_WANT_READ: 
                printf("SSL_ERROR_WANT_READ\n");
                break;
            default:
                printf("error happens %i\n", r); 
            }     
            break;
        }

        count++;
    }
}
return rc;

多谢各位

创建非阻塞套接字是非阻塞连接的先决条件

以下步骤总结:(请参阅下面链接的站点中的完整描述)

1)调用fcntl()API将套接字描述符的当前标志设置检索到局部变量中

2)在该局部变量中,将O_非阻塞(非阻塞)标志设置为on。(注意不要篡改其他标志)

3)调用fcntl()API,将描述符的标志设置为局部变量中的值

()

假设有一个现有套接字,下面将实现上述步骤:

BOOL SetSocketBlockingEnabled(SOCKET fd, BOOL blocking)
{
     if (fd < 0) return FALSE;  
   #ifdef WIN32
       unsigned long mode = blocking ? 0 : 1;
       return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? TRUE : FALSE;
   #else
       int flags = fcntl(fd, F_GETFL, 0);
       if (flags < 0) return false;
       flags = blocking ? (flags&~O_NONBLOCK) : (flags|O_NONBLOCK);
       return (fcntl(fd, F_SETFL, flags) == 0) ? TRUE : FALSE;
   #endif
}
BOOL SetSocketBlockingEnabled(套接字fd,BOOL阻塞)
{
如果(fd<0)返回FALSE;
#ifdef WIN32
无符号长模式=阻塞?0:1;
返回(ioctlsocket(fd、FIONBIO和mode)==0)?真:假;
#否则
int flags=fcntl(fd,F_GETFL,0);
如果(标志<0)返回false;
标志=阻塞?(标志和非阻塞):(标志非阻塞);
返回(fcntl(fd,F_SETFL,flags)==0)?真:假;
#恩迪夫
}

一旦有了非阻塞套接字,请参阅解释如何进行非阻塞连接,因为我需要添加一些链接。对不起,没问题,朋友,我现在就试试,并向您汇报,谢谢您可能的和的副本。