2013-11-18 16:47:57 +01:00
|
|
|
#include <iostream>
|
2013-11-19 13:42:50 +01:00
|
|
|
#include "Client.h"
|
|
|
|
#include <WinSock2.h>
|
2013-11-22 09:17:07 +01:00
|
|
|
#include "..\NetworkDependencies\Translator.h"
|
|
|
|
|
2013-11-18 16:47:57 +01:00
|
|
|
using namespace std;
|
2013-11-22 09:17:07 +01:00
|
|
|
using namespace Oyster::Network::Protocols;;
|
2013-11-21 13:40:52 +01:00
|
|
|
using namespace Oyster::Network::Client;
|
2013-11-19 13:42:50 +01:00
|
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
|
|
|
2013-11-22 09:17:07 +01:00
|
|
|
|
2013-11-19 13:42:50 +01:00
|
|
|
void ShutdownSockets();
|
|
|
|
bool InitSockets();
|
2013-11-19 14:18:34 +01:00
|
|
|
void chat(Client client);
|
2013-11-18 16:47:57 +01:00
|
|
|
|
2013-11-22 08:56:00 +01:00
|
|
|
#include "../NetworkDependencies/Protocols.h"
|
|
|
|
#include "../NetworkDependencies/Translator.h"
|
|
|
|
using namespace Oyster::Network::Protocols;
|
|
|
|
|
2013-11-18 16:34:50 +01:00
|
|
|
int main()
|
|
|
|
{
|
2013-11-19 13:42:50 +01:00
|
|
|
char msgRecv[255] = "\0";
|
|
|
|
|
|
|
|
InitSockets();
|
|
|
|
|
2013-11-19 09:34:24 +01:00
|
|
|
cout << "Client" << endl;
|
2013-11-18 16:47:57 +01:00
|
|
|
|
2013-11-19 13:42:50 +01:00
|
|
|
//Create Client
|
|
|
|
Client client;
|
|
|
|
|
|
|
|
//Connect to server
|
2013-11-22 08:56:00 +01:00
|
|
|
client.Connect(9876, "127.0.0.1");
|
|
|
|
|
2013-11-22 09:17:07 +01:00
|
|
|
chat(client);
|
2013-11-19 13:42:50 +01:00
|
|
|
|
|
|
|
//Recieve message
|
2013-11-19 14:18:34 +01:00
|
|
|
//client.Recv(msgRecv);
|
2013-11-19 13:42:50 +01:00
|
|
|
|
|
|
|
//print message
|
2013-11-19 14:18:34 +01:00
|
|
|
//cout << msgRecv << endl;
|
2013-11-18 16:47:57 +01:00
|
|
|
|
2013-11-19 14:21:25 +01:00
|
|
|
ShutdownSockets();
|
2013-11-19 13:42:50 +01:00
|
|
|
|
|
|
|
system("pause");
|
2013-11-18 16:34:50 +01:00
|
|
|
return 0;
|
2013-11-19 13:42:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InitSockets()
|
|
|
|
{
|
|
|
|
WSADATA wsaData;
|
|
|
|
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShutdownSockets()
|
|
|
|
{
|
|
|
|
WSACleanup();
|
2013-11-19 14:18:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void chat(Client client)
|
|
|
|
{
|
2013-11-22 09:17:07 +01:00
|
|
|
Oyster::Network::Translator *t = new Oyster::Network::Translator();
|
|
|
|
|
|
|
|
unsigned char msgRecv[255] = "\0";
|
|
|
|
string msgSend = "";
|
|
|
|
|
2013-11-22 09:24:44 +01:00
|
|
|
ProtocolHeader* header;
|
2013-11-22 09:17:07 +01:00
|
|
|
ProtocolTest test;
|
2013-11-19 14:18:34 +01:00
|
|
|
|
|
|
|
bool chatDone = false;
|
|
|
|
|
|
|
|
while(!chatDone)
|
|
|
|
{
|
|
|
|
client.Recv(msgRecv);
|
2013-11-22 09:17:07 +01:00
|
|
|
|
|
|
|
header = t->Translate(msgRecv);
|
|
|
|
|
2013-11-22 09:24:44 +01:00
|
|
|
switch(header->packageType)
|
2013-11-22 09:17:07 +01:00
|
|
|
{
|
|
|
|
case package_type_header:
|
|
|
|
break;
|
|
|
|
case package_type_test:
|
|
|
|
cout <<"Client 2: " <<((ProtocolTest*)&header)->textMessage <<endl;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-19 14:18:34 +01:00
|
|
|
|
|
|
|
|
2013-11-22 09:17:07 +01:00
|
|
|
std::getline(std::cin, msgSend);
|
|
|
|
std::cin.clear();
|
|
|
|
|
|
|
|
if(msgSend.length() < 1)
|
|
|
|
{
|
|
|
|
//memcpy(msgSend, " " , 1);
|
|
|
|
msgSend = " ";
|
|
|
|
//strcpy_s((char)msgSend , " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if( msgSend != "exit")
|
|
|
|
{
|
|
|
|
if(msgSend.length() < 1)
|
|
|
|
{
|
|
|
|
//memcpy(msgSend, "ERROR" , 5);
|
|
|
|
msgSend = "ERROR!";
|
|
|
|
//strcpy_s(msgSend, "ERROR");
|
2013-11-20 10:33:52 +01:00
|
|
|
}
|
2013-11-22 09:17:07 +01:00
|
|
|
|
|
|
|
test.packageType = package_type_test;
|
|
|
|
test.size = msgSend.length();
|
|
|
|
test.textMessage = msgSend;
|
|
|
|
|
|
|
|
unsigned char *message = t->Translate(test);
|
|
|
|
|
|
|
|
client.Send(message);
|
2013-11-19 14:18:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chatDone = true;
|
|
|
|
}
|
|
|
|
|
2013-11-19 14:59:00 +01:00
|
|
|
cin.clear();
|
|
|
|
|
2013-11-19 14:18:34 +01:00
|
|
|
}
|
|
|
|
|
2013-11-18 16:34:50 +01:00
|
|
|
}
|