gpt4 book ai didi

c - OpenSSL 非阻塞套接字 SSL_read() 不可预测

转载 作者:太空宇宙 更新时间:2023-11-04 05:54:18 32 4
gpt4 key购买 nike

我正在尝试使用 OpenSSL 创建一个非阻塞套接字,这样我就可以遍历 SSL_read() 直到没有更多数据剩余,然后中断循环。经过大量工作,我终于使连接正常工作,但现在对于前一千多次迭代,SSL_read() 将返回 -1。之后,它会给我套接字上的实际数据。 SSL_read() 也没有返回准确的读取字节数,它总是说 -1,即使它正在读取正确的字节。我能够通过阻塞套接字让它完美地工作,但非阻塞似乎有问题......

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

if (c)
{
while (1)
{

received = SSL_read (c->sslHandle, buffer, readSize);

buffer[received] = '\0';

TotalReceived += received;
printf("Buffsize - %i - %s \n", received, buffer);


if (received <= 0)
{

// this line added per advice of Eric Tsui but does not
// change behaviour
received = SSL_read (c->sslHandle, buffer, readSize);



//printf(" received equal to or less than 0\n")
switch (SSL_get_error(c->sslHandle, received))
{
case SSL_ERROR_NONE:
{
printf("SSL_ERROR_NONE %i\n", count);
//if (received != -1)
// goto END;
break;
}
case SSL_ERROR_ZERO_RETURN:
{
printf("SSL_ERROR_ZERO_RETURN %i\n", count);
goto END;
break;
}
case SSL_ERROR_WANT_READ:
{
printf("SSL_ERROR_WANT_READ %i\n", count);

break;
}
case SSL_ERROR_WANT_WRITE:
{
printf("SSL_ERROR_WANT_WRITE %i\n", count);
goto END;
//break;
}
default:
{
printf("error %i\n", received);
break;
}
}


count++;
}
}
}
END:
return rc;
}

如果您以前这样做过,如果您能告诉我这段代码有什么问题,我将不胜感激。谢谢。

最佳答案

您的循环调用 SSL_read() 的次数过多,并且当确实没有更多数据可读取时(SSL_ERROR_WANT_READ 条件),您的错误处理并未中断循环).尝试更像这样的东西:

char *sslRead (connection *c)
{
const int readSize = 1024;
char *rc = NULL;
int received, count = 0;
int TotalReceived = 0;
fd_set fds;
struct timeval timeout;
char buffer[1024];

if (c)
{
while (1)
{
received = SSL_read (c->sslHandle, buffer, readSize);
if (received > 0)
{
TotalReceived += received;
printf("Buffsize - %i - %.*s \n", received, received, buffer);
}
else
{
count++;

//printf(" received equal to or less than 0\n")
int err = SSL_get_error(c->sslHandle, received);
switch (err)
{
case SSL_ERROR_NONE:
{
// no real error, just try again...
printf("SSL_ERROR_NONE %i\n", count);
continue;
}

case SSL_ERROR_ZERO_RETURN:
{
// peer disconnected...
printf("SSL_ERROR_ZERO_RETURN %i\n", count);
break;
}

case SSL_ERROR_WANT_READ:
{
// no data available right now, wait a few seconds in case new data arrives...
printf("SSL_ERROR_WANT_READ %i\n", count);

int sock = SSL_get_rfd(c->sslHandle);
FD_ZERO(&fds);
FD_SET(sock, &fds);

timeout.tv_sec = 5;
timeou.tv_nsec = 0;

err = select(sock+1, &fds, NULL, NULL, &timeout);
if (err > 0)
continue; // more data to read...

if (err == 0) {
// timeout...
} else {
// error...
}

break;
}

case SSL_ERROR_WANT_WRITE:
{
// socket not writable right now, wait a few seconds and try again...
printf("SSL_ERROR_WANT_WRITE %i\n", count);

int sock = SSL_get_wfd(c->sslHandle);
FD_ZERO(&fds);
FD_SET(sock, &fds);

timeout.tv_sec = 5;
timeou.tv_nsec = 0;

err = select(sock+1, NULL, &fds, NULL, &timeout);
if (err > 0)
continue; // can write more data now...

if (err == 0) {
// timeout...
} else {
// error...
}

break;
}

default:
{
printf("error %i:%i\n", received, err);
break;
}
}

break;
}
}
}

return rc;
}

关于c - OpenSSL 非阻塞套接字 SSL_read() 不可预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31171396/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com