-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created minimal Client class with testing in main.cpp
- Loading branch information
Showing
3 changed files
with
96 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters