3
#include <sys/socket.h>
4
#include <netinet/in.h>
11
#include <sys/ioctl.h>
14
#include <emscripten.h>
17
#define EXPECTED_BYTES 5
21
void finish(int result) {
29
void iter(void *arg) {
30
static int retries = 0;
34
FD_SET(sockfd, &sett);
36
// currently, we've connected to a closed server port.
37
// the initial async connect "succeeded" and select
38
// should say that the socket is ready for a non-blocking
39
// read, however, the read should be 0 sized signalling
40
// that the remote end has closed.
41
int handles = select(64, &sett, NULL, NULL, NULL);
43
perror("select failed");
47
if (FD_ISSET(sockfd, &sett)) {
49
int n = recv(sockfd, buffer, sizeof(buffer), 0);
50
if (n == -1 && retries++ > 10) {
51
perror("recv failed");
54
perror("Connection to websocket server failed as expected.");
60
// This is for testing a websocket connection to a closed server port.
61
// The connect call will succeed (due to the asynchronous websocket
62
// behavior) but once the underlying websocket system realized that
63
// the connection cannot be established, the next select call will fail.
65
struct sockaddr_in addr;
68
sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
70
perror("cannot create socket");
73
fcntl(sockfd, F_SETFL, O_NONBLOCK);
75
memset(&addr, 0, sizeof(addr));
76
addr.sin_family = AF_INET;
77
addr.sin_port = htons(SOCKK);
78
if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1) {
79
perror("inet_pton failed");
83
// This call should succeed (even if the server port is closed)
84
res = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
85
if (res == -1 && errno != EINPROGRESS) {
86
perror("connect failed");
91
emscripten_set_main_loop(iter, 0, 0);