-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
48 lines (38 loc) · 1.1 KB
/
main.cpp
File metadata and controls
48 lines (38 loc) · 1.1 KB
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
#if defined(_WIN32) || defined(_WIN64)
#include <WinSock2.h>
#include <WS2tcpip.h>
#endif
#include <stdio.h>
#include <iostream>
#include <thread>
#include "server.cpp"
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char* argv[])
{
WSADATA wsaData;
int iResult;
// Parse command-line arguments for UDP flag
bool useUdp = false;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-u") == 0 || strcmp(argv[i], "--udp") == 0) {
useUdp = true;
}
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
Protocol proto = useUdp ? Protocol::UDP : Protocol::TCP;
std::cout << "Starting server with protocol: " << (useUdp ? "UDP" : "TCP") << std::endl;
// Create and start the server on port 7
Server server("127.0.0.1", 7, proto);
// Run server in a separate thread
std::thread serverThread(&Server::Start, &server);
std::cin.get();
server.Stop();
serverThread.join();
WSACleanup();
return 0;
}