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;
}
错误是
“未知错误”
这是主要功能
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");
return 0;
}
因此,据我所知,它应该捕获任何异常。我怎样才能解决这个问题?(根本不应该有例外,因为它已连接到 google.com 并且 winsock 已初始化等)
更新:错误实际上是在 WSAConnect 之后引发的,但连接不应该有问题,并且由于某种原因没有使用我的 catch 语句。
更新2:现在它捕获了错误,但它说“未知错误”,这对我来说没用。为什么连接不上谷歌?
已解决:谢谢!