mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 11:37:35 +00:00
acd6c32184
This change should fix the Windows issues Qt Creator has been having, by ensuring accept() and accept4() work in O_NONBLOCK mode. I switched away from AcceptEx() which is buggy, back to using WSAAccept(). This requires making a tradeoff where we have to accept a busy loop. However it is low latency in nature, just like our new and improved Windows poll() code. I was furthermore able to eliminate a bunch of Windows-related test todos.
71 lines
1.6 KiB
C
71 lines
1.6 KiB
C
#include <arpa/inet.h>
|
|
#include <cosmo.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <netinet/in.h>
|
|
#include <poll.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
int main() {
|
|
|
|
// Create server socket
|
|
int server_fd;
|
|
struct sockaddr_in address;
|
|
int addrlen = sizeof(address);
|
|
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
|
return 1;
|
|
address.sin_family = AF_INET;
|
|
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
address.sin_port = 0; // let os assign random port
|
|
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)))
|
|
return 2;
|
|
if (getsockname(server_fd, (struct sockaddr *)&address,
|
|
(socklen_t *)&addrlen))
|
|
return 3;
|
|
if (listen(server_fd, SOMAXCONN))
|
|
return 4;
|
|
|
|
{
|
|
// poll server
|
|
struct pollfd fds[2] = {
|
|
{server_fd, POLLIN | POLLOUT},
|
|
};
|
|
int ret = poll(fds, 1, 0);
|
|
if (ret != 0)
|
|
return 5;
|
|
}
|
|
|
|
// create client socket
|
|
int client_fd;
|
|
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
|
return 6;
|
|
if (connect(client_fd, (struct sockaddr *)&address, sizeof(address)))
|
|
return 7;
|
|
|
|
// accept client
|
|
int server_client_fd;
|
|
if ((server_client_fd = accept4(server_fd, 0, 0, SOCK_NONBLOCK)) == -1)
|
|
return 8;
|
|
|
|
// check that it's non-blocking
|
|
char buf[1];
|
|
if (read(server_client_fd, buf, 1) != -1)
|
|
return 9;
|
|
if (errno != EAGAIN && errno != EWOULDBLOCK)
|
|
return 10;
|
|
|
|
// Clean up
|
|
if (close(server_client_fd))
|
|
return 12;
|
|
if (close(client_fd))
|
|
return 13;
|
|
if (close(server_fd))
|
|
return 14;
|
|
|
|
CheckForMemoryLeaks();
|
|
}
|