#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include int main(void) { const struct sockaddr_in connect_addr = {.sin_family = AF_INET, .sin_addr = htonl(INADDR_LOOPBACK), .sin_port = htons(9374)}; int buffer_size = 1024 * 1024; int listener, server, client, ret, error; struct sockaddr_in addr = {0}; struct linger linger; struct pollfd pollfd; unsigned int i; pthread_t thread; socklen_t len; char *buffer = malloc(buffer_size); listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); ret = getsockname(listener, (struct sockaddr *)&addr, &len); printf("getsockname -> %d, {%u, %#x}, %s\n", ret, addr.sin_family, addr.sin_addr.s_addr, strerror(errno)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("127.0.0.1"); ret = bind(listener, (struct sockaddr *)&addr, sizeof(addr)); if (ret < 0) printf("bind -> %d, %s\n", ret, strerror(errno)); len = sizeof(addr); ret = getsockname(listener, (struct sockaddr *)&addr, &len); if (ret < 0) printf("getsockname -> %d, %s\n", ret, strerror(errno)); ret = listen(listener, 1); if (ret < 0) printf("listen -> %d, %s\n", ret, strerror(errno)); ret = connect(client, &addr, sizeof(addr)); if (ret < 0) printf("connect -> %d, %s\n", ret, strerror(errno)); server = accept4(listener, (struct sockaddr *)&addr, &len, SOCK_NONBLOCK); if (server < 0) printf("accept -> %d, %s\n", server, strerror(errno)); ret = send(client, "A", 1, MSG_OOB); if (ret != 1) printf("send -> %d, %s\n", ret, strerror(errno)); ret = recv(server, buffer, buffer_size, MSG_OOB); if (ret != 1) printf("recv -> %d, %s\n", ret, strerror(errno)); if (0) { ret = recv(server, buffer, buffer_size, 0); if (ret != 1) printf("recv #3 -> %d, %s\n", ret, strerror(errno)); } pollfd.fd = server; pollfd.events = POLLIN | POLLPRI; ret = poll(&pollfd, 1, 0); printf("poll -> %d, %#x\n", ret, pollfd.revents); ret = 1; ret = setsockopt(server, SOL_SOCKET, SO_OOBINLINE, &ret, sizeof(ret)); if (ret < 0) printf("setsockopt -> %d, %s\n", ret, strerror(errno)); pollfd.fd = server; pollfd.events = POLLIN | POLLPRI; ret = poll(&pollfd, 1, 0); printf("poll -> %d, %#x\n", ret, pollfd.revents); ret = recv(server, buffer, buffer_size, 0); if (ret != 1) printf("recv #3 -> %d, %s\n", ret, strerror(errno)); return 0; }