3
#include <sys/socket.h>
4
#include <netinet/in.h>
10
#include <sys/ioctl.h>
13
#include <emscripten.h>
16
#define EXPECTED_BYTES 5
22
void iter(void *arg) {
23
static char readbuf[1024];
24
static int readPos = 0;
28
FD_SET(SocketFD, &sett);
32
int selectRes = select(64, &sett, NULL, NULL, NULL);
37
if( selectRes == -1 ){
38
perror( "Connection to websocket server failed" );
42
assert(FD_ISSET(SocketFD, &sett));
44
int bytesRead = recv( SocketFD, readbuf+readPos, 7-readPos, 0 );
48
// here the server should have closed the connection
49
int selectRes = select(64, &sett, NULL, NULL, NULL);
54
if( selectRes == -1 ){
55
perror( "Connection to websocket server failed as expected" );
58
emscripten_cancel_main_loop();
63
printf( "Error: socket should not show up on select call anymore.\n" );
71
// Scenario: the server sends data and closes the connection after 7 bytes.
72
// This test should provoke the situation in which the underlying
73
// tcp connection has been torn down already but there is still data
74
// in the inQueue. The select call has to succeed as long the queue
75
// still contains data and only then start to throw errors.
78
struct sockaddr_in stSockAddr;
80
SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
84
perror("cannot create socket");
88
memset(&stSockAddr, 0, sizeof(stSockAddr));
90
stSockAddr.sin_family = AF_INET;
91
stSockAddr.sin_port = htons(
98
Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);
101
perror("error: first parameter is not a valid address family");
104
} else if (0 == Res) {
105
perror("char string (second parameter does not contain valid ipaddress)");
110
// This call should succeed (even if the server port is closed)
111
if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) {
112
perror("connect failed");
119
emscripten_set_main_loop(iter, 0, 0);
121
while (!done) iter(NULL);