Skip to content

Commit

Permalink
Further Client functionailty added
Browse files Browse the repository at this point in the history
  • Loading branch information
dmj12004 committed Apr 27, 2017
1 parent f1a3587 commit 5b1fe28
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
18 changes: 16 additions & 2 deletions include/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@

#include <netinet/in.h>

#include <sstream>
#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);

};

Expand Down
71 changes: 59 additions & 12 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
#include <sstream>
#include <string>
#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)
{
Expand Down Expand Up @@ -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");
Expand All @@ -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();
}

2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

int main(int argc, const char * argv[]) {

Client c;
Client c(13, 0);

/*
std::unique_ptr<BitBoard> board (new BitBoard);
Expand Down

0 comments on commit 5b1fe28

Please sign in to comment.