Hey everyone I''m having problems with this code:
sockaddr_in target;
target.sin_family = AF_INET; // address family Internet
target.sin_port = htons (port); // set server''s port number
u_long addr = inet_addr(host); // try and parse it if it is an IP address
if (addr == INADDR_NONE) {
// Host isn''t an IP address, try using DNS
hostent* HE = gethostbyname(host);
if (HE == 0) {
// error: Unable to parse!
WSACleanup ();
return;
}
addr = *((u_long*)HE->h_addr_list[0]);
}
target.sin_addr.s_addr = inet_addr ((const char *)addr); // set server''s IP
if (connect(s, target, sizeof(target)) == SOCKET_ERROR)
{ // an error connecting has occurred!
printf("Error Connecting...");
WSACleanup ();
return;
}
when I call connect in the if loop it says it takes a sockaddr not a sockaddr_in...
Any suggestions? This is modified code from the tutorials.
-TipTup
TipTup.Com
🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
Having Problems With Winsock2's connect function
SOCKADDR_IN addr; addr.sin_family = AF_INET; addr.sin_port = htons(iPort); if ((addr.sin_addr.s_addr = inet_addr(pszHost)) == -1) { struct hostent *hs; if ((hs = gethostbyname(pszHost)) == NULL) { throw "Can not resolve specified host (gethostbyname)"; }// addr.sin_family = hs->h_addrtype; memcpy((void *)&addr.sin_addr.s_addr, hs->h_addr, hs->h_length); } if (connect(sock, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR) { throw "Can not connect to specified host (connect)"; }
![](http://www.baskuenen.myweb.nl/images/title.jpg)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement