forked from xaelsouth/rtl-wmbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_support.h
47 lines (35 loc) · 966 Bytes
/
net_support.h
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
#ifndef NET_SUPPORT_H
#define NET_SUPPORT_H
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
FILE * get_net(const char *hostname, int port);
FILE * get_net(const char *hostname, int port)
{
struct sockaddr_in address = {.sin_family = AF_INET, .sin_port = htons(port)};
if (-1 == inet_aton(hostname, &address.sin_addr))
{
fprintf(stderr, "inet_aton() error\n");
return NULL;
}
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
fprintf(stderr, "socket() error\n");
return NULL;
}
if (connect(fd, (struct sockaddr *) &address, sizeof (address)) == -1)
{
fprintf(stderr, "connect() error: %s:%d\n", hostname, port);
return NULL;
}
FILE *input = fdopen(fd, "rb");
if (input == NULL)
{
fprintf(stderr, "fdopen() error\n");
}
return input;
}
#endif /* NET_SUPPORT_H */