Danbias/Code/Network/OysterNetworkClient/ClientMain.cpp

152 lines
2.6 KiB
C++
Raw Normal View History

2013-11-18 16:47:57 +01:00
#include <iostream>
#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;;
using namespace Oyster::Network::Client;
#pragma comment(lib, "ws2_32.lib")
2013-11-22 09:17:07 +01:00
void ShutdownSockets();
bool InitSockets();
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;
int main()
{
char msgRecv[255] = "\0";
InitSockets();
cout << "Client" << endl;
2013-11-18 16:47:57 +01:00
//Create Client
Client client;
//Connect to server
2013-11-22 08:56:00 +01:00
client.Connect(9876, "127.0.0.1");
unsigned char* recvBuffer = new unsigned char[255];
2013-11-22 09:17:07 +01:00
<<<<<<< HEAD
2013-11-22 08:56:00 +01:00
client.Send(recvBuffer);
2013-11-22 08:56:00 +01:00
//chat(client);
2013-11-22 09:17:07 +01:00
=======
chat(client);
>>>>>>> 4142688f6c4a63aa97341205588ad6cace0f43af
//Recieve message
//client.Recv(msgRecv);
//print message
//cout << msgRecv << endl;
2013-11-18 16:47:57 +01:00
2013-11-19 14:21:25 +01:00
ShutdownSockets();
system("pause");
return 0;
}
bool InitSockets()
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
}
void ShutdownSockets()
{
WSACleanup();
}
void chat(Client client)
{
2013-11-22 09:17:07 +01:00
<<<<<<< HEAD
2013-11-22 08:56:00 +01:00
unsigned char msgRecv[255] = "\0";
unsigned char msgSend[255] = "\0";
2013-11-22 09:17:07 +01:00
=======
Oyster::Network::Translator *t = new Oyster::Network::Translator();
unsigned char msgRecv[255] = "\0";
string msgSend = "";
ProtocolHeader header;
ProtocolTest test;
>>>>>>> 4142688f6c4a63aa97341205588ad6cace0f43af
bool chatDone = false;
while(!chatDone)
{
client.Recv(msgRecv);
2013-11-22 09:17:07 +01:00
header = t->Translate(msgRecv);
switch(header.packageType)
{
case package_type_header:
break;
2013-11-22 09:17:07 +01:00
case package_type_test:
cout <<"Client 2: " <<((ProtocolTest*)&header)->textMessage <<endl;
break;
}
2013-11-22 09:17:07 +01:00
<<<<<<< HEAD
2013-11-22 08:56:00 +01:00
cin.getline((char*)msgSend , 255 , '\n');
2013-11-22 08:56:00 +01:00
if(strlen((char*)msgSend) < 1)
2013-11-19 14:59:00 +01:00
{
2013-11-22 08:56:00 +01:00
memcpy(msgSend, " ", 1);
2013-11-19 14:59:00 +01:00
}
2013-11-22 08:56:00 +01:00
if((char*)msgSend != "exit")
{
2013-11-22 08:56:00 +01:00
if(strlen((char*)msgSend) < 1)
{
2013-11-22 08:56:00 +01:00
memcpy(msgSend, "ERROR", 1);
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");
>>>>>>> 4142688f6c4a63aa97341205588ad6cace0f43af
}
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);
}
else
{
chatDone = true;
}
2013-11-19 14:59:00 +01:00
cin.clear();
}
}