diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2011-01-14 01:56:12 +0100 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2011-01-14 01:56:12 +0100 |
| commit | cf073d5083edaa8249ad1a960db0b2ba7619e70e (patch) | |
| tree | fd4687e291c34601b8b629ff130b35573528b36b | |
| parent | 1b9897c0253415e35123f38b1e24d10d47d73959 (diff) | |
added initial server/client code
| -rw-r--r-- | src/net.c | 157 | ||||
| -rw-r--r-- | src/net.h | 10 |
2 files changed, 167 insertions, 0 deletions
diff --git a/src/net.c b/src/net.c new file mode 100644 index 0000000..627f2b0 --- /dev/null +++ b/src/net.c @@ -0,0 +1,157 @@ +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <assert.h> +#include "net.h" + +/** + * Server side function; start server on specified port + * @param[in] port Port on which server should listen + * @return Listening socket + */ +int server_start(const char* port) +{ + int status; + int serversock; + struct addrinfo hints, *result, *tmp; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 + hints.ai_socktype = SOCK_STREAM; // TCP socket + hints.ai_flags = AI_PASSIVE; // wildcard IP + hints.ai_protocol = 0; // any protocol + + status = getaddrinfo(NULL, port, &hints, &result); + if(status != 0) + { + printf("getaddrinfo: %s\n", gai_strerror(status)); + exit(EXIT_FAILURE); + } + + // iterate over linked addrinfo list and use first useable entry + for(tmp = result; tmp != NULL; tmp = tmp->ai_next) + { + int yes=1; + + // create socket + serversock = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol); + if(serversock == -1) + continue; + + // try to reuse still open sockets in TIME_WAIT state + setsockopt(serversock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); + + // try to bind to address/port + if(bind(serversock, tmp->ai_addr, tmp->ai_addrlen) == 0) + break; // Success! + + close(serversock); + } + + if(tmp == NULL) + { + printf("failed to bind\n"); + exit(EXIT_FAILURE); + } + freeaddrinfo(result); + + // start listening + status = listen(serversock, 1); + if(status == -1) + { + printf("listen: %s\n", strerror(status)); + exit(EXIT_FAILURE); + } + + return serversock; +} + +/** + * Server side function; accepts connections from clients + * @param[in] serversock Socket on which server is listening + * @param[in] count Number of clients that should connect + * @return List of $count open sockets with connections to clients + */ +int* server_get_players(int serversock, const uint8_t count) +{ + int* clientsocks; + int i; + + assert(count < MAX_PLAYERS && count > 0); + + clientsocks = malloc(count*sizeof(int)); + assert(clientsocks != NULL); + + // accept connections + for(i=0; i<count; i++) + { + int sock; + struct sockaddr_storage addr; + socklen_t addrlen = sizeof(addr); + sock = accept(serversock, (struct sockaddr*) &addr, &addrlen); + if(sock == -1) + { + printf("accept: %s\n", strerror(sock)); + exit(EXIT_FAILURE); + } + //printf("new client connected: %s\n", inet_ntop(sock.ss_family, get_in_addr((struct sockaddr*)&sock), INET6_ADDRSTRLEN)); + printf("new client connected (%d/%d)\n", i+1, count); + clientsocks[i] = sock; + } + + return clientsocks; +} + +/** + * Client side function; connects to specified host:port + * @param[in] host Hostname of server + * @param[in] port Port of server + * @return Socket with open connection to server + */ +int client_connect_server(const char* host, const char* port) +{ + int status; + int sock; + struct addrinfo hints, *result, *tmp; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 + hints.ai_socktype = SOCK_STREAM; // TCP socket + + status = getaddrinfo(host, port, &hints, &result); + if(status != 0) + { + printf("getaddrinfo: %s\n", gai_strerror(status)); + exit(EXIT_FAILURE); + } + + // connect to first result in linked addrinfo list + for(tmp = result; tmp != NULL; tmp = tmp->ai_next) + { + // create socket + sock = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol); + if(sock == -1) + continue; + + // connect! + if(connect(sock, tmp->ai_addr, tmp->ai_addrlen) != -1) + break; // Success! + + close(sock); + } + + if(tmp == NULL) + { + printf("failed to connect\n"); + exit(EXIT_FAILURE); + } + freeaddrinfo(result); + + return sock; +} + + diff --git a/src/net.h b/src/net.h new file mode 100644 index 0000000..f29100a --- /dev/null +++ b/src/net.h @@ -0,0 +1,10 @@ +#ifndef OXEN_NET_H +#define OXEN_NET_H + +#define MAX_PLAYERS 10 + +int server_start(const char* port); +int* server_get_players(int serversock, const uint8_t count); +int client_connect_server(const char* host, const char* port); + +#endif // OXEN_NET_H |
