-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind.c
More file actions
29 lines (26 loc) · 657 Bytes
/
bind.c
File metadata and controls
29 lines (26 loc) · 657 Bytes
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
#include "bind.h"
#include <signal.h>
#include <string.h>
#include <arpa/inet.h>
int create_listen_socket(uint16_t port) {
signal(SIGPIPE, SIG_IGN);
if (port == 0) {
return -1;
}
struct sockaddr_in addr;
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0) {
return -2;
}
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htons(INADDR_ANY);
addr.sin_port = htons(port);
if (bind(listenfd, (struct sockaddr *) &addr, sizeof addr) < 0) {
return -3;
}
if (listen(listenfd, 500) < 0) {
return -4;
}
return listenfd;
}