From 27ff91250607cf94787b26b8b1cc3b90203057a1 Mon Sep 17 00:00:00 2001 From: michalStarski Date: Thu, 8 Nov 2018 16:57:11 +0100 Subject: [PATCH] added initial server structure --- common.h | 18 ++++++ handle_communication/handle_communication.c | 44 ++++++++++++++ handle_communication/handle_communication.h | 8 +++ main.c | 64 +++++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 common.h create mode 100644 handle_communication/handle_communication.c create mode 100644 handle_communication/handle_communication.h create mode 100644 main.c diff --git a/common.h b/common.h new file mode 100644 index 0000000..e567e93 --- /dev/null +++ b/common.h @@ -0,0 +1,18 @@ +#ifndef common_H_ +#define common_H_ + +// Position structure +typedef struct +{ + float x; + float y; + float z; +} playerPosition; + +typedef struct +{ + playerPosition position; + int Socket; +} communicationData; + +#endif diff --git a/handle_communication/handle_communication.c b/handle_communication/handle_communication.c new file mode 100644 index 0000000..9aa566c --- /dev/null +++ b/handle_communication/handle_communication.c @@ -0,0 +1,44 @@ +#include "handle_communication.h"; +#include "../common.h"; +#include +#include +#include +#include +#include +#include + +void handle_communication(void *player_data) +{ + + communicationData *playerData = (communicationData *)player_data; + + struct sockaddr_in client_address; + socklen_t c_addrlen = sizeof(client_address); + + //Receive data from the client + int Socket_receive = recvfrom( + playerData->Socket, + &playerData->position, sizeof(playerPosition), + 0, + (const struct sockaddr *)&client_address, + c_addrlen); + + if(Socket_receive < 0) { + printf("Error while receiving a package\n"); + return; + } + + //Send updated position back + int Socket_send = sendto( + playerData->Socket, + &playerData->position, + sizeof(playerPosition), + 0, + (const struct sockaddr *)&client_address, + c_addrlen); + + if(Socket_send < 0) { + printf("Error while sending a package\n"); + return; + } +} diff --git a/handle_communication/handle_communication.h b/handle_communication/handle_communication.h new file mode 100644 index 0000000..ce49fac --- /dev/null +++ b/handle_communication/handle_communication.h @@ -0,0 +1,8 @@ +#ifndef handle_communication_H_ +#define handle_communication_H_ + +#include "../common.h" + +void handle_communication(void *player_data); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..adca2e7 --- /dev/null +++ b/main.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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); +}