Question: Apply non - blocking and multiple client connection to this Client Server codes by using Select ( ) method. The client and server should be

Apply non-blocking and multiple client connection to this Client Server codes by using Select() method. The client and server should be able to send messages to each other whenever they want. They shouldn't have to send them in order. I don't prefer it for now, but if necessary, you can use a single thread structure. The kick and quit should continue to work as below.
//Client
#define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include
// Need to link with Ws2_32.lib
#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;
}
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR){
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET){
printf("Unable to connect to server!
");
WSACleanup();
return 1;
}
char sendbuf[DEFAULT_BUFLEN];
printf("Connected to server. Type 'quit' to exit.
");
while (1){
printf("Enter message: ");
fgets(sendbuf, DEFAULT_BUFLEN, stdin);
sendbuf[strcspn(sendbuf,"
")]=0;
iResult = send(ConnectSocket, sendbuf,(int)strlen(sendbuf),0);
if (iResult == SOCKET_ERROR){
printf("send failed with error: %d
", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
if (strcmp(sendbuf, "quit")==0){
printf("Client exiting...
");
break;
}
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult >0){
recvbuf[iResult]='\0';
printf("Server: %s
", recvbuf);
if (strcmp(recvbuf, "kick")==0){
printf("Kicked by server.
");
break;
}
} else if (iResult ==0){
printf("Connection closed
");
break;
} else {
printf("recv failed with error: %d
", WSAGetLastError());
break;
}
}
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
// Server
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include
// Need to link with Ws2_32.lib
#pragma comment(lib,"Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
int __cdecl main(void)
{
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL, hints;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
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;
}
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("Waiting for client to connect...
");
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET){
printf("accept failed with error: %d
", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
closesocket(ListenSocket);
char sendbuf[DEFAULT_BUFLEN];
printf("Client connected. Type 'kick' to disconnect the client.
");
while (1){
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (iResult >0){
recvbuf[iResult]='\0';
printf("Client: %s
", recvbuf);
if (strcmp(recvbuf, "quit")==0){
printf("Client disconnected.
");
break;
}
printf("Enter message: ");
fgets(sendbuf, DEFAULT_BUFLEN, stdin);
sendbuf[strcspn(sendbuf,"
")]=0;
iResult = send(ClientSocket, sendbuf,(int)strlen(sendbuf),0);
if (iResult == SOCKET_ERROR){
printf("send failed with error: %d
", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return 1;
}
if (strcmp(sendbuf, "kick")==0){
printf("Client kicked.
");
break;
}

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 Finance Questions!