added initial server structure
This commit is contained in:
parent
8e66f603d7
commit
27ff912506
18
common.h
Normal file
18
common.h
Normal file
@ -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
|
44
handle_communication/handle_communication.c
Normal file
44
handle_communication/handle_communication.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include "handle_communication.h";
|
||||
#include "../common.h";
|
||||
#include <string.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
8
handle_communication/handle_communication.h
Normal file
8
handle_communication/handle_communication.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef handle_communication_H_
|
||||
#define handle_communication_H_
|
||||
|
||||
#include "../common.h"
|
||||
|
||||
void handle_communication(void *player_data);
|
||||
|
||||
#endif
|
64
main.c
Normal file
64
main.c
Normal file
@ -0,0 +1,64 @@
|
||||
#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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user