Danbias/Code/Network/OysterNetworkServer/ServerMain.cpp

97 lines
2.2 KiB
C++
Raw Normal View History

2013-11-18 16:47:57 +01:00
#include <iostream>
#include <WinSock2.h>
2013-11-22 14:23:08 +01:00
#include <vld.h>
#include "../NetworkDependencies/WinsockFunctions.h"
#include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/Translator.h"
#include "Client.h"
#include "../NetworkDependencies/OysterByte.h"
2013-11-18 16:47:57 +01:00
#pragma comment(lib, "ws2_32.lib")
using namespace std;
using namespace Oyster::Network::Server;
2013-11-21 14:49:30 +01:00
using namespace Oyster::Network;
using namespace ::Protocols;
int main()
{
OysterByte recvBuffer;
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
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
int clientSocket = listener.Accept();
2013-11-19 14:21:25 +01:00
Client client1(clientSocket);
cout << "First client connected." << endl;
//Accept a client
2013-11-19 14:21:25 +01:00
clientSocket = listener.Accept();
Client client2(clientSocket);
cout << "Second client connected." << endl;
ProtocolSet* set = new ProtocolSet;
2013-11-22 15:48:49 +01:00
ProtocolTest test;
test.clientID = 0;
test.size = 2;
2013-11-22 15:48:49 +01:00
test.textMessage = "hej";
test.numOfFloats = 35;
test.f = new float[test.numOfFloats];
float temp = 395.456f;
for(int i = 0; i < test.numOfFloats; i++)
{
test.f[i] = temp;
temp--;
}
t.Pack(test, recvBuffer);
2013-11-22 15:48:49 +01:00
client1.Send(recvBuffer);
2013-11-19 14:21:25 +01:00
while(1)
{
2013-11-22 08:56:00 +01:00
client1.Recv(recvBuffer);
t.Unpack(set, recvBuffer);
cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl;
cout << "Client1: " << set->Protocol.pTest->textMessage << endl;
for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ';
}
cout << 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
t.Unpack(set, recvBuffer);
cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl;
cout << "Client2: " << set->Protocol.pTest->textMessage << endl;
for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ';
}
cout << 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
ShutdownSockets();
delete set;
2013-11-22 08:56:00 +01:00
system("pause");
return 0;
}