Repository navigation
Expand file tree
/
Copy pathItServerSocket.cpp
More file actions
100 lines (84 loc) · 3.45 KB
/
Copy pathItServerSocket.cpp
File metadata and controls
100 lines (84 loc) · 3.45 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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <thread>
#include "libs/ItServerSocket.h"
#pragma comment(lib, "ws2_32.lib")
/**
* @brief Constructs the ItServerSocket instance.
* * Initializes the internal server state to a running condition.
*/
ItServerSocket::ItServerSocket() {
serverRun = true;
}
/**
* @brief Configures and initializes the Winsock environment and listener socket.
* * Performs WSAStartup, socket creation, address binding, and places the
* socket into a listening state. Logs initialization progress via Logger.
*/
void ItServerSocket::setUpSocket() {
WSAStartup(MAKEWORD(2, 2), &(this->wsaData));
this->ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
this->serverService.sin_family = AF_INET;
this->serverService.sin_addr.s_addr = INADDR_ANY;
this->serverService.sin_port = htons(8080);
bind(this->ListenSocket, (SOCKADDR*)&(this->serverService), sizeof(this->serverService));
listen(this->ListenSocket, SOMAXCONN);
Logger::getInstance().log("Server avviato e in ascolto sulla porta 8080...");
}
/**
* @brief Starts the main server loop to accept and process client connections.
* * Operates in a blocking loop that accepts incoming connections. Upon successful
* connection, it enters an inner loop to process data. Received data is passed
* to the provided callback function and an acknowledgment is sent back to the client.
* * @param recv_callback A pointer to a function that processes received character data.
*/
void ItServerSocket::runSocket(int (*recv_callback)(const char *)) {
while (serverRun) {
Logger::getInstance().log("In attesa di una nuova connessione...");
SOCKET ClientSocket = accept(this->ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
Logger::getInstance().log("Accept fallito.");
continue;
}
Logger::getInstance().log("Nuovo Client connesso!");
while (serverRun) {
char recvbuf[512];
int result = recv(ClientSocket, recvbuf, 511, 0);
if (result > 0) {
recvbuf[result] = '\0';
//std::cout << "Client dice: " << recvbuf << std::endl;
recv_callback(recvbuf);
const ssize_t sendSize = (result + strlen("Messaggio Ricevuto: ")) + 1;
char* sendbuf = (char *)malloc(sendSize * sizeof(char));
memset(sendbuf, 0, sendSize);
strcat(sendbuf, "Messaggio Ricevuto: ");
strcat(sendbuf, recvbuf);
send(ClientSocket, sendbuf, (int)strlen(sendbuf), 0);
free(sendbuf);
}
else if (result == 0) {
//std::cout << "Il client ha chiuso la connessione. Fine della comunicazione." << std::endl;
break;
}
else {
//std::cerr << "Errore in ricezione o client disconnesso brutalmente." << std::endl;
break;
}
}
shutdown(ClientSocket, SD_BOTH);
closesocket(ClientSocket);
}
closesocket(this->ListenSocket);
WSACleanup();
}
/**
* @brief Signals the server to terminate its operations.
* * Sets the internal `serverRun` flag to false, allowing the `runSocket` loop
* to exit gracefully once the current connection processing is finished.
*/
void ItServerSocket::closeSocket() {
this->serverRun = false;
}