Ok. So I have created my class and all this other jazz, but when I try/catch my class's constructor, it seems my main function is reading some values from variables inside the class and getting unexpected results, at least to me .
Here is my class+constructor
Code:
class server_socket
{
private:
int fsock;
short int fport;
struct sockaddr_in faddr;
bool ssocket(void);
bool sbind(void);
bool slisten(void);
public:
int ssocket_error;
int sbind_error;
int slisten_error;
server_socket(short int port);
~server_socket();
};
server_socket::server_socket(short int port)
{
/* Used for debugging */
std::cout << "Constructor Called" << std::endl;
/* Initialize variables */
this->fport = port;
this->fsock = 0;
memset(&(this->faddr), 0, sizeof(struct sockaddr_in));
this->ssocket_error = 1;
this->sbind_error = 2;
this->slisten_error = 3;
/* Prepare socket */
if (!ssocket())
throw ssocket_error;
if (!sbind())
throw sbind_error;
if (!slisten())
throw slisten_error;
}
Here is my main
Code:
int main (void)
{
server_socket ........ck;
try
{
ssck = new server_socket (80);
std::cout << "Socket Creation Successful!" << std::endl;
delete ssck;
}
catch (int err)
{
if (err == ssck->ssocket_error)
std::cout << "Error in socket()" << std::endl;
else if (err == ssck->sbind_error)
std::cout << "Error in bind()" << std::endl;
else if (err == ssck->slisten_error)
std::cout << "Error in listen()" << std::endl;
else
std::cout << "Unknown Error: (" << err << ")" << std::endl;
/* Taking a look at error variable values */
std::cout << "socket: " << ssck->ssocket_error << std::endl;
std::cout << " bind: " << ssck->sbind_error << std::endl;
std::cout << "listen: " << ssck->slisten_error << std::endl;
return -1;
}
return 0;
}
The three functions my constructor calls in no way modifies my three error variables.
So when I fire this bad boy up, I have tested all three cases (where each of the three functions fail), and get results I am not looking for. It is telling me "Unknown Error (2)" or "Unknown Error (3)".
From this point, I check the values of the member variables and they are the following:
ssocket_error = 1
sbind_error = 1
slisten_error = 0
So obviously they aren't matching up. I feel I'm dancing around the lines of undefinedness due to this behavior. Anyone see where I am making a problem?