diff options
Diffstat (limited to 'tests/websockets_bi_side.c')
-rw-r--r-- | tests/websockets_bi_side.c | 103 |
1 files changed, 60 insertions, 43 deletions
diff --git a/tests/websockets_bi_side.c b/tests/websockets_bi_side.c index 1d557ed8..b8910632 100644 --- a/tests/websockets_bi_side.c +++ b/tests/websockets_bi_side.c @@ -3,10 +3,12 @@ #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> +#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <fcntl.h> #include <sys/ioctl.h> #if EMSCRIPTEN #include <emscripten.h> @@ -14,62 +16,77 @@ #define EXPECTED_BYTES 5 -int main(void) -{ - struct sockaddr_in stSockAddr; - int Res; -#if !TEST_DGRAM - int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); -#else - int SocketFD = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); -#endif +int sockfd = -1; - if (-1 == SocketFD) - { - perror("cannot create socket"); - exit(EXIT_FAILURE); - } - - memset(&stSockAddr, 0, sizeof(stSockAddr)); +void finish(int result) { + close(sockfd); + exit(result); +} - stSockAddr.sin_family = AF_INET; - stSockAddr.sin_port = htons(SOCKK); - Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr); +void loop() { + fd_set fdw; + int res; - if (0 > Res) { - perror("error: first parameter is not a valid address family"); - close(SocketFD); - exit(EXIT_FAILURE); - } else if (0 == Res) { - perror("char string (second parameter does not contain valid ipaddress)"); - close(SocketFD); - exit(EXIT_FAILURE); + // Make sure that sockfd has actually finished connecting + // and is ready to read. + FD_ZERO(&fdw); + FD_SET(sockfd, &fdw); + res = select(64, NULL, &fdw, NULL, NULL); + if (res == -1) { + perror("select failed"); + finish(EXIT_FAILURE); + } else if (!FD_ISSET(sockfd, &fdw)) { + return; } - printf("connect..\n"); + char data[] = "hello from the other siide\n"; - if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) { - perror("connect failed"); - close(SocketFD); - exit(EXIT_FAILURE); + printf("send..\n"); +#if TEST_FILE_OPS + res = write(sockfd, data, sizeof(data)); +#else + res = send(sockfd, data, sizeof(data), 0); +#endif + if (res == -1) { + if (errno != EAGAIN) { + perror("send error"); + finish(EXIT_FAILURE); + } + return; } -#if TEST_FILE_OPS - printf("write..\n"); + finish(EXIT_SUCCESS); +} - char data[] = "hello from the other siide (fileops)\n"; - write(SocketFD, data, sizeof(data)); +int main() { + struct sockaddr_in addr; + int res; +#if !TEST_DGRAM + sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); #else - printf("send..\n"); - - char data[] = "hello from the other siide\n"; - send(SocketFD, data, sizeof(data), 0); + sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); #endif + if (sockfd == -1) { + perror("cannot create socket"); + finish(EXIT_FAILURE); + } + fcntl(sockfd, F_SETFL, O_NONBLOCK); + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(SOCKK); + if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) { + perror("inet_pton failed"); + finish(EXIT_FAILURE); + } - printf("stall..\n"); + res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)); + if (res == -1 && errno != EINPROGRESS) { + perror("connect failed"); + finish(EXIT_FAILURE); + } - //int bytes; - //while (1) ioctl(SocketFD, FIONREAD, &bytes); + emscripten_set_main_loop(loop, 0, 0); return EXIT_SUCCESS; } |