Question: Modify the Winsock server program developed below so that it compiles and runs on the Command Line. The program should accept the server socket endpoint
Modify the Winsock server program developed below so that it compiles and runs on the Command Line. The program should accept the server socket endpoint (IP address and port number) as command-line arguments. No client program is required for now.
Code attached below. please help with modifying the code below to function as stated in the description above.
#include
#include
#include
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#define PORT "3228"
#define BUFLEN 513
int main(int argc, char *argv[])
{
//Declare and initialize variables
WSADATA wsaData;
int iResult;
struct addrinfo *result = NULL, hints;
char ipaddr[65] = "127.0.0.1";
SOCKET servSock; //server socket handle
char recvbuf[BUFLEN];
int recvbuflen = BUFLEN;
int iSendResult;
//Create a temporary SOCKET object for accepting connections from clients
SOCKET ClientSocket;//client socket handle
//Initialize Winsock library
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed: %d ", iResult);
return -1;
}
//Fill out the hints address info structure which will be passed to the getaddrinfo function as an argument
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
iResult = getaddrinfo(ipaddr, PORT, &hints, &result);
if (iResult != 0)
{
printf("getaddrinfo failed: %d ", iResult);
WSACleanup();
return -1;
}
//Create a SOCKET for listening for incoming connection requests
servSock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (servSock == INVALID_SOCKET)
{
printf("socket failed with error: %d ", WSAGetLastError());
WSACleanup();
return -1;
}
iResult = bind(servSock, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
printf("bind failed. %d ", WSAGetLastError());
freeaddrinfo(result);
closesocket(servSock);
WSACleanup();
return -1;
}
/*Once the bind function is called, the address information returned by the getaddrinfo function is no longer needed
Therefore, it is OK to free the memory allocated by the getaddrinfo function for this address information*/
freeaddrinfo(result);
//Listen for incoming connection requests on the created socket
//SOMAXCONN is the value for the backlog which is the maximum pending connections to accept
iResult = listen(servSock, SOMAXCONN);
if (iResult == SOCKET_ERROR)
{
printf("listen function failed. %d ", WSAGetLastError());
closesocket(servSock);
WSACleanup();
return -1;
}
printf("Listening on socket: %s:%s ", ipaddr, PORT);
//Accept a client socket
ClientSocket = accept(servSock, NULL, NULL);
if (ClientSocket == INVALID_SOCKET)
{
printf("accept failed: %d ", WSAGetLastError());
closesocket(servSock);
WSACleanup();
return -1;
}
iResult = recv(ClientSocket, recvbuf, recvbuflen - 1, 0);
if (iResult > 0)
{
printf("Bytes Received: %d ", iResult);
recvbuf[iResult] = '\0';
//Echo the buffer back to the sender
iSendResult = send(ClientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR)
{
printf("send failed: %d ", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return -1;
}
}
else if (iResult == 0)
printf("Connection closing... ");
else
{
printf("recv failed: %d ", WSAGetLastError());
closesocket(ClientSocket);
WSACleanup();
return -1;
}
closesocket(ClientSocket);
closesocket(servSock);
WSACleanup();
printf("Closing the connection gracefully... ");
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
