So I followed the section on creating a basic blocking server, and everything works except for a WSAECONNRESET error. I ran the source code available to download and that didn't work either. I've done a little bit of research on this error, and it sounds like there are a lot of causes. I'm not sure what information would be useful for you guys, so just ask if you need something.
Thanks,
Scott
#include <iostream>
#include <string>
#include <sstream>
#include <winsock2.h>
#include <windows.h>
using namespace std;
class ROTException
{
public:
ROTException() :
m_pMessage("") {}
virtual ~ROTException() {}
ROTException(const char *pMessage) :
m_pMessage(pMessage) {}
const char *what(){ return m_pMessage;}
private:
const char *m_pMessage;
};
const int REQ_WINSOCK_VER = 2;
const int DEFAULT_PORT = 4444;
const int TEMP_BUFFER_SIZE = 128;
//Bind Socket
void setServerSockAddr(sockaddr_in *pSockAddr, int portNumber)
{
pSockAddr->sin_family = AF_INET;
pSockAddr->sin_port = htons(portNumber);
pSockAddr->sin_addr.S_un.S_addr = INADDR_ANY;
}
//Return string about host
string getHostDescription(const sockaddr_in &sockAddr)
{
ostringstream stream;
stream << inet_ntoa(sockAddr.sin_addr) << ":" << ntohs(sockAddr.sin_port);
return stream.str();
}
void rot13(char *pBuffer, int size)
{
for(int i=0;i<size;i++)
{
char c = pBuffer;
if ((c >= 'a' && c < 'n') || (c >= 'A' && c < 'N') )
c += 13;
else if ((c>='n' && c <= 'z') || (c>='N' && c <= 'Z'))
c -= 13;
else
continue;
pBuffer = c;
}
}
//Handle Clients
void handleConnection(SOCKET hClientSocket, const sockaddr_in &sockAddr)
{
cout << "Connected with " << getHostDescription(sockAddr) << ".\n";
char tempBuffer[TEMP_BUFFER_SIZE];
//Interact with Client
while(true){
int retval;
retval = recv(hClientSocket, tempBuffer, sizeof(tempBuffer), 0);
if(retval == 0)
break;
else if(retval == SOCKET_ERROR){ //This is where the error occurs. I have allowed the program to continue.
if(WSAGetLastError() != WSAECONNRESET)
throw ROTException("Socket error #1 while receiving.");
}else{
rot13(tempBuffer, retval);
if(send(hClientSocket, tempBuffer, retval, 0) == SOCKET_ERROR)
throw ROTException("Socket error #2 while sending.");
}
}
cout << "Connection close.\n";
}
//Setup Sockets
bool runServer(int portNumber)
{
SOCKET hSocket = INVALID_SOCKET, hClientSocket = INVALID_SOCKET;
bool bSuccess = true;
sockaddr_in sockAddr = {0};
try{
//Create Socket
cout << "Creating socket... ";
if((hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
throw ROTException("Could not create socket.");
cout << "created.\n";
//Bind Socket
cout << "Binding socket... ";
setServerSockAddr(&sockAddr, portNumber);
if(bind(hSocket, reinterpret_cast<sockaddr*>(&sockAddr), sizeof(sockAddr)) != 0)
throw ROTException("Could not bind socket.");
cout <<"bound.\n";
cout << getHostDescription((const sockaddr_in)sockAddr);
//Listen
cout << "Putting socket in listening mode... ";
if(listen(hSocket, SOMAXCONN)!= 0)
throw ROTException("Could not put socket in listening mode.");
cout << "ready.\n";
//Connect
cout << "listening... ";
sockaddr_in clientSockAddr;
int clientSockSize = sizeof(clientSockAddr);
hClientSocket = accept(hSocket, reinterpret_cast<sockaddr*>(&clientSockAddr), &clientSockSize);
if(hClientSocket == INVALID_SOCKET)
throw ROTException("Accept function failed.");
cout << "Client found.\n";
//Handle Connection
handleConnection(hClientSocket, clientSockAddr);
}catch(ROTException e){
cerr << "\nError: " << e.what() << endl;
bSuccess = false;
}
}
//Main
int main(int argc, char *argv[])
{
int iRet = 1;
WSADATA wsaData;
cout << "Initializing winsock... ";
if(WSAStartup(MAKEWORD(REQ_WINSOCK_VER, 0), &wsaData) == 0){
if(LOBYTE(wsaData.wVersion) >= REQ_WINSOCK_VER){
cout << "initialized.\n";
int port = DEFAULT_PORT;
if(argc > 1)
port = atoi(argv[1]);
iRet = !runServer(port);
}else
cerr << "required version not supported!";
cout << "Cleaning up winsock... ";
if(WSACleanup() != 0){
cerr << "cleanup failed!\n";
iRet = 1;
}
cout << "done.\n";
}else
cerr << "startup failed!\n";
while(1){}
return iRet;
}