Question: server #include #include #include #include #include #include #pragma comment ( lib , Ws 2 _ 3 2 . lib ) #define DEFAULT _ BUFLEN

server
#include
#include
#include
#include
#include
#include
#pragma comment(lib,"Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
#define MAX_CLIENTS 10
std::vector ClientSockets;
std::mutex mtx;
int __cdecl main(void){
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
struct addrinfo *result = NULL, hints;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
fd_set readfds;
int max_sd, sd, activity;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult !=0){
printf("WSAStartup failed with error: %d
", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult !=0){
printf("getaddrinfo failed with error: %d
", iResult);
WSACleanup();
return 1;
}
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET){
printf("socket failed with error: %ld
", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
u_long mode =1; //1 to enable non-blocking mode
iResult = ioctlsocket(ListenSocket, FIONBIO, &mode);
if (iResult == SOCKET_ERROR){
printf("ioctlsocket failed with error: %d
", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR){
printf("bind failed with error: %d
", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR){
printf("listen failed with error: %d
", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
printf("Server is running. Waiting for connections...
");
while (1){
FD_ZERO(&readfds);
FD_SET(ListenSocket, &readfds);
max_sd = ListenSocket;
// Lock the mutex before accessing the shared list
mtx.lock();
for (SOCKET &sock : ClientSockets){
sd = sock;
if (sd >0) FD_SET(sd, &readfds);
if (sd > max_sd) max_sd = sd;
}
mtx.unlock(); // Unlock the mutex after accessing the shared list
activity = select(max_sd +1, &readfds, NULL, NULL, NULL);
if (activity == SOCKET_ERROR){
printf("select failed with error: %d
", WSAGetLastError());
break;
}
if (FD_ISSET(ListenSocket, &readfds)){
SOCKET ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET){
printf("accept failed with error: %d
", WSAGetLastError());
continue;
}
u_long mode =1; //1 to enable non-blocking mode
iResult = ioctlsocket(ClientSocket, FIONBIO, &mode);
if (iResult == SOCKET_ERROR){
printf("ioctlsocket failed with error: %d
", WSAGetLastError());
closesocket(ClientSocket);
continue;
}
printf("New connection accepted
");
// Lock the mutex before modifying the shared list
mtx.lock();
ClientSockets.push_back(ClientSocket);
mtx.unlock(); // Unlock the mutex after modification
}
mtx.lock();
for (auto it = ClientSockets.begin(); it != ClientSockets.end(); ){
sd =*it;
if (FD_ISSET(sd, &readfds)){
iResult = recv(sd, recvbuf, recvbuflen, 0);
if (iResult >0){
recvbuf[iResult]='\0';
printf("Client %d: %s
",(int)(it - ClientSockets.begin()), recvbuf);
send(sd, recvbuf, iResult, 0);
} else if (iResult ==0){
printf("Client %d disconnected
",(int)(it - ClientSockets.begin()));
closesocket(sd);
it = ClientSockets.erase(it); // Erase the client from the list
continue;
} else {
printf("recv failed with error: %d
", WSAGetLastError());
closesocket(sd);
it = ClientSockets.erase(it); // Erase the client from the list
continue;
}
}
++it;
}
mtx.unlock();
}
closesocket(ListenSocket);
mtx.lock();
for (SOCKET &sock : ClientSockets){
if (sock != INVALID_SOCKET){
closesocket(sock);
}
}
mtx.unlock();
WSACleanup();
return 0;
}
client
#include
#include
#include
#include
#include
#pragma comment(lib,"Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int __cdecl main(int argc, char **argv){
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL, *ptr = NULL, hints;
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
if (argc !=2){
printf("usage: %s server-name
", argv[0]);
return 1;
}
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult !=0){
printf("WSAStartup failed with error: %d
", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
if (iResult !=0){
printf("getaddrinfo failed with error: %d
", iResult);
WSACleanup();
return 1;
}
for (ptr = result; ptr != NULL; ptr = ptr->ai_next){
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET){
printf("socket failed with error: %ld
", WSAGetLastError());
WSACleanup();
return 1;
}
u_long mode =1; //1 to enable non-blocking mode
iResult = ioctlsocket(ConnectSocket, FIONBIO, &mode);
if (iResult == SOCKET_ERROR){
printf("ioctlsocket failed with error: %d
", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!