在Python中,我想socket.connect()在设置为非阻塞的套接字上使用。当我尝试这样做时,该方法总是抛出一个BlockingIOError. 当我忽略错误(如下所示)时,程序将按预期执行。当我在连接后将套接字设置为非阻塞时,没有错误。当我使用 select.select() 确保套接字可读或可写时,我仍然收到错误。
测试服务器.py
import socket
import select
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
host = socket.gethostname()
port = 1234
sock.bind((host, port))
sock.listen(5)
while True:
select.select([sock], [], [])
con, addr = sock.accept()
message = con.recv(1024).decode('UTF-8')
print(message)
Run Code Online (Sandbox Code Playgroud)
测试客户端.py
import socket
import select
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
host = socket.gethostname()
port = 1234
try:
sock.connect((host, port))
except BlockingIOError as e:
print("BlockingIOError")
msg = "--> From the client\n"
select.select([], [sock], [])
if sock.send(bytes(msg, 'UTF-8')) == len(msg):
print("sent ", …Run Code Online (Sandbox Code Playgroud) 我的linux应用程序正在执行非阻塞TCP连接系统调用,然后用于epoll_wait检测三次握手完成.有时epoll_wait返回同时为同一套接字描述符设置的POLLOUT&POLLERRevents.
我想了解TCP级别的情况.我无法按需复制它.我的猜测是,在epoll_wait我的事件循环内部的两个调用之间,我们有一个SYN + ACK/ACK/FIN序列,但我再次无法重现它.
这个问题与(非常接近)在非阻塞套接字连接中,select()始终返回1;但是,我似乎找不到我的代码步履蹒跚的地方。
我正在使用非阻塞套接字,并且想在将客户端连接到服务器以检查超时/成功时使用select()。问题是select()几乎总是立即返回1,即使我什至没有服务器在运行,也没有什么可连接的。预先感谢您的帮助,代码片段如下:
//Loop through the addrinfo structs and try to connect to the first one we can
for(p = serverinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
{
//We couldn't create the socket, try again
perror("client: socket");
continue;
}
//Set the socket to non-blocking
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
//The error was something other than non-block/in progress, …Run Code Online (Sandbox Code Playgroud) 我想建立一个非阻塞的 OpenSSL 连接
在此连接上 - 如果没有可供读取的数据,则整个程序执行流程会在 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); …Run Code Online (Sandbox Code Playgroud) nonblocking ×3
c ×2
sockets ×2
epoll ×1
linux ×1
networking ×1
openssl ×1
python ×1
select ×1
tcp ×1