#### Archived

This topic is now archived and is closed to further replies.

# ahh! f**king winsock!

This topic is 5678 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

alright, winsock is quite literally kicking my ass... I can''t figure it out... and when I can''t figure something out, I get pissed off... like now... can ANYONE ! PLEASE ! show me where I can find a WinSock tutorial that shows you how to use winsock directly through tcp/ip, connecting with an IP address, NOT a "www.thisisboring.com" address... ................... or... could anyone give me a working set of functions to make my life easier... if you want me to post the code i''ve already got, I will, but i need to go cool off for awhile... thx

##### Share on other sites
In winsock, an address is converted to an unsigned long before being used. The function which converts "215.76.231.19" to an unsigned long is inet_addr. You seem to have found tutorials which use gethostbyname to convert "something.com" into an unsigned long - just replace the appropriate code with inet_addr, and you should be fine.

##### Share on other sites
bsd sockets are perhaps the best example of how *not* to design an API.

{
{
sin_family = AF_INET;
sin_port = htons(Port);
ZeroMemory(sin_zero, 8);
}
{return reinterpret_cast(this);}
{return reinterpret_cast(this);}
}

However, as you may be aware, the connect function takes a sockaddr* not a SOCKADDR_IN*, so you need to cast it, or if you use a class like the one above it will perform the conversion for you.

##### Share on other sites
just for clarifacation -- this should work, correct?

        SOCKADDR_IN saServer;LPHOSTENT lpHostEntry;lpHostEntry = new LPHOSTENT;lpHostEntry = gethostbyaddr(addr_to_connect_to,strlen(addr_to_connect_to), AF_INET);				// Specifying server by its IP        if (lpHostEntry == NULL) 		{			MessageBox(NULL,"Error at gethostbyaddr()", "Mq Online Client Error", MB_OK);			ShutDown();			return false;		}		//		// Use SOCKADDR_IN to fill in address information		//		saServer.sin_family = AF_INET;		saServer.sin_addr.s_addr = inet_addr(addr_to_connect_to);		saServer.sin_port = htons(port);		nRet = connect(sckt,						(LPSOCKADDR)&saServer,		// Server address						sizeof(struct sockaddr));	// Length of address structure	    if (nRet == SOCKET_ERROR)		{		   MessageBox(NULL,"Error at connect()", "Mq Online Client Error", MB_OK);		   WSACleanup();		   return false;		}		return true;

i know that there is something wrong with it -- it compiles just fine, but I get an error at gethostbyaddr()
i think it may have something to do with pointers, as that is about 50% of my problems anyways

[edited by - zackriggle on November 2, 2002 10:13:55 PM]

[edited by - zackriggle on November 2, 2002 10:15:38 PM]

##### Share on other sites
huh, I don''t recall the issue, but look at this curious code I wrote:

//hostent* host = gethostbyaddr(szName, strlen(szName), AF_INET);hostent* host;host = gethostbyname(szName);

Also, call WSAGetLastError to see why something fails (it''s just a macro to GetLastError IIRC)

##### Share on other sites
DUMBASS 14year old figures something out for a change!!

Sound fake? I THINK NOT!!

All I had to do was what you told me to, only I took it the wrong way....

I was passing an actual char* containing a char array that was the host address.
What I *should* have done (and I now have) is:
**then**

Wohoo!
And now overyone is thinking im an idiot... go figure... can''t say i disagree

Now that i''m done jumping up and down (its very hard to type while doing this), can someone tell my how to get the localhost''s IP (programatically, not through ipconfig)???

gethostname.

• 10
• 18
• 14
• 18
• 15