-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
179 lines (147 loc) · 5.97 KB
/
server.cpp
File metadata and controls
179 lines (147 loc) · 5.97 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
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
168
169
170
171
172
173
174
175
176
177
178
179
#if defined(_WIN32) || defined(_WIN64)
#include <WinSock2.h>
#include <WS2tcpip.h>
#endif
#include <stdio.h>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <mutex>
#pragma comment(lib, "ws2_32.lib")
enum Protocol {
TCP = 0,
UDP = 1
};
class Server {
public:
Server(const std::string& iIp = "127.0.0.1", unsigned int iPort = 7, Protocol iProtocol = Protocol::TCP)
: _ip(iIp), _port(iPort), _proto(iProtocol), _serverSocket(INVALID_SOCKET), _running(false) {
std::cout << "Initializing server on " << _ip << ":" << _port << "..." << std::endl;
}
~Server() {
Shutdown();
}
bool Start() {
// Create socket based on protocol
int socketType = (_proto == Protocol::TCP) ? SOCK_STREAM : SOCK_DGRAM;
_serverSocket = socket(AF_INET, socketType, (_proto == Protocol::TCP) ? IPPROTO_TCP : IPPROTO_UDP);
if (_serverSocket == INVALID_SOCKET) {
std::cerr << "Socket creation failed: " << WSAGetLastError() << std::endl;
return false;
}
// Setup address structure
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(_port);
// Convert IP string to address
if (inet_pton(AF_INET, _ip.c_str(), &serverAddr.sin_addr) <= 0) {
std::cerr << "Invalid IP address: " << _ip << std::endl;
closesocket(_serverSocket);
return false;
}
// Bind socket
if (bind(_serverSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
std::cerr << "Bind failed: " << WSAGetLastError() << std::endl;
closesocket(_serverSocket);
return false;
}
// Behavior differs for TCP and UDP
if (_proto == Protocol::TCP) {
if (listen(_serverSocket, SOMAXCONN) == SOCKET_ERROR) {
std::cerr << "Listen failed: " << WSAGetLastError() << std::endl;
closesocket(_serverSocket);
return false;
}
std::cout << "Server listening on " << _ip << ":" << _port << " (TCP)" << std::endl;
_running = true;
// Accept connections in a loop
while (_running) {
sockaddr_in clientAddr;
int clientAddrLen = sizeof(clientAddr);
SOCKET clientSocket = accept(_serverSocket, (sockaddr*)&clientAddr, &clientAddrLen);
if (clientSocket == INVALID_SOCKET) {
if (_running) {
std::cerr << "Accept failed: " << WSAGetLastError() << std::endl;
}
break;
}
// Get client IP address
char clientIP[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, INET_ADDRSTRLEN);
std::cout << "Connection accepted from " << clientIP << ":" << ntohs(clientAddr.sin_port) << std::endl;
// Create a thread to handle this client
std::thread clientThread(&Server::HandleClient, this, clientSocket, std::string(clientIP), ntohs(clientAddr.sin_port));
clientThread.detach();
}
} else if (_proto == Protocol::UDP) {
std::cout << "Server listening on " << _ip << ":" << _port << " (UDP)" << std::endl;
_running = true;
const int BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
while (_running) {
sockaddr_in clientAddr;
int clientAddrLen = sizeof(clientAddr);
int recvSize = recvfrom(_serverSocket, buffer, BUFFER_SIZE, 0,
(sockaddr*)&clientAddr, &clientAddrLen);
if (recvSize == SOCKET_ERROR) {
if (_running) {
std::cerr << "recvfrom failed: " << WSAGetLastError() << std::endl;
}
break;
}
if (recvSize > 0) {
// Echo back to sender
buffer[recvSize < BUFFER_SIZE ? recvSize : BUFFER_SIZE-1] = '\0';
int sent = sendto(_serverSocket, buffer, recvSize, 0,
(sockaddr*)&clientAddr, clientAddrLen);
if (sent == SOCKET_ERROR) {
std::cerr << "sendto failed: " << WSAGetLastError() << std::endl;
break;
}
}
}
}
return true;
}
void Stop() {
_running = false;
if (_serverSocket != INVALID_SOCKET) {
closesocket(_serverSocket);
_serverSocket = INVALID_SOCKET;
}
}
void Shutdown() {
Stop();
}
private:
void HandleClient(SOCKET clientSocket, const std::string& clientIP, unsigned int clientPort) {
const int BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
std::cout << "Handling client " << clientIP << ":" << clientPort << std::endl;
// Simple echo loop - receive and send back
while (_running) {
int recvSize = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0);
if (recvSize == SOCKET_ERROR) {
std::cerr << "Recv failed for " << clientIP << ":" << clientPort << std::endl;
break;
}
if (recvSize == 0) {
std::cout << "Client " << clientIP << ":" << clientPort << " disconnected" << std::endl;
break;
}
buffer[recvSize] = '\0';
// Echo back to client
if (send(clientSocket, buffer, recvSize, 0) == SOCKET_ERROR) {
std::cerr << "Send failed for " << clientIP << ":" << clientPort << std::endl;
break;
}
}
closesocket(clientSocket);
}
std::string _ip;
unsigned int _port = 7;
Protocol _proto;
SOCKET _serverSocket;
bool _running;
};