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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include <iostream>
#include <vector>
#include <fstream>
#define _WIN32_WINNT 0x05010300 //Windows XP
#include <winsock2.h>
#include <ws2tcpip.h>
void pause()
{
std::cin.sync();
std::cin.ignore();
}
DWORD WorkerThread(LPVOID Arg); //NOT CURRENTLY USED
DWORD DisplayThread(LPVOID Arg);
void Broadcast(std::string Arg);
std::vector<HANDLE*> HandleVec;
std::vector<SOCKET*> SocketVec;
int main(int argc, char *argv[])
{
WSAData wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
addrinfo *result = NULL;
addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
SOCKET Listen = INVALID_SOCKET;
SOCKET Client = INVALID_SOCKET;
HANDLE NewThread = NULL;
DWORD ExitCode = 0;
if(getaddrinfo(NULL, "27015", &hints, &result) != 0)
{
std::cout << "FAILED TO GET ADDRESS\n" << WSAGetLastError();
WSACleanup();
pause();
return 1;
}
Listen = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if(Listen == INVALID_SOCKET)
{
std::cout << "FAILED TO CREATE SOCKET\n" << WSAGetLastError();
WSACleanup();
pause();
return 2;
}
if(bind(Listen, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR)
{
std::cout << "FAILED TO BIND SOCKET\n" << WSAGetLastError();
WSACleanup();
pause();
return 3;
}
freeaddrinfo(result);
if(listen(Listen, SOMAXCONN) == SOCKET_ERROR)
{
std::cout << "FAILED TO listen\n" << WSAGetLastError();
WSACleanup();
closesocket(Listen);
pause();
return 3;
}
do
{
Client = accept(Listen, NULL, NULL);
if(Client != INVALID_SOCKET)
{
NewThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &DisplayThread, (LPVOID) Client, 0, NULL);
HandleVec.push_back(&NewThread);
SocketVec.push_back(&Client);
}
for(int i = 0; i < HandleVec.size(); ++i)
{
if(GetExitCodeThread(HandleVec[i], &ExitCode) != STILL_ACTIVE)
{
CloseHandle(*HandleVec[i]);
HandleVec.erase(HandleVec.begin() + i);
}
}
}while(1);
pause();
WSACleanup();
closesocket(Listen);
return EXIT_SUCCESS;
}
DWORD WorkerThread(LPVOID Arg) //NOT CURRENTLY USED
{
SOCKET Client = (SOCKET) Arg;
int DEFAULT_BUFLEN = 512;
char RData[DEFAULT_BUFLEN];
int Result = 1;
int RBuffer = DEFAULT_BUFLEN;
DWORD ExitCode = 0;
HANDLE New = NULL;
New = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &DisplayThread, Arg, 0, NULL);
do
{
std::cin.getline(RData, 256, '\n');
if(send(Client, RData, RBuffer, 0) == SOCKET_ERROR)
{
std::cout << "\nCOULD NOT SEND\n";
closesocket(Client);
CloseHandle(New);
return 1;
}
}while(Result != 0);
closesocket(Client);
CloseHandle(New);
return 0;
}
DWORD DisplayThread(LPVOID Arg)
{
SOCKET Client = (SOCKET) Arg;
int DEFAULT_BUFLEN = 512;
char RData[DEFAULT_BUFLEN];
int Result = 0;
int RBuffer = DEFAULT_BUFLEN;
ZeroMemory(&RData, sizeof(RData));
Result = recv(Client, RData, RBuffer, 0);
std::cout << RData << std::endl;
send(Client, "close", RBuffer, 0);
return 5;
}
|