3
#include <sys/socket.h>
4
#include <netinet/in.h>
10
#include <sys/ioctl.h>
12
#include <emscripten.h>
15
#define EXPECTED_BYTES 5
19
unsigned int get_all_buf(int sock, char* output, unsigned int maxsize)
22
if (ioctl(sock, FIONREAD, &bytes)) return 0;
23
if (bytes == 0) return 0;
27
unsigned int offset = 0;
28
while((errno = 0, (n = recv(sock, buffer, sizeof(buffer), 0))>0) ||
32
if (((unsigned int) n)+offset > maxsize) { fprintf(stderr, "too much data!"); exit(EXIT_FAILURE); }
33
memcpy(output+offset, buffer, n);
38
if(n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
39
fprintf(stderr, "error in get_all_buf!");
48
/* perform read write operations ... */
49
static char out[1024*2];
51
int n = get_all_buf(SocketFD, out+pos, 1024-pos);
52
if (n) printf("read! %d\n", n);
54
if (pos >= EXPECTED_BYTES) {
56
for (i=0; i < pos; i++) {
57
printf("%x\n", out[i]);
61
shutdown(SocketFD, SHUT_RDWR);
67
printf("sum: %d\n", sum);
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(7001);
93
struct hostent *host0 = gethostbyname("test.com"); // increment hostname counter to check for possible but at 0,0 not differentiating low/high
94
struct hostent *host = gethostbyname("localhost");
95
char **addr_list = host->h_addr_list;
96
int *addr = (int*)*addr_list;
97
printf("raw addr: %d\n", *addr);
98
char name[INET_ADDRSTRLEN];
99
if (!inet_ntop(AF_INET, addr, name, sizeof(name))) {
100
printf("could not figure out name\n");
103
printf("localhost has 'ip' of %s\n", name);
105
Res = inet_pton(AF_INET, name, &stSockAddr.sin_addr);
108
perror("error: first parameter is not a valid address family");
111
} else if (0 == Res) {
112
perror("char string (second parameter does not contain valid ipaddress)");
117
if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) {
118
perror("connect failed");
125
emscripten_set_main_loop(iter, 0, 0);
127
while (!done) iter();