Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/wait.h>
- #include <signal.h>
- #include <time.h>
- #include <pthread.h>
- #define MSG_LEN 256
- #define MAX_K 65535
- #define PLAYERS 4
- void die(char * f)
- {
- perror(f);
- exit(1);
- }
- struct sockaddr_in setup_addr(int port)
- {
- struct sockaddr_in server_address;
- server_address.sin_family = AF_INET;
- server_address.sin_port = htons(port);
- server_address.sin_addr.s_addr =
- htonl(INADDR_ANY); // htonl seems unecessary
- return(server_address);
- }
- void * play_game(void * s)
- {
- int ** clients = (int **) s;
- srand (time(NULL));
- int k = rand() % MAX_K;
- int tries = 3;
- char *messages[6] = {"PLUS \n","MOINS \n","GAGNE\n","PERDU\n", "DEBUT\n",
- "K?:\n"};
- int turn = 0;
- for (int i = 0; i < PLAYERS; i++)
- {
- printf("s%d: %d\n", i, *clients[i]);
- }
- for (int i = 0; i < PLAYERS; i++)
- send(*clients[i], messages[4], strlen(messages[4]), 0);
- while (tries != 0)
- {
- turn %= PLAYERS;
- int guess;
- char msg[MSG_LEN];
- if (send(*clients[turn], messages[5], strlen(messages[5]), 0) < 0)
- die("send");
- if ((recv(*clients[turn], &msg, MSG_LEN, 0) < 0)) die("recv");
- guess = atoi(msg);
- printf("Player: %d\n", turn);
- printf("Josef K.: %d\n", k);
- printf("message: %d\n", guess);
- if (guess < k) {
- if (turn == PLAYERS - 1)
- --tries;
- send(*clients[turn], messages[0], strlen(messages[0]), 0);
- }
- else if (guess > k) {
- if (turn == PLAYERS - 1)
- --tries;
- send(*clients[turn], messages[1], strlen(messages[1]), 0);
- }
- else if (guess == k) {
- send(*clients[turn], messages[2], strlen(messages[2]), 0);
- close(*clients[turn]);
- break;
- }
- turn++;
- }
- for (int i = 0; i < PLAYERS; i++) {
- send(*clients[i], messages[3], strlen(messages[3]), 0);
- close(*clients[i]);
- }
- pthread_exit(NULL);
- return (NULL);
- }
- int main(int argc, char ** argv)
- {
- int ** clients;
- int server_sock;
- struct sockaddr_in server_addr;
- struct sockaddr_in client_address;
- socklen_t size = sizeof(client_address);
- server_sock = socket(AF_INET, SOCK_STREAM, 0);
- server_addr = setup_addr(1337);
- int so_reuseaddr = 1;
- setsockopt(server_sock ,SOL_SOCKET,SO_REUSEADDR,
- &so_reuseaddr,
- sizeof so_reuseaddr);
- if (bind(server_sock, (struct sockaddr *)&server_addr,
- sizeof(server_addr)) < 0) die("Socket bind failed");
- if (listen(server_sock, 1) != 0) die("Failed to listen");
- clients = malloc(sizeof(int *) * PLAYERS);
- while (1)
- {
- for (int i = 0; i < PLAYERS; i++) {
- clients[i] = malloc(sizeof(int));
- *clients[i] = accept(server_sock,
- (struct sockaddr *)&client_address, &size);
- printf("s%d: %d\n", i, *clients[i]);
- }
- pthread_t th;
- if (pthread_create(&th, NULL, play_game, clients) < 0)
- die("create thread");
- }
- for (int i = 0; i < PLAYERS; i++) free(clients[i]);
- free(clients);
- close(server_sock);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement