From 465b264684a51f3414a304d23bab260e99815204 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Tue, 9 Nov 2021 09:43:33 -0300 Subject: [PATCH] Change setSockAddress() Co-authored-by: Ezequiel Bellver Co-authored-by: Juan Barmasch --- client.c | 8 +++----- include/client.h | 2 +- include/sockets.h | 3 +-- server.c | 4 +--- sockets.c | 9 +++++---- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/client.c b/client.c index 98dd486..58be3df 100644 --- a/client.c +++ b/client.c @@ -6,18 +6,16 @@ int main(int argc, char *argv[]) { int fd = createSocket(); struct sockaddr_in serv_addr; - setSockAddress(argc, argv, &serv_addr); - + setSockAddress(argc, argv, &serv_addr, INADDR_LOOPBACK); connectToSocket(fd, &serv_addr); - processAndSendInput(fd); + readAndSendInput(fd); closeSocket(fd); - return EXIT_SUCCESS; } -void processAndSendInput(int fd) { +void readAndSendInput(int fd) { char input[MAX_LEN] = {0}; int readChars; while ((readChars = read(STDIN_FILENO, input, MAX_LEN - 1)) != 0) { diff --git a/include/client.h b/include/client.h index 4fb89e1..767c461 100644 --- a/include/client.h +++ b/include/client.h @@ -13,6 +13,6 @@ #define MAX_LEN 100 -void processAndSendInput(int fd); +void readAndSendInput(int fd); #endif \ No newline at end of file diff --git a/include/sockets.h b/include/sockets.h index 5ed0899..d750911 100644 --- a/include/sockets.h +++ b/include/sockets.h @@ -12,12 +12,11 @@ #include "errors.h" #define PORT 8080 -#define ADDRESS "127.0.0.1" #define MAX_PEND_REQ 3 int createSocket(); void setSockOptions(int fd); -void setSockAddress(int argc, char *argv[], struct sockaddr_in * address); +void setSockAddress(int argc, char *argv[], struct sockaddr_in * address, in_addr_t inAddress); int bindAndGetSocket(int fd, struct sockaddr_in *address); void connectToSocket(int fd, struct sockaddr_in *address); void closeSocket(int fd); diff --git a/server.c b/server.c index 2c62808..cb5b5ba 100644 --- a/server.c +++ b/server.c @@ -7,8 +7,7 @@ int main(int argc, char *argv[]) { setSockOptions(fd); struct sockaddr_in address; - setSockAddress(argc, argv, &address); - + setSockAddress(argc, argv, &address, INADDR_ANY); int fdAux = bindAndGetSocket(fd, &address); if (setvbuf(stdout, NULL, _IONBF, 0) != 0) @@ -17,7 +16,6 @@ int main(int argc, char *argv[]) { startChallenge(fdAux); closeSocket(fd); - return EXIT_SUCCESS; } diff --git a/sockets.c b/sockets.c index 7aa66e9..79d9efc 100644 --- a/sockets.c +++ b/sockets.c @@ -11,7 +11,7 @@ int createSocket() { void setSockOptions(int fd) { int opt = 1; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) printSystemError("Sockets: setsockopt()"); } @@ -39,9 +39,9 @@ void closeSocket(int fd) { printSystemError("Sockets: close()"); } -void setSockAddress(int argc, char *argv[], struct sockaddr_in * address) { +void setSockAddress(int argc, char *argv[], struct sockaddr_in * address, in_addr_t inAddress) { address->sin_family = AF_INET; - address->sin_addr.s_addr = inet_addr(ADDRESS); + address->sin_addr.s_addr = htonl(inAddress); address->sin_port = htons(PORT); int opt; @@ -51,7 +51,8 @@ void setSockAddress(int argc, char *argv[], struct sockaddr_in * address) { printError("Usage: server [-a address] [-p port]\n"); switch (opt) { case 'a': - address->sin_addr.s_addr = inet_addr(optarg); + if (inet_pton(AF_INET, optarg, &address->sin_addr) <= 0) + printError("Sockets: inet_pton()"); break; case 'p': address->sin_port = htons(atoi(optarg));