Skip to content

Commit

Permalink
Created minimal Client class with testing in main.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
dmj12004 committed Apr 26, 2017
1 parent 35126f4 commit f1a3587
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 71 deletions.
17 changes: 17 additions & 0 deletions include/Client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef CLIENT_H
#define CLIENT_H

#include <netinet/in.h>


class Client {

int StringToSockaddr(char *name, struct sockaddr_in *address);
public:
Client();

};



#endif
143 changes: 72 additions & 71 deletions src/Client.cpp
Original file line number Diff line number Diff line change
@@ -1,83 +1,84 @@
#pragma comment(lib, "ws2_32.lib")
#include <WinSock2.h>
/* C References */
#include <netdb.h> /* for gethostbyname() and struct hostent */
#include <sys/types.h> /* for pid_t, socket.h */
#include <sys/param.h> /* for MAXHOSTNAMELEN */
#include <sys/socket.h> /* for AF_INET, etc. */
#include <netinet/in.h> /* for struct sockaddr_in */


#include <iostream>
#include <sstream>
#include <string>
#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;
}
recv(mySocket, msgbuf, BUFSIZE, 0);
std::stringstream s;
recv(mySocket, msgbuf, BUFSIZE, 0);
s.write(msgbuf, BUFSIZE);
std::cout << s.str() << std::endl;
}

7 changes: 7 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
#include <cstdint>
#include <vector>
#include <memory>

#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<BitBoard> board (new BitBoard);
Heuristic heur_test;
MinimaxSearch searchDriver(heur_test);
Expand All @@ -18,6 +24,7 @@ int main(int argc, const char * argv[]) {
board = board->result(bestMove);
std::cout << "=======================================" << std::endl;
}
*/

return 0;
}

0 comments on commit f1a3587

Please sign in to comment.