From 73d5a5dab8881b9ed5f93a675d64f86a9b45e6bc Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Thu, 19 Jan 2012 19:51:16 +0800 Subject: [PATCH] Setup client and server properly Erm, now what? --- judge/manager/main.cpp | 5 +- judge/manager/network_controller.cpp | 109 +++++++++++++-------------- judge/manager/network_controller.h | 4 +- 3 files changed, 59 insertions(+), 59 deletions(-) diff --git a/judge/manager/main.cpp b/judge/manager/main.cpp index 83b3c63..08c381c 100644 --- a/judge/manager/main.cpp +++ b/judge/manager/main.cpp @@ -242,7 +242,10 @@ Piece::Colour SetupGame(int argc, char ** argv) } else if (red == NULL || blue == NULL) //Not enough players { - fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n"); + if (client) + fprintf(stderr, "ARGUMENT_ERROR - When using the --client switch, supply an IP for the Red player.\n"); + else + fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n"); exit(EXIT_FAILURE); } diff --git a/judge/manager/network_controller.cpp b/judge/manager/network_controller.cpp index 6d56554..9d14676 100644 --- a/judge/manager/network_controller.cpp +++ b/judge/manager/network_controller.cpp @@ -2,13 +2,12 @@ NetworkController::NetworkController(const Piece::Colour & newColour, const char * newName) : Controller(newColour, newName), sfd(-1) { - struct protoent * tcp = getprotobyname("tcp"); + //struct protoent * tcp = getprotobyname("tcp"); - sfd = socket(PF_INET, SOCK_STREAM, tcp->p_proto); + sfd = socket(PF_INET, SOCK_STREAM, 0); if (sfd < 0) { - // fprintf(stderr, "NetworkController::NetworkController - couldn't create a TCP socket!"); - perror("NetworkController::NetworkController - creating TCP socket... "); + perror("NetworkController::NetworkController - Error creating TCP socket"); return; } } @@ -19,7 +18,6 @@ NetworkController::~NetworkController() { if (shutdown(sfd, SHUT_RDWR) == -1) { - // fprintf(stderr, "NetworkController::~NetworkController - Can't shutdown socket %d!", sfd); perror("NetworkController::~NetworkController - shutting down socket... "); close(sfd); sfd = -1; @@ -28,77 +26,76 @@ NetworkController::~NetworkController() close(sfd); } +void NetworkController::Message(const char * string) +{ + +} + Server::Server(const Piece::Colour & newColour, const char * newName) : NetworkController(newColour, newName) { - struct sockaddr_in ipa; - ipa.sin_family = AF_INET; - ipa.sin_port = htons(NetworkController::port); - ipa.sin_addr.s_addr = INADDR_ANY; - memset(&ipa,0, sizeof ipa); + struct sockaddr_in name; + char buf[1024]; + int cc; - if (bind(sfd, (struct sockaddr*)&ipa, sizeof (ipa)) == -1) //dERP DERP DERP + + name.sin_family = AF_INET; + name.sin_addr.s_addr = htonl(INADDR_ANY); + name.sin_port = htons(NetworkController::port); + + if (bind( sfd, (struct sockaddr *) &name, sizeof(name) ) < 0) { - // fprintf(stderr, "Server::Server - Couldn't bind to socket! Abort\n"); - perror("Server::Server - binding to socket... "); - close(sfd); - sfd = -1; - return; + perror("Server::Server - Error binding socket"); + close(sfd); sfd = -1; return; } - //Listen for at most 1 connection - if (listen(sfd, 1) == -1) + if (listen(sfd,1) < 0) { - fprintf(stderr, "Server::Server - listening failed.\n"); - close(sfd); - sfd = -1; - return; - } - - //Accept the connection - sfd = accept(sfd, NULL, NULL); + perror("Server::Server - Error listening on socket"); + close(sfd); sfd = -1; return; + } + int psd = accept(sfd, 0, 0); + close(sfd); + sfd = psd; if (sfd < 0) { - fprintf(stderr, "Server::Server - couldn't accept connection.\n"); - close(sfd); - sfd = -1; - return; + perror("Server::Server - Error accepting connection"); + close(sfd); sfd = -1; return; } - //We are now ready to play! + + for(;;) + { + cc=recv(sfd,buf,sizeof(buf), 0) ; + if (cc == 0) exit (0); + buf[cc] = '\0'; + printf("message received: %s\n", buf); + } } -Client::Client(const Piece::Colour & newColour, const char * newName, const char * server) : NetworkController(newColour, newName) +Client::Client(const Piece::Colour & newColour, const char * newName, const char * address) : NetworkController(newColour, newName) { - struct sockaddr_in ipa; - ipa.sin_family = AF_INET; - ipa.sin_port = htons(NetworkController::port); - int Res = inet_pton(AF_INET, server, &ipa.sin_addr); + struct sockaddr_in server; + struct hostent *hp; - if (Res < 0) - { - fprintf(stderr, "Client::Client - First parameter is not a valid address family!\n"); - close(sfd); - sfd = -1; - return; - } - else if (Res == 0) - { - fprintf(stderr, "Client::Client - Second parameter does not contain a valid IP Address!\n"); - close(sfd); - sfd = -1; - return; - } - - if (connect(sfd, (struct sockaddr*)&sfd, sizeof sfd) == -1) + + server.sin_family = AF_INET; + hp = gethostbyname("127.0.0.1"); + bcopy ( hp->h_addr, &(server.sin_addr.s_addr), hp->h_length); + server.sin_port = htons(NetworkController::port); + + if (connect(sfd, (struct sockaddr *) &server, sizeof(server)) < 0) { - fprintf(stderr, "Client::Client - Connection to server at \"%s\" failed.\n", server); - close(sfd); - sfd = -1; + perror("Client::Client - Error connecting to server at address %s"); + close(sfd); sfd = -1; return; } - //We are now ready to play! + for (;;) { + send(sfd, "HI", 2,0 ); + sleep(2); + } + } //EOF diff --git a/judge/manager/network_controller.h b/judge/manager/network_controller.h index a64ad30..7b9af30 100644 --- a/judge/manager/network_controller.h +++ b/judge/manager/network_controller.h @@ -25,13 +25,13 @@ class NetworkController : public Controller virtual bool Valid() {return sfd != -1;} - virtual void Message(const char * string) {fprintf(stderr, "NetworkController unimplemented!\n"); assert(false);} + virtual void Message(const char * string); virtual MovementResult QuerySetup(const char * opponentName, std::string setup[]) {fprintf(stderr, "NetworkController unimplemented!\n"); assert(false);} virtual MovementResult QueryMove(std::string & buffer) {fprintf(stderr, "NetworkController unimplemented!\n"); assert(false);} protected: int sfd; int cfd; - static const int port = 666; + static const int port = 4950; }; class Server : public NetworkController -- 2.20.1