forked from fpv-wtf/msp-osd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetwork.c
63 lines (52 loc) · 1.32 KB
/
network.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include "network.h"
int connect_to_server(char *address, int port)
{
int sockfd;
struct sockaddr_in servaddr;
// socket create and verification
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1)
{
printf("socket failed!\n");
return -1;
}
memset(&servaddr, 0, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(address);
servaddr.sin_port = htons(port);
if (connect(sockfd, &servaddr, sizeof(servaddr)) != 0)
{
printf("connection failed!\n");
return -1;
}
fcntl(sockfd, F_SETFL, O_NONBLOCK);
return sockfd;
}
int bind_socket(int port)
{
struct sockaddr_in si_me;
int s;
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
printf("Failed to get socket!\n");
return -1;
}
memset((char *)&si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(port);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
// bind socket to port
if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me)) == -1)
{
printf("Failed to get bound!\n");
return -1;
}
return s;
}