-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
73 lines (72 loc) · 2.25 KB
/
server.c
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
#include <winsock2.h>
#include <ini.h>
int WINAPI WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPTSTR cmd_line, int cmd_show) {
LPWSTR command_line = GetCommandLineW();
int argc;
LPWSTR *argv = CommandLineToArgvW(command_line, &argc);
char *port = NULL;
if (argv != NULL && argc == 3) {
port = argv[1];
} else {
ini_t *config = ini_load("config.ini");
if (config == NULL) {
MessageBox(NULL, "Configuration file not found", "Error", MB_OK | MB_ICONERROR);
ExitProcess(1);
}
port = ini_get(config, NULL, "port");
if (!port) {
MessageBox(NULL, "Invalid configuration file", "Error", MB_OK | MB_ICONERROR);
ExitProcess(1);
}
}
WSADATA wsa_data;
if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0) {
MessageBox(NULL, "WSAStartup failed", "Error", MB_OK | MB_ICONERROR);
ExitProcess(1);
}
SOCKET server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == INVALID_SOCKET) {
MessageBox(NULL, "Failed to create socket", "Error", MB_OK | MB_ICONERROR);
WSACleanup();
ExitProcess(1);
}
struct sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(atoi(port));
server_address.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == SOCKET_ERROR) {
MessageBox(NULL, "Bind failed", "Error", MB_OK | MB_ICONERROR);
closesocket(server_socket);
WSACleanup();
ExitProcess(1);
}
if (listen(server_socket, SOMAXCONN) == SOCKET_ERROR) {
MessageBox(NULL, "Listen failed", "Error", MB_OK | MB_ICONERROR);
closesocket(server_socket);
WSACleanup();
ExitProcess(1);
}
while (1) {
SOCKET client_socket = accept(server_socket, NULL, NULL);
if (client_socket == INVALID_SOCKET) {
MessageBox(NULL, "Accept failed", "Error", MB_OK | MB_ICONERROR);
closesocket(server_socket);
WSACleanup();
ExitProcess(1);
}
char buffer[1024];
int bytes_received;
while ((bytes_received = recv(client_socket, buffer, sizeof(buffer), 0)) > 0) {
buffer[bytes_received] = '\0';
if (strcmp(buffer, "kill") == 0) {
const char* command = "start nvda -r";
system(command);
}
}
closesocket(client_socket);
}
closesocket(server_socket);
WSACleanup();
ExitProcess(0);;
}