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
|
#include<iostream>
#include<string>
#include<sstream>
#include<Ws2tcpip.h> //used instead of "Winsock2.h"
//using namespace std;
#define MAX_DATA 1024 //maximum bytes could be sent or received
void initiateWinsockFacility()
{
WORD wVersionRequested;
WSADATA wsaData;
int error;
wVersionRequested = MAKEWORD(2, 2);
error = WSAStartup(wVersionRequested, &wsaData);
if (error != 0)
{
std::cerr<<"WSAStartup failed with error: "<<error<<std::endl;
system("pause");
exit(-1);
}
}
//output, to serverInfo, full information about the connection to be made
void getFullAddressInformation(std::string ipAddress, std::string portNumber, const struct addrinfo giveHints, struct addrinfo *receiveServerInfo)
{
int status = getaddrinfo(ipAddress.c_str(), portNumber.c_str(), &giveHints, &receiveServerInfo);
if(status != 0)
{
std::cerr<<"getaddrinfo() error: "<< gai_strerror(status) <<std::endl;
WSACleanup();
system("pause");
exit(-1);
}
}
int main()
{
struct addrinfo hints; //input connection type and ip type
struct addrinfo *serverInfo = NULL; //holds full address information of a server
SOCKET socketFileDescriptor;
char userInput[MAX_DATA];
char serverResponse[MAX_DATA];
std::string ip;
std::string port;
initiateWinsockFacility();
system("cls");
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
std::cout<<"Please enter server's IP: ";
getline(std::cin, ip);
std::cout<<"Please enter port's number: ";
getline(std::cin, port);
std::cout<<"SOMETHING GOES WRONG IN HERE!"<<std::endl;
system("pause");
getFullAddressInformation(ip, port, hints, serverInfo);
try
{
socketFileDescriptor = socket(serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol);
if(socketFileDescriptor == INVALID_SOCKET )
{
std::cerr<<"socket() error: " << WSAGetLastError() <<std::endl;
throw 1;
}
std::cout<<"connecting to the server ..."<<std::endl;
int connectStatus = connect(socketFileDescriptor, serverInfo->ai_addr, serverInfo->ai_addrlen);
if(connectStatus == SOCKET_ERROR)
{
std::cerr<<"connect() error: " << WSAGetLastError() <<std::endl;
throw 2;
}
//send and receive messages
while(true)
{
std::cout<<"\nyou: ";
std::cin.getline(userInput, MAX_DATA);
if(std::cin.fail())
{
std::cerr<<"Overflow occured. Restart Application"<<std::endl;
throw 2;
}
int bytes_sent = send(socketFileDescriptor, userInput, strlen(userInput), 0);
//chack whther bytes are sent fully or not
while(bytes_sent != strlen(userInput))
{
//if bytes not sent fully, then see if its equal to socket error or not
//if it is, then throw an error. If it isn't then ask user to resend data
if(bytes_sent == SOCKET_ERROR)
{
std::cerr<<"send() error: " << WSAGetLastError() <<std::endl;
throw 2;
}
//re-send
std::cout<<"\nyou: ";
std::cin.getline(userInput, MAX_DATA);
if(std::cin.fail())
{
std::cerr<<"Overflow occured. Restart Application"<<std::endl;
throw 2;
}
int bytes_sent = send(socketFileDescriptor, userInput, strlen(userInput), 0);
}
int bytes_received = recv(socketFileDescriptor, serverResponse, MAX_DATA, 0);
//make sure the returned value is socket error and not zero
if((bytes_received == SOCKET_ERROR) && (bytes_received != 0))
{
std::cerr<<"recv() error: " << WSAGetLastError() <<std::endl;
throw 2;
}else{ //returned value is zero which means the server shut down
std::cout<<"server shut down"<< std::endl;
break;
}
std::string echoedMsg(serverResponse, bytes_received);
std::cout<<"server: "<< echoedMsg << std::endl;
}
}
catch(int v)
{
if(v == 1)
{
freeaddrinfo(serverInfo);
WSACleanup();
system("pause");
exit(-1);
}
if(v == 2)
{
closesocket(socketFileDescriptor);
freeaddrinfo(serverInfo);
WSACleanup();
system("pause");
exit(-1);
}
}
closesocket(socketFileDescriptor);
freeaddrinfo(serverInfo);
WSACleanup();
system("pause");
return 0;
}
|