1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
/**This file contains all of the Networking code for Ergo_Server**/
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
unsigned Main::Thread(void *pArgs) //This is the MainThread, we will Initi Winsock, then create our socket.
{
WSADATA wsaData;
int ErgoServ = WSAStartup(MAKEWORD(2,2),&wsaData);
SOCKET ergoSock(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(6000);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
if(bind,(SOCKADDR*)&sin, sizeof(sin) == SOCKET_ERROR);
printf("Bind failed: %d\n", WSAGetLastError());
WSACleanup();
closesocket(ergoSock);
}
unsigned Accept::Sock(void *pArgs)//This is a secondary Main:: that will accept incomming connections.
{
int max_allowed_connections = 20;
int connections = 0;
while(connections > max_allowed_connections)
{
AcceptSocket = accept(ergoSock, NULL, NULL);
if(AcceptSocket == SOCKET_ERROR)
{
printf("Error on accepting connections:\n", WSAGetLastError()):
WSACleanup();
closesocket(ergoSock);
}
else{
connections++;
}
}
}
void Accept::Start()
{
hThread = (HANDLE)_beginthreadex(NULL, 0, &Accept::Sock, NULL, 0, &threadID);
printf("ErgoServer accepting connections with thread:\n", &threadID);
}
void Main::Start()
{
//create thread
hThread = (HANDLE) _beginthreadex(NULL, 0, &Main::Thread, NULL, 0, &threadID);
printf("ErgoServer starting with thread:\n", &threadID);
}
| |