Danbias/Code/Network/OysterNetworkServer/ServerMain.cpp

66 lines
1.1 KiB
C++
Raw Normal View History

2013-11-18 16:47:57 +01:00
#include <iostream>
#include <WinSock2.h>
#include "Listener.h"
#include "Client.h"
2013-11-18 16:47:57 +01:00
using namespace std;
#pragma comment(lib, "ws2_32.lib")
void ShutdownSockets();
bool InitSockets();
int main()
{
cout << "Server" << endl;
2013-11-18 16:47:57 +01:00
2013-11-19 14:21:25 +01:00
char recvBuffer[255];
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;
client1.Send("Hej");
2013-11-19 14:21:25 +01:00
while(1)
{
client1.Recv(recvBuffer);
cout << "Client1: " << recvBuffer << endl;
2013-11-19 14:21:25 +01:00
client2.Send(recvBuffer);
client2.Recv(recvBuffer);
cout << "Client2: " << recvBuffer << endl;
2013-11-19 14:21:25 +01:00
client1.Send(recvBuffer);
}
2013-11-18 16:47:57 +01:00
ShutdownSockets();
system("pause");
return 0;
}
bool InitSockets()
{
WSADATA wsaData;
return WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR;
}
void ShutdownSockets()
{
WSACleanup();
}