Last active
December 14, 2015 06:29
-
-
Save andriesss/5042946 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "IpRelay.h" | |
#include <stdexcept> | |
IpRelay::IpRelay(char *ipAddress, u_short port) | |
{ | |
this->setIpAddress(ipAddress); | |
this->port = port; | |
this->sock = INVALID_SOCKET; | |
} | |
bool IpRelay::connect() | |
{ | |
sock = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP); | |
if (sock == INVALID_SOCKET) { | |
return false; | |
} | |
struct sockaddr_in clientService; | |
clientService.sin_family = AF_INET; | |
clientService.sin_addr.s_addr = this->ipAddress; | |
clientService.sin_port = this->port; | |
int iResult; | |
iResult = ::connect(sock, (SOCKADDR*) &clientService, sizeof(clientService)); | |
if (iResult == SOCKET_ERROR) { | |
closesocket(this->sock); | |
WSACleanup(); | |
throw std::runtime_error("socket connection failed"); | |
} | |
return true; | |
} | |
bool IpRelay::disconnect() | |
{ | |
int iResult = closesocket(sock); | |
if (iResult == SOCKET_ERROR) { | |
WSACleanup(); | |
return false; | |
} | |
return true; | |
} | |
void IpRelay::open(RelayNumber relay) | |
{ | |
} | |
void IpRelay::close(RelayNumber relay) | |
{ | |
} | |
void IpRelay::setIpAddress(char *ipAddress) | |
{ | |
this->ipAddress = inet_addr(ipAddress); | |
if (this->ipAddress == INADDR_NONE) { | |
WSACleanup(); | |
throw std::invalid_argument("inet_addr failed and returned INADDR_NONE"); | |
} | |
if (this->ipAddress == INADDR_ANY) { | |
WSACleanup(); | |
throw std::invalid_argument("inet_addr failed and returned INADDR_ANY"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment