Change setSockAddress()

Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
This commit is contained in:
Santiago Lo Coco 2021-11-09 09:43:33 -03:00
parent 77f3ccf2eb
commit 465b264684
5 changed files with 11 additions and 15 deletions

View File

@ -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) {

View File

@ -13,6 +13,6 @@
#define MAX_LEN 100
void processAndSendInput(int fd);
void readAndSendInput(int fd);
#endif

View File

@ -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);

View File

@ -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;
}

View File

@ -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));