~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/websockets_gethostbyname.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <errno.h>
2
 
#include <sys/types.h>
3
 
#include <sys/socket.h>
4
 
#include <netinet/in.h>
5
 
#include <arpa/inet.h>
6
 
#include <stdio.h>
7
 
#include <stdlib.h>
8
 
#include <string.h>
9
 
#include <unistd.h>
10
 
#include <sys/ioctl.h>
11
 
#if EMSCRIPTEN
12
 
#include <emscripten.h>
13
 
#endif
14
 
 
15
 
#define EXPECTED_BYTES 5
16
 
 
17
 
int SocketFD;
18
 
 
19
 
unsigned int get_all_buf(int sock, char* output, unsigned int maxsize)
20
 
{
21
 
  int bytes;
22
 
  if (ioctl(sock, FIONREAD, &bytes)) return 0;
23
 
  if (bytes == 0) return 0;
24
 
 
25
 
  char buffer[1024];
26
 
  int n;
27
 
  unsigned int offset = 0;
28
 
  while((errno = 0, (n = recv(sock, buffer, sizeof(buffer), 0))>0) ||
29
 
    errno == EINTR) {
30
 
    if(n>0)
31
 
    {
32
 
      if (((unsigned int) n)+offset > maxsize) { fprintf(stderr, "too much data!"); exit(EXIT_FAILURE); }
33
 
      memcpy(output+offset, buffer, n);
34
 
      offset += n;
35
 
    }
36
 
  }
37
 
 
38
 
  if(n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
39
 
    fprintf(stderr, "error in get_all_buf!");
40
 
    exit(EXIT_FAILURE);
41
 
  }
42
 
  return offset;
43
 
}
44
 
 
45
 
int done = 0;
46
 
 
47
 
void iter() {
48
 
  /* perform read write operations ... */
49
 
  static char out[1024*2];
50
 
  static int pos = 0;
51
 
  int n = get_all_buf(SocketFD, out+pos, 1024-pos);
52
 
  if (n) printf("read! %d\n", n);
53
 
  pos += n;
54
 
  if (pos >= EXPECTED_BYTES) {
55
 
    int i, sum = 0;
56
 
    for (i=0; i < pos; i++) {
57
 
      printf("%x\n", out[i]);
58
 
      sum += out[i];
59
 
    }
60
 
 
61
 
    shutdown(SocketFD, SHUT_RDWR);
62
 
 
63
 
    close(SocketFD);
64
 
 
65
 
    done = 1;
66
 
 
67
 
    printf("sum: %d\n", sum);
68
 
 
69
 
#if EMSCRIPTEN
70
 
    int result = sum;
71
 
    REPORT_RESULT();
72
 
#endif
73
 
  }
74
 
}
75
 
 
76
 
int main(void)
77
 
{
78
 
  struct sockaddr_in stSockAddr;
79
 
  int Res;
80
 
  SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
81
 
 
82
 
  if (-1 == SocketFD)
83
 
  {
84
 
    perror("cannot create socket");
85
 
    exit(EXIT_FAILURE);
86
 
  }
87
 
 
88
 
  memset(&stSockAddr, 0, sizeof(stSockAddr));
89
 
 
90
 
  stSockAddr.sin_family = AF_INET;
91
 
  stSockAddr.sin_port = htons(7001);
92
 
 
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");
101
 
    return 0;
102
 
  }
103
 
  printf("localhost has 'ip' of %s\n", name);
104
 
 
105
 
  Res = inet_pton(AF_INET, name, &stSockAddr.sin_addr);
106
 
 
107
 
  if (0 > Res) {
108
 
    perror("error: first parameter is not a valid address family");
109
 
    close(SocketFD);
110
 
    exit(EXIT_FAILURE);
111
 
  } else if (0 == Res) {
112
 
    perror("char string (second parameter does not contain valid ipaddress)");
113
 
    close(SocketFD);
114
 
    exit(EXIT_FAILURE);
115
 
  }
116
 
 
117
 
  if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) {
118
 
    perror("connect failed");
119
 
    close(SocketFD);
120
 
    exit(EXIT_FAILURE);
121
 
 
122
 
  }
123
 
 
124
 
#if EMSCRIPTEN
125
 
  emscripten_set_main_loop(iter, 0, 0);
126
 
#else
127
 
  while (!done) iter();
128
 
#endif
129
 
 
130
 
  return EXIT_SUCCESS;
131
 
}
132