Archived

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

zackriggle

ahh! f**king winsock!

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 this post


Link to post
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 this post


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

You want to use SOCKADDR_IN:

struct ipAddress : SOCKADDR_IN
{
ipAddress(u16 Port, u32 IPAddress=INADDR_ANY)
{
sin_family = AF_INET;
sin_port = htons(Port);
sin_addr.S_un.S_addr = htonl(IPAddress);
ZeroMemory(sin_zero, 8);
}
operator SOCKADDR*()
{return reinterpret_cast(this);}
operator const SOCKADDR*() const
{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 this post


Link to post
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 this post


Link to post
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);


Look like I made the same mistake your are - try using gethostbyname instead

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

Share this post


Link to post
Share on other sites
National Enquirer Headline:
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:
some_long = inet_addr(my_charp_addr)
**then**
some_lphostent = gethostbyaddr((char*)&some_long,....)

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)???

Share this post


Link to post
Share on other sites