From c04c364496407ea8a25cbae769ff9c6986196790 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 11 Jul 2010 22:39:17 +0800 Subject: [PATCH] Server now listens and accepts connections (and returns a value to USER) --- server/src/main.c | 24 +++++++++++++++++++ server/src/server.c | 58 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/server/src/main.c b/server/src/main.c index 7d7ba4c..89fe909 100644 --- a/server/src/main.c +++ b/server/src/main.c @@ -15,6 +15,7 @@ extern void Init_Cokebank(void); extern void Load_Itemlist(void); extern void Server_Start(void); +extern int giServer_Port; // === GLOBALS === int giDebugLevel = 0; @@ -22,6 +23,29 @@ extern void Server_Start(void); // === CODE === int main(int argc, char *argv[]) { + int i; + + // Parse Arguments + for( i = 1; i < argc; i++ ) + { + char *arg = argv[i]; + if( arg[0] == '-' ) + { + switch(arg[1]) + { + case 'p': + giServer_Port = atoi(argv[++i]); + break; + default: + // Usage Error? + break; + } + } + else { + // Usage Error? + } + } + //Init_Cokebank(); //Load_Itemlist(); diff --git a/server/src/server.c b/server/src/server.c index 5d54235..98c2cbb 100644 --- a/server/src/server.c +++ b/server/src/server.c @@ -11,6 +11,9 @@ #include #include "common.h" #include +#include +#include +#include #include #define MAX_CONNECTION_QUEUE 5 @@ -54,11 +57,62 @@ struct sClientCommand { */ void Server_Start(void) { + int server_socket, client_socket; + struct sockaddr_in server_addr, client_addr; + // Create Server + server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if( server_socket < 0 ) { + fprintf(stderr, "ERROR: Unable to create server socket\n"); + return ; + } + + // Make listen address + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sin_family = AF_INET; // Internet Socket + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on all interfaces + server_addr.sin_port = htons(giServer_Port); // Port + + // Bind + if( bind(server_socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0 ) { + fprintf(stderr, "ERROR: Unable to bind to 0.0.0.0:%i\n", giServer_Port); + return ; + } + + // Listen + if( listen(server_socket, MAX_CONNECTION_QUEUE) < 0 ) { + fprintf(stderr, "ERROR: Unable to listen to socket\n"); + return ; + } + + printf("Listening on 0.0.0.0:%i\n", giServer_Port); + + for(;;) + { + uint len = sizeof(client_addr); + + client_socket = accept(server_socket, (struct sockaddr *) &client_addr, &len); + if(client_socket < 0) { + fprintf(stderr, "ERROR: Unable to accept client connection\n"); + return ; + } + + if(giDebugLevel >= 2) { + char ipstr[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &client_addr.sin_addr, ipstr, INET_ADDRSTRLEN); + printf("Client connection from %s\n", ipstr); + } + + // TODO: Multithread this? + Server_HandleClient(client_socket); + + close(client_socket); + } } /** * \brief Reads from a client socket and parses the command strings + * \param Socket Client socket number/handle */ void Server_HandleClient(int Socket) { @@ -125,6 +179,8 @@ void Server_HandleClient(int Socket) /** * \brief Parses a client command and calls the required helper function * \param Client Pointer to client state structure + * \param CommandString Command from client (single line of the command) + * \return Heap String to return to the client */ char *Server_ParseClientCommand(tClient *Client, char *CommandString) { @@ -156,6 +212,8 @@ char *Server_ParseClientCommand(tClient *Client, char *CommandString) // --- /** * \brief Set client username + * + * Usage: USER */ char *Server_Cmd_USER(tClient *Client, char *Args) { -- 2.20.1