bcssocket/server.c

60 lines
1.6 KiB
C

#include "include/server.h"
int clientFd;
challenge_t challenges[] = {
{"A volar bobby", "Cómo es que matan?", "IZPUAN"},
{"A cagar bobby", "A una niña", "PUANIZ"}
};
int main(int argc, char *argv[]) {
int fd;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
printSystemError("Server: socket()");
int opt = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))
printSystemError("Server: setsockopt()");
struct sockaddr_in address;
address.sin_family = AF_INET;
// address.sin_addr.s_addr = INADDR_ANY;
address.sin_addr.s_addr = inet_addr(ADDRESS);
address.sin_port = htons(PORT);
if (bind(fd, (struct sockaddr *) &address, sizeof(address)) < 0)
printSystemError("Server: bind()");
if (listen(fd, MAX_PEND_REQ) < 0)
printSystemError("Server: listen()");
int addrlen = sizeof(address);
if ((clientFd = accept(fd, (struct sockaddr *) &address, (socklen_t *) &addrlen)) < 0)
printSystemError("Server: accept()");
startChallenge();
close(clientFd);
close(fd);
return EXIT_SUCCESS;
}
void startChallenge() {
int challengeCount = 0;
char ansChallenge = 1, output[MAX_LEN] = {0};
while (challengeCount < MAX_CHALLENGES && ansChallenge) {
challenge_t currentChallenge = challenges[challengeCount];
printf("\033[1;1H\033[2J"); // Limpiamos shell
if ((ansChallenge = genChallenge(clientFd, output, currentChallenge))) {
challengeCount++;
}
else {
printf("Incorrecta\n");
sleep(TIME_SLEEP);
}
}
return;
}