diff --git a/include/Client.hpp b/include/Client.hpp index 9eea538..f1c9a04 100644 --- a/include/Client.hpp +++ b/include/Client.hpp @@ -3,12 +3,26 @@ #include +#include +#include "Move.h" class Client { - + int socketfd; + char msgbuf[512]; + + const static std::string user13; + const static std::string user14; + const static std::string password13; + const static std::string password14; + + std::stringstream s; int StringToSockaddr(char *name, struct sockaddr_in *address); + std::string getMsg(); + int sendMsg(std::string cmd); + Move getMove(); + int sendMove(Move m); public: - Client(); + Client(int userID, int oppID); }; diff --git a/src/Client.cpp b/src/Client.cpp index d8df51f..6536625 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -10,7 +10,12 @@ #include #include #include "Client.hpp" -#define BUFSIZE 256 +#define BUFSIZE 512 + +const std::string Client::user13 = "13"; +const std::string Client::user14 = "14"; +const std::string Client::password13 = "525871"; +const std::string Client::password14 = "306173"; int Client::StringToSockaddr(char *name, struct sockaddr_in *address) { @@ -48,8 +53,34 @@ int Client::StringToSockaddr(char *name, struct sockaddr_in *address) } -Client::Client(){ - int mySocket; +std::string Client::getMsg() { + bzero(msgbuf, BUFSIZE); + recv(socketfd, msgbuf, BUFSIZE, 0); + return msgbuf; +} + +int Client::sendMsg(std::string cmd){ + bzero(msgbuf, BUFSIZE); + int sz = cmd.size(); + cmd.copy(msgbuf, sz, 0); + msgbuf[sz] = '\r'; + msgbuf[sz+1] = '\n'; + msgbuf[sz+2] = 0; + + send(socketfd, msgbuf, sz+2, 0); + return 0; +} + +Move Client::getMove(){ + Move m; + return m; +} + +int Client::sendMove(Move m){ + return 0; +} + +Client::Client(int userID, int oppID){ struct sockaddr_in icarus_addr; gethostbyname("icarus.engr.uconn.edu"); @@ -58,27 +89,43 @@ Client::Client(){ 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) + if ((socketfd = socket(AF_INET,SOCK_STREAM,0)) < 0) { std::cout << "couldn't allocate socket"; exit(-1); } //connect - if (connect(mySocket,(struct sockaddr *) &icarus_addr,sizeof(icarus_addr)) < 0) + if (connect(socketfd,(struct sockaddr *) &icarus_addr,sizeof(icarus_addr)) < 0) { std::cout << "failed to connect to server" << std::endl; exit(-1); } + std::cout << getMsg(); + std::cout << getMsg(); + + if(userID == 13) + sendMsg(user13); + else + sendMsg(user14); + + std::cout << getMsg(); + + if(userID == 13) + sendMsg(password13); + else + sendMsg(password14); + + std::cout << getMsg(); + // std::cout << getMsg(); + sendMsg(std::to_string(oppID)); + - recv(mySocket, msgbuf, BUFSIZE, 0); - std::stringstream s; - recv(mySocket, msgbuf, BUFSIZE, 0); - s.write(msgbuf, BUFSIZE); - std::cout << s.str() << std::endl; + std::cout << getMsg(); + std::cout << getMsg(); + std::cout << getMsg(); + std::cout << getMsg(); } diff --git a/src/main.cpp b/src/main.cpp index 4fd4246..3cff5d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,7 @@ int main(int argc, const char * argv[]) { - Client c; + Client c(13, 0); /* std::unique_ptr board (new BitBoard);