开发者

Why doesn't this C++ code work?

开发者 https://www.devze.com 2023-01-04 06:14 出处:网络
int Socket::Connect(const std::string& host, int port) { if(this->_connected) throw \"Socket is already connected\";
int Socket::Connect(const std::string& host, int port)
{

    if(this->_connected)
        throw "Socket is already connected";
    // Get the IP from the string


    hostent* ip = gethostbyname(host.c_str());

    /*if(server == NULL)
        throw strerror(WSAGetLastError());*/

    // Information for WinSock.
    sockaddr_in addr;
    // Clear up the memory
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr = *((in_addr *)ip->h_addr); 

    // Try and connect
   if(WSAConnect(this->_socket, (sockaddr *)&addr, sizeof(addr), NULL, NULL, NULL, NULL) != 0)
        throw strerror(WSAGetLastError()); // this is being thrown but not caught?
   this->_connected = true;
    return 0;
}

The error is

"Unknown Error"

and here is the main function

int _tmain(int argc, _TCHAR* argv[])
{
    try{


    Socket* socket = new Socket();
    if(socket->Connect("google.com", 80) == 0)
        std::cout << "[-] connected..." << endl;

    std::string line = socket->RecvLine();
    std::cout << line << endl;
    }
    catch(char* errcstr)
    {
        std::cout << errcstr << endl;
    }
    catch(int err)
    {
        std::cout << err << endl;
    }
    catch(std::string errstr)
    {
        std::cout << errstr << endl;
    }
    catch(exception ex)
    {
        std::cout << ex.what() << endl;
    }
    system("pause");
    retu开发者_高级运维rn 0;
}

So it should catch any exceptions as far as I know. How can I fix this? (There shouldn't an exception at all since it's connected to google.com and winsock is initialized etc)

UPDATE: The error is actually being thrown after WSAConnect but there shouldn't be a problem connecting and none of my catch statements are being used for some reason.

UPDATE 2: Well now it catches the error but it says "Unknown Error" which is useless to me. Why won't it connect to google?

SOLVED: thanks!


strerror() returns a char* on windows so you need a catch(char* error)


strerror() is not appropriate here. It looks like you're trying to move Unix code to Windows; strerror() is the right thing on Unix. connect() on Unix stores error codes in the global errno value, and strerror() translates errno codes to error strings. Winsock handles error codes entirely differently, even down to the actual error values, so that they're not compatible with strerror().

See item 2.8 in the Winsock Programmer's FAQ for the correct way to turn Winsock error numbers into error message strings.


Sorry, I meant to post this as an answer not a comment.

You're throwing a char* but there is no catch clause to catch it. Perhaps this is what you wanted to do:

if(WSAConnect(this->_socket, (sockaddr *)&addr, sizeof(addr), NULL, NULL, NULL, NULL) != 0)
        throw std::runtime_error(strerror(WSAGetLastError()));

UPDATE:

Is there any particular reason why you're using WSAConnect() instead of connect()? This should work:

_socket = socket(AF_INET, SOCK_STREAM, NULL);
if ( connect( _socket, &addr, sizeof addr ) == SOCKET_ERROR ) {
    //Error
}

You might also find this useful: http://www.madwizard.org/programming/tutorials/netcpp

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号