From f1a3587f4c60dffe2d1ac9c6975d628e917074a8 Mon Sep 17 00:00:00 2001 From: David Jardim Date: Wed, 26 Apr 2017 15:31:59 -0400 Subject: [PATCH] Created minimal Client class with testing in main.cpp --- include/Client.hpp | 17 ++++++ src/Client.cpp | 143 +++++++++++++++++++++++---------------------- src/main.cpp | 7 +++ 3 files changed, 96 insertions(+), 71 deletions(-) create mode 100644 include/Client.hpp diff --git a/include/Client.hpp b/include/Client.hpp new file mode 100644 index 0000000..9eea538 --- /dev/null +++ b/include/Client.hpp @@ -0,0 +1,17 @@ +#ifndef CLIENT_H +#define CLIENT_H + +#include + + +class Client { + + int StringToSockaddr(char *name, struct sockaddr_in *address); +public: + Client(); + +}; + + + +#endif diff --git a/src/Client.cpp b/src/Client.cpp index af87cae..d8df51f 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -1,83 +1,84 @@ -#pragma comment(lib, "ws2_32.lib") -#include +/* C References */ +#include /* for gethostbyname() and struct hostent */ +#include /* for pid_t, socket.h */ +#include /* for MAXHOSTNAMELEN */ +#include /* for AF_INET, etc. */ +#include /* for struct sockaddr_in */ + + #include +#include #include +#include "Client.hpp" +#define BUFSIZE 256 -int main() +int Client::StringToSockaddr(char *name, struct sockaddr_in *address) { - //Winsock startup - char User[5] = "14\r\n"; - char Password[9] = "306173\r\n"; - char Opponent[5] = "0\r\n"; - - WSAData wsaData; - WORD DllVersion = MAKEWORD(2, 1); - if (WSAStartup(DllVersion, &wsaData) != 0) - { - MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR); - } + int a,b,c,d,p; + char string[BUFSIZE]; + char *cp; + + strcpy(string,name); + cp = string; + + address->sin_family = AF_INET; + + /* throw away leading blanks, since they make gethostbyname() choke. */ + while (cp[0]==' ' || cp[0]=='\t') cp++; + + char *port; + + /* find the '-' in string: format must be hostname-port*/ + if ((port=strchr(cp,'-')) == NULL) + return -3; + + /* split string in two... hostname\0port\0 and increment port past \0 */ + *port++ = '\0'; + + struct hostent *destHostEntry; + destHostEntry=gethostbyname(cp); + + /* copy the address from the hostEntry into our address */ + bcopy(destHostEntry->h_addr_list[0], + &address->sin_addr.s_addr, destHostEntry->h_length); + /* look-up the hostentry for hostname */ + address->sin_port = htons(atoi(port)); + + return 0; +} - SOCKADDR_IN addr; - int sizeofaddr = sizeof(addr); - addr.sin_addr.s_addr = inet_addr("137.99.15.69"); - addr.sin_port = htons(3499); - addr.sin_family = AF_INET; //IPv4 socket - SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL); - if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0) +Client::Client(){ + int mySocket; + struct sockaddr_in icarus_addr; + + gethostbyname("icarus.engr.uconn.edu"); + bzero((char *) &icarus_addr, sizeof(icarus_addr)); + + char server[] = "icarus.engr.uconn.edu-3499"; + StringToSockaddr(server, &icarus_addr); + + char msgbuf[BUFSIZE]; + + // create socket + if ((mySocket = socket(AF_INET,SOCK_STREAM,0)) < 0) { - MessageBoxA(NULL, "Failed to Connect", "Error", MB_OK | MB_ICONERROR); - return 0; + std::cout << "couldn't allocate socket"; + exit(-1); } - std::cout << "Connected!" << std::endl; - char Terminate[4] = "\r\n"; - - char Message[64]; - recv(Connection, Message, sizeof(Message), NULL); //Receive SAM, Username - std::cout << "read: " << Message << std::endl; //Send Username - recv(Connection, Message, sizeof(Message), NULL); - - std::cout << "read: " << Message << std::endl; - send(Connection, User, sizeof(User), MSG_OOB); - std::cout << "sent: " << User << std::endl; - - recv(Connection, Message, sizeof(Message), NULL); //Receive and send Password - std::cout << "read: " << Message << std::endl; - send(Connection, Password, sizeof(Password), MSG_OOB); - - recv(Connection, Message, sizeof(Message), NULL); //Receive and send opponent - std::cout << "read: " << Message << std::endl; - send(Connection, Opponent, sizeof(Opponent), NULL); - std::cout << "sent: " << Opponent << std::endl; - recv(Connection, Message, sizeof(Message), NULL); //Receive game ID - std::cout << "read: " << Message << std::endl; - std::string GameID = Message; //get Game ID - GameID = GameID.substr(5, 10); - - recv(Connection, Message, sizeof(Message), NULL); //Receive color - std::string Color = Message; //Get Color - Color = Color.substr(5, 6); //For some reason I can't get just 'White' or just 'Black', ':White' is closest I can get - std::cout << Color << std::endl; - std::cout << "I am playing as " << Color << " in game number " << GameID << std::endl; - std::cout << "read: " << Message << std::endl; - - if (Color == ":White") + + //connect + if (connect(mySocket,(struct sockaddr *) &icarus_addr,sizeof(icarus_addr)) < 0) { - std::cout << "WHITE" << std::endl; - while (TRUE) - { - - } + std::cout << "failed to connect to server" << std::endl; + exit(-1); } - else - { - std::cout << "BLACK" << std::endl; - while (TRUE) - { - } - } - system("pause"); - return 0; -} \ No newline at end of file + recv(mySocket, msgbuf, BUFSIZE, 0); + std::stringstream s; + recv(mySocket, msgbuf, BUFSIZE, 0); + s.write(msgbuf, BUFSIZE); + std::cout << s.str() << std::endl; +} + diff --git a/src/main.cpp b/src/main.cpp index 1d1ff06..4fd4246 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,12 +2,18 @@ #include #include #include + #include "BitBoard.h" #include "Move.h" #include "MinimaxSearch.h" #include "Heuristic.hpp" +#include "Client.hpp" int main(int argc, const char * argv[]) { + + Client c; + + /* std::unique_ptr board (new BitBoard); Heuristic heur_test; MinimaxSearch searchDriver(heur_test); @@ -18,6 +24,7 @@ int main(int argc, const char * argv[]) { board = board->result(bestMove); std::cout << "=======================================" << std::endl; } + */ return 0; }