Advertisement

Simple beginner questions

Started by August 25, 2005 08:13 PM
2 comments, last by hplus0603 19 years, 5 months ago
When creating the client side socket using WinSock, how do I input an actual IP where "ip" is in the following: htonl(ip). I tried putting in a few different formats but say I wanted to put in the IP, "198.162.0.1" how would I go about converting that into a format that the htonl() function can read? Also, I tried using telnet in the command promt with windows xp and got no connection every time, why can it not connect to anything? -Chris
htons is for your port, not the ip.

(in this case szServer is a null terminated C style string containing the ip address)

sockaddr_in servAddr;
(...)
servAddr.sin_addr.s_addr=inet_addr(szServer);
servAddr.sin_port=htons(iPort);

"Think you Disco Duck, think!" Professor Farnsworth
Advertisement
I see, I read a networking sample somewhere that said to use htons() for port and htonl() for ip, does it make a difference if I use inet_addr() in place of htonl() or will it be pretty much the same thing?

-Chris
inet_addr() takes a string, and returns a properly-formatted address.

htonl() takes a long, and returns a properly formatted long (which can be used as the address).

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement