Question: Write a TCP client/server software system using c++ that can exchange multiple lines of text. The server should accept messages from the client, print the
Write a TCP client/server software system using c++ that can exchange multiple lines of text. The server should accept messages from the client, print the messages on the standard output, and then echo back each and every one of them. The main protocol for this service is that client and server must alternate between sending and receiving messages (with the client initiating the process). The messages from the client must be generated from the keyboard. The user should be able to send as many messages as he/she sees fit until the string "CLOSE" is sent. This string is the final data indicating when the dialogue is to cease. After sending the "CLOSE" message, the client can immediately initiate a connection closedown. Upon receipt of the "CLOSE" message, the server should also close its connection to the client.
-------Following are the programs I have from the class-------
//server program
#include
#include
#include
using namespace std;
#pragma comment(lib, "Ws2_32.lib") // link project
#define PORT "3228" // above 1024
#define BUFLEN 513
int main(int argc, char *argv[])
{
// initialize
int iResult;
WSADATA wsaData;
struct addrinfo *result = NULL, hints;
char ipaddr[65] = "localhost"; // loopback address or can put "localhost" instead of loopback
SOCKET servSock; //socket handle
SOCKET ClientSocket;
char recvbuf[BUFLEN];
int recvbuflen = BUFLEN;
int iSendResult;
//initialize the winsock library
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d ", iResult);
return 1;
}
//the sockaddr_in structure specifies the address family
//ip address, and prot for the socket that is being bound
memset(&hints, 0, sizeof(hints)); //in bytes, create memory space
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; //tcP use this type
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE; //bind socket
iResult = getaddrinfo(ipaddr, PORT, &hints, &result);
if (iResult != 0){
printf("getaddrinfo faild! with an error: %d ", iResult);
WSACleanup();
return -1;
}
//create socket
servSock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (servSock == INVALID_SOCKET){
printf("socket failed with an error: %d ", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return -1;
}
//bind socket
iResult = bind(servSock, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed. %d ", WSAGetLastError());
closesocket(servSock);
WSACleanup();
return -1;
}
//listen for incoming connectionn requests on the created socket
//samaxconn 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 with error: %d ", WSAGetLastError());
closesocket(servSock);
WSACleanup();
return -1;
}
printf("Listening on socket: %s:%s ", ipaddr, PORT);
//accept the 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';
iSendResult = send(ClientSocket, recvbuf, iResult, 0);
if (iSendResult == INVALID_SOCKET){
printf("accept 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("colsing the connection gracefully.... ");
system("PAUSE");
return 0;
}
//client program
#include
#include
#include
using namespace std;
#pragma comment(lib, "Ws2_32.lib") // link project to library
#define PORT "3228" // can be used above 1024, below that are reserved
#define BUFFERSIZE 129
#define BUFLEN 513
int main()
{
// initialize
int iResult;
WSADATA wsaData;
struct addrinfo *result = NULL, hints; //point to nothing
char serverIP[64] = "localhost"; //hostname buffer
SOCKET sock;
char sendBuffer[BUFFERSIZE];
char recvBuffer[BUFFERSIZE];
int recvbuflen = BUFFERSIZE;
//initialize the winsock library
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d ", iResult);
return 1;
}
//fill out the hints address info sturcture which is passed to getaddrinfo() function
memset(&hints, 0, sizeof(hints)); //in bytes, create memory space
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; //tcP use this type
hints.ai_protocol = IPPROTO_TCP;
iResult = getaddrinfo(serverIP, PORT, &hints, &result);
if (iResult != 0){
printf("getaddrinfo faild! with an error: %d ", iResult);
WSACleanup();
return -1;
}
//create socket for connection to our own server
sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (sock == INVALID_SOCKET){
printf("socket failed with an error: %d ", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return -1;
}
//create socket to connect
iResult = connect(sock, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed. %d ", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
freeaddrinfo(result);
strcpy_s(sendBuffer, "Hello server");
iResult = send(sock, sendBuffer, (int)strlen(sendBuffer), 0);
if (iResult == SOCKET_ERROR){
printf("send failed with error: %d ", WSAGetLastError());
closesocket(sock);
WSACleanup();
return -1;
}
printf("Number of bytes sent: %ld ", iResult);
iResult = shutdown(sock, SD_SEND);
if (iResult == SOCKET_ERROR){
printf("shutdown failed with error", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
iResult = recv(sock, recvBuffer, recvbuflen - 1, 0);
if (iResult > 0){
printf("Bytes received: %d ", iResult);
recvBuffer[iResult] = '\0';
printf("%s ", recvBuffer);
}
closesocket(sock);
WSACleanup();
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
