-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_multisock.c
49 lines (42 loc) · 949 Bytes
/
test_multisock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <multisock.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int
main(int argc, char **argv)
{
multisock_t sock;
int sockfd;
sock = multisock_create(MULTISOCK_ATTR_DEFAULT);
if (sock == MULTISOCK_CREATE_FAILED) {
perror("multisock_socket");
exit(EXIT_FAILURE);
}
sockfd = multisock_connect_name(sock, "github.com", "http");
{
const char request[] = "GET / HTTP/1.0\r\nHost: github.com\r\n\r\n";
const char *start = request;
const char *end = request + strlen(request);
size_t size;
while ((size = send(sockfd, start, end - start, 0)) > 0)
start += size;
if (size == -1) {
perror("write");
exit(EXIT_SUCCESS);
}
}
while (1) {
char buffer[256] = { 0 };
int size;
size = recv(sockfd, buffer, sizeof buffer - 1, 0);
switch (size) {
case -1:
perror("read");
exit(EXIT_SUCCESS);
case 0:
exit(EXIT_FAILURE);
default:
printf("%d: <<<%s>>>\n", size, buffer);
}
}
}