Working Server and Client
This commit is contained in:
88
network/networking.c
Normal file
88
network/networking.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "networking.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
Server StartServer(int port) {
|
||||
Server s = {2};
|
||||
s.clientID = 0;
|
||||
|
||||
|
||||
// Create Socket
|
||||
if ((s.server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
|
||||
perror("socket failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Set Address
|
||||
s.address.sin_family = AF_INET;
|
||||
s.address.sin_addr.s_addr = INADDR_ANY; // listen on all interfaces
|
||||
s.address.sin_port = htons(port);
|
||||
|
||||
// Bind Port
|
||||
if (bind(s.server_fd, (struct sockaddr *)&s.address, sizeof(s.address)) < 0) {
|
||||
perror("bind failed");
|
||||
close(s.server_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Listen for incoming connections
|
||||
if (listen(s.server_fd, 3) < 0) {
|
||||
perror("listen");
|
||||
close(s.server_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Server listening on port %d...\n", port);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void RemoveClient(Server* s, int index) {
|
||||
close(s->clients[index]);
|
||||
s->clients[index] = s->clients[s->client_count - 1];
|
||||
s->client_count--;
|
||||
}
|
||||
|
||||
Client StartClient(char* host, int port) {
|
||||
|
||||
Client c;
|
||||
|
||||
// Create socket
|
||||
if ((c.client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
perror("socket failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Set address info
|
||||
c.address.sin_family = AF_INET;
|
||||
c.address.sin_port = htons(port);
|
||||
|
||||
// Convert hostname/IP to binary form
|
||||
if (inet_pton(AF_INET, host, &c.address.sin_addr) <= 0) {
|
||||
// If not a direct IP, try resolving hostname
|
||||
struct hostent* he = gethostbyname(host);
|
||||
if (he == NULL) {
|
||||
perror("gethostbyname failed");
|
||||
close(c.client_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
memcpy(&c.address.sin_addr, he->h_addr_list[0], he->h_length);
|
||||
}
|
||||
|
||||
// Connect to server
|
||||
if (connect(c.client_fd, (struct sockaddr*)&c.address, sizeof(c.address)) < 0) {
|
||||
perror("connect failed");
|
||||
close(c.client_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Connected to %s:%d\n", host, port);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
26
network/networking.h
Normal file
26
network/networking.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef GAME_NETWORKING_H
|
||||
#define GAME_NETWORKING_H 1
|
||||
|
||||
#include <netinet/in.h>
|
||||
|
||||
typedef struct {
|
||||
const int max_clients;
|
||||
int server_fd;
|
||||
struct sockaddr_in address;
|
||||
int clients[2];
|
||||
int clientID;
|
||||
int client_count;
|
||||
} Server;
|
||||
|
||||
Server StartServer(int port);
|
||||
void RemoveClient(Server* s, int index);
|
||||
|
||||
typedef struct {
|
||||
int client_fd;
|
||||
struct sockaddr_in address;
|
||||
} Client;
|
||||
|
||||
|
||||
Client StartClient(char* host, int port);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user