-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
203 lines (172 loc) · 6.58 KB
/
server.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "socketutil.h"
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct AcceptedSocket {
int acceptedSocketFD;
int error;
bool acceptedSuccessfully;
};
AcceptedSocket acceptedSockets[10];
int acceptedSocketCount = 0;
struct ClientInfo {
int socketFD;
string name;
};
vector<ClientInfo> activeClients;
void receiveAndPrintIncomingDataOnSeparateThread(AcceptedSocket* pSocket);
void freeAcceptedSocket(AcceptedSocket* pSocket);
static void* _receiveAndPrintIncomingDataWrapper(void* arg);
void receiveAndPrintIncomingData(int socketFD);
void sendReceivedMessageToTheOtherClients(char* buffer, int senderSocketFD);
void removeClientFromActiveClients(int socketFD);
AcceptedSocket* acceptIncomingConnection(int serverSocketFD) {
struct sockaddr_in clientAddress;
int clientAddressSize = sizeof(struct sockaddr_in);
int clientSocketFD = accept(serverSocketFD, (struct sockaddr*)&clientAddress, (socklen_t*)&clientAddressSize);
AcceptedSocket* acceptedSocket = new AcceptedSocket();
acceptedSocket->acceptedSocketFD = clientSocketFD;
acceptedSocket->acceptedSuccessfully = clientSocketFD > 0;
if (!acceptedSocket->acceptedSuccessfully) {
acceptedSocket->error = clientSocketFD;
}
else {
char name[256];
ssize_t bytesReceived = recv(clientSocketFD, name, sizeof(name) - 1, 0);
if (bytesReceived > 0) {
name[bytesReceived] = '\0';
ClientInfo newClient = { clientSocketFD, string(name) };
activeClients.push_back(newClient);
}
}
return acceptedSocket;
}
void startAcceptingIncomingConnections(int serverSocketFD) {
while (true) {
AcceptedSocket* clientSocket = acceptIncomingConnection(serverSocketFD);
if (clientSocket->acceptedSuccessfully) {
acceptedSockets[acceptedSocketCount++] = *clientSocket;
receiveAndPrintIncomingDataOnSeparateThread(clientSocket);
freeAcceptedSocket(clientSocket);
}
else {
// Handle error condition
freeAcceptedSocket(clientSocket);
}
// Add a condition to break out of the loop if needed
// For example, you could check for a specific signal or user input
}
}
static void* _receiveAndPrintIncomingDataWrapper(void* arg) {
int* socketFD = (int*)arg;
receiveAndPrintIncomingData(*socketFD);
delete socketFD;
pthread_exit(NULL);
return NULL;
}
void receiveAndPrintIncomingDataOnSeparateThread(AcceptedSocket* pSocket) {
pthread_t id;
int* socketFD = new int(pSocket->acceptedSocketFD);
pthread_create(&id, NULL, _receiveAndPrintIncomingDataWrapper, socketFD);
pthread_detach(id);
}
void receiveAndPrintIncomingData(int socketFD) {
char buffer[1024];
while (true) {
ssize_t amountReceived = recv(socketFD, buffer, 1024, 0);
if (amountReceived > 0) {
buffer[amountReceived] = 0;
cout << buffer << endl;
sendReceivedMessageToTheOtherClients(buffer, socketFD);
}
if (amountReceived == 0) {
removeClientFromActiveClients(socketFD);
break;
}
}
close(socketFD);
}
void sendPrivateMessage(char* buffer, int senderSocketFD, string recipientName) {
string message(buffer);
transform(recipientName.begin(), recipientName.end(), recipientName.begin(), ::tolower);
ClientInfo* senderClient = nullptr;
for (auto& client : activeClients) {
if (client.socketFD == senderSocketFD) {
senderClient = &client;
break;
}
}
if (senderClient != nullptr) {
string senderName = senderClient->name;
string privateMessage = "Private message from " + senderName + ": " + message;
for (const auto& client : activeClients) {
string lowercaseName = client.name;
transform(lowercaseName.begin(), lowercaseName.end(), lowercaseName.begin(), ::tolower);
if (lowercaseName == recipientName) {
send(client.socketFD, privateMessage.c_str(), privateMessage.length(), 0);
return;
}
}
}
string errorMessage = "Error: Recipient '" + recipientName + "' not found.";
send(senderSocketFD, errorMessage.c_str(), errorMessage.length(), 0);
}
void sendReceivedMessageToTheOtherClients(char* buffer, int senderSocketFD) {
string message(buffer);
if (message == "showmembers") {
string memberList = "Active members:\n";
for (const auto& client : activeClients) {
memberList += client.name + "\n";
}
send(senderSocketFD, memberList.c_str(), memberList.length(), 0);
} else if (message.find("exit") != string::npos) {
removeClientFromActiveClients(senderSocketFD);
} else if (message.find("/pm ") == 0) {
size_t recipientNameEnd = message.find(' ', 4);
if (recipientNameEnd != string::npos) {
string recipientName = message.substr(4, recipientNameEnd - 4);
string privateMessage = message.substr(recipientNameEnd + 1);
sendPrivateMessage(const_cast<char*>(privateMessage.c_str()), senderSocketFD, recipientName);
} else {
string errorMessage = "Error: Invalid private message format. Usage: /pm <recipient> <message>";
send(senderSocketFD, errorMessage.c_str(), errorMessage.length(), 0);
}
}
else {
for (int i = 0; i < acceptedSocketCount; i++) {
int recipientSocketFD = acceptedSockets[i].acceptedSocketFD;
if (recipientSocketFD != senderSocketFD) {
send(recipientSocketFD, buffer, strlen(buffer), 0);
}
}
}
}
void removeClientFromActiveClients(int socketFD) {
auto it = find_if(activeClients.begin(), activeClients.end(), [&](const ClientInfo& client) {
return client.socketFD == socketFD;
});
if (it != activeClients.end()) {
activeClients.erase(it);
}
}
void freeAcceptedSocket(AcceptedSocket* pSocket) {
delete pSocket;
}
int main() {
int serverSocketFD = createTCPIpv4Socket();
char ip[] = "";
struct sockaddr_in* serverAddress = createIPv4Address(ip, 2000);
int result = bind(serverSocketFD, (struct sockaddr*)serverAddress, sizeof(struct sockaddr_in));
if (result == 0) {
cout << "Socket was bound successfully" << endl;
}
int listenResult = listen(serverSocketFD, 10);
startAcceptingIncomingConnections(serverSocketFD);
shutdown(serverSocketFD, SHUT_RDWR);
return 0;
}