2013-11-18 16:47:57 +01:00
|
|
|
#include <iostream>
|
2013-11-19 13:42:50 +01:00
|
|
|
#include <WinSock2.h>
|
2013-11-22 14:23:08 +01:00
|
|
|
#include <vld.h>
|
2013-11-19 13:42:50 +01:00
|
|
|
#include "Listener.h"
|
|
|
|
#include "Client.h"
|
2013-11-18 16:47:57 +01:00
|
|
|
using namespace std;
|
2013-11-21 13:40:52 +01:00
|
|
|
using namespace Oyster::Network::Server;
|
2013-11-18 16:47:57 +01:00
|
|
|
|
2013-11-19 13:42:50 +01:00
|
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
|
|
|
|
|
|
void ShutdownSockets();
|
|
|
|
bool InitSockets();
|
|
|
|
|
2013-11-21 14:49:30 +01:00
|
|
|
#include "../NetworkDependencies/Translator.h"
|
|
|
|
using namespace Oyster::Network;
|
|
|
|
using namespace ::Protocols;
|
2013-11-21 13:42:38 +01:00
|
|
|
|
2013-11-18 16:34:50 +01:00
|
|
|
int main()
|
|
|
|
{
|
2013-11-22 14:23:08 +01:00
|
|
|
unsigned char* recvBuffer = new unsigned char[256];
|
2013-11-21 14:49:30 +01:00
|
|
|
cout << "Server" << endl;
|
2013-11-22 08:56:00 +01:00
|
|
|
Translator t;
|
2013-11-19 14:21:25 +01:00
|
|
|
|
2013-11-19 13:42:50 +01:00
|
|
|
if(!InitSockets())
|
|
|
|
{
|
|
|
|
cout << "Sockets failed to initialize" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create socket
|
|
|
|
Listener listener;
|
|
|
|
listener.Init(9876);
|
|
|
|
|
|
|
|
//Start listening
|
2013-11-19 14:21:25 +01:00
|
|
|
//Accept a client
|
2013-11-19 13:42:50 +01:00
|
|
|
int clientSocket = listener.Accept();
|
2013-11-19 14:21:25 +01:00
|
|
|
Client client1(clientSocket);
|
|
|
|
cout << "First client connected." << endl;
|
2013-11-19 13:42:50 +01:00
|
|
|
|
|
|
|
//Accept a client
|
2013-11-19 14:21:25 +01:00
|
|
|
clientSocket = listener.Accept();
|
|
|
|
Client client2(clientSocket);
|
|
|
|
cout << "Second client connected." << endl;
|
|
|
|
|
2013-11-22 11:40:55 +01:00
|
|
|
ProtocolSet* set = new ProtocolSet;
|
2013-11-22 15:48:49 +01:00
|
|
|
ProtocolTest test;
|
|
|
|
test.clientID = 0;
|
|
|
|
test.textMessage = "hej";
|
|
|
|
recvBuffer = t.Translate(test);
|
|
|
|
|
|
|
|
client1.Send(recvBuffer);
|
2013-11-22 14:23:08 +01:00
|
|
|
|
2013-11-19 14:21:25 +01:00
|
|
|
while(1)
|
|
|
|
{
|
2013-11-22 08:56:00 +01:00
|
|
|
client1.Recv(recvBuffer);
|
2013-11-22 14:23:08 +01:00
|
|
|
|
|
|
|
t.Translate(set, recvBuffer);
|
2013-11-22 11:40:55 +01:00
|
|
|
cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl;
|
|
|
|
cout << "Client1: " << set->Protocol.pTest->textMessage << endl;
|
|
|
|
set->Release();
|
2013-11-19 14:21:25 +01:00
|
|
|
client2.Send(recvBuffer);
|
|
|
|
|
|
|
|
client2.Recv(recvBuffer);
|
2013-11-22 08:56:00 +01:00
|
|
|
|
2013-11-22 14:23:08 +01:00
|
|
|
t.Translate(set, recvBuffer);
|
2013-11-22 11:40:55 +01:00
|
|
|
cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl;
|
|
|
|
cout << "Client2: " << set->Protocol.pTest->textMessage << endl;
|
|
|
|
set->Release();
|
2013-11-22 08:56:00 +01:00
|
|
|
client1.Send(recvBuffer);
|
2013-11-19 14:21:25 +01:00
|
|
|
}
|
2013-11-18 16:47:57 +01:00
|
|
|
|
2013-11-19 13:42:50 +01:00
|
|
|
ShutdownSockets();
|
2013-11-22 08:56:00 +01:00
|
|
|
delete[] recvBuffer;
|
2013-11-22 11:40:55 +01:00
|
|
|
delete set;
|
2013-11-22 08:56:00 +01:00
|
|
|
|
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-18 16:34:50 +01:00
|
|
|
}
|