1#define _CRT_SECURE_NO_WARNINGS
11#pragma comment(lib,"ws2_32.lib")
12#pragma comment(lib,"Synchronization.lib")
16#define DEFAULT_BUFLEN 1024
17#define DEFAULT_PORT "8999"
22void send_to_socket(ptrCLIENTINFO info,
char* buf,
int len)
25 int iSendResult = send(info->CLIENT.sockClient.clientSocket, buf, len, 0);
26 if (iSendResult == SOCKET_ERROR)
28 printf(
"send failed with error: %d\n", WSAGetLastError());
29 closesocket(info->CLIENT.sockClient.clientSocket);
33DWORD WINAPI listen_for_client(LPVOID lpParam)
35 struct addrinfo hints;
37 struct addrinfo *result = NULL;
39 ZeroMemory(&hints,
sizeof(hints));
40 hints.ai_family = AF_INET;
41 hints.ai_socktype = SOCK_STREAM;
42 hints.ai_protocol = IPPROTO_TCP;
43 hints.ai_flags = AI_PASSIVE;
46 iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
49 fprintf(stderr,
"getaddrinfo failed with error: %d\n", iResult);
55 listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
56 if (listenSocket == INVALID_SOCKET)
58 fprintf(stderr,
"socket failed with error: %ld\n", WSAGetLastError());
65 iResult = bind(listenSocket, result->ai_addr, (
int)result->ai_addrlen);
66 if (iResult == SOCKET_ERROR)
68 fprintf(stderr,
"bind failed with error: %d\n", WSAGetLastError());
70 closesocket(listenSocket);
78 char recvbuf[DEFAULT_BUFLEN];
79 int recvbuflen = DEFAULT_BUFLEN;
82 iResult = listen(listenSocket, SOMAXCONN);
83 if (iResult == SOCKET_ERROR)
85 fprintf(stderr,
"listen failed with error: %d\n", WSAGetLastError());
86 closesocket(listenSocket);
92 clientSocket = accept(listenSocket, NULL, NULL);
93 if (clientSocket == INVALID_SOCKET)
95 fprintf(stderr,
"accept failed with error: %d\n", WSAGetLastError());
96 closesocket(listenSocket);
101 iResult = recv(clientSocket, recvbuf, recvbuflen, 0);
105 add_new_socket_client(clientSocket, recvbuf);
109 fprintf(stderr,
"recv failed with error: %d\n", WSAGetLastError());
110 closesocket(clientSocket);
118 printf(
"\nInitialising Winsock...");
119 if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
121 fprintf(stderr,
"Failed. Error Code : %d\n", WSAGetLastError());
124 printf(
"Initialized.\n");
126 CreateThread(NULL, 1024, listen_for_client, NULL,
false, &listenThreadId);
130DWORD WINAPI command_recv_process(LPVOID lpParam)
132 ptrCLIENTINFO clientInfo = lpParam;
133 SOCKCLIENTINFO sockClient = clientInfo->CLIENT.sockClient;
135 char recvbuf[DEFAULT_BUFLEN];
136 int recvbuflen = DEFAULT_BUFLEN;
142 iResult = recv(sockClient.clientSocket, recvbuf, recvbuflen, 0);
145 process_command(clientInfo, recvbuf, iResult);
146 WaitOnAddress(&sockClient.iswait,
147 &sockClient.dontUseMe,
148 sizeof sockClient.dontUseMe,
150 send_to_socket(clientInfo,
"__continue__", (
int)(strlen(
"__continue__")) + 1);
152 else if (iResult == 0)
153 fprintf(stderr,
"Connection closing...\n");
156 fprintf(stderr,
"recv failed with error: %d\n", WSAGetLastError());
157 closesocket(sockClient.clientSocket);
161 }
while (iResult > 0);
164 iResult = shutdown(sockClient.clientSocket, SD_BOTH);
165 if (iResult == SOCKET_ERROR)
167 fprintf(stderr,
"shutdown failed with error: %d\n", WSAGetLastError());
168 closesocket(sockClient.clientSocket);
173 closesocket(sockClient.clientSocket);