Added argument parser and set all logs to STDERR
This commit is contained in:
10
server.c
10
server.c
@@ -34,7 +34,7 @@ bool _accept() {
|
||||
int server_open(const char* host, int port) {
|
||||
struct hostent *he = gethostbyname(host);
|
||||
if (!he) {
|
||||
printf("[!] gethostbyname failed: %d (h_errno)\n", h_errno);
|
||||
fprintf(stderr, "[!] gethostbyname failed: %d (h_errno)\n", h_errno);
|
||||
return -1;
|
||||
}
|
||||
_fd = socket(
|
||||
@@ -43,13 +43,13 @@ int server_open(const char* host, int port) {
|
||||
IPPROTO_TCP
|
||||
);
|
||||
if (_fd == -1) {
|
||||
printf("[!] socket failed: %d\n", errno);
|
||||
fprintf(stderr, "[!] socket failed: %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
const int opt = 1;
|
||||
if (setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
|
||||
close(_fd);
|
||||
printf("[!] setsockopt failed: %d\n", errno);
|
||||
fprintf(stderr, "[!] setsockopt failed: %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
struct sockaddr_in addr;
|
||||
@@ -58,12 +58,12 @@ int server_open(const char* host, int port) {
|
||||
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
|
||||
if (bind(_fd, (const void *)&addr, sizeof(addr)) == -1) {
|
||||
close(_fd);
|
||||
printf("[!] bind failed: %d\n", errno);
|
||||
fprintf(stderr, "[!] bind failed: %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
if (listen(_fd, LISTEN_BACKLOG) == -1) {
|
||||
close(_fd);
|
||||
printf("[!] listen failed: %d\n", errno);
|
||||
fprintf(stderr, "[!] listen failed: %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
return _fd;
|
||||
|
||||
Reference in New Issue
Block a user