65 lines
1.6 KiB
C
65 lines
1.6 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
#include <pthread.h>
|
|
#include <sys/types.h>
|
|
#include <limits.h>
|
|
|
|
#include "common.h"
|
|
#include "handle_communication/handle_communication.h"
|
|
|
|
#define PORT 8080
|
|
|
|
int main()
|
|
{
|
|
// Players' positions
|
|
playerPosition player_one_position;
|
|
playerPosition player_two_position;
|
|
|
|
//Threads
|
|
pthread_t thread1, thread2;
|
|
|
|
/* Defining players' address structures */
|
|
struct sockaddr_in address_server;
|
|
|
|
communicationData com_data_playerOne;
|
|
communicationData com_data_playerTwo;
|
|
|
|
// Create a socket for communication
|
|
int Socket = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if(Socket < 0) {
|
|
printf("Error while creating a socket\n");
|
|
return -1;
|
|
}
|
|
|
|
/* Setting the server structures */
|
|
address_server.sin_port = PORT;
|
|
address_server.sin_family = AF_INET;
|
|
address_server.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
//Bind it to the address
|
|
int Socket_bind = bind(
|
|
Socket,
|
|
(const struct sockaddr *)&address_server,
|
|
sizeof(address_server));
|
|
|
|
if(Socket_bind < 0) {
|
|
printf("Error while binding a socket\n");
|
|
return -1;
|
|
}
|
|
|
|
com_data_playerOne.position = player_one_position;
|
|
com_data_playerTwo.position = player_two_position;
|
|
|
|
com_data_playerOne.Socket = Socket;
|
|
com_data_playerTwo.Socket = Socket;
|
|
|
|
int thread_desc_one = pthread_create(&thread1, NULL, handle_communication, (void *)&com_data_playerOne);
|
|
int thread_desc_two = pthread_create(&thread2, NULL, handle_communication, (void *)&com_data_playerTwo);
|
|
}
|