Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to connect using sockets

Status
Not open for further replies.

ForeverCode

Programmer
Dec 6, 2002
41
US
Alright, I'm having problems connecting to servers with my sockets. I'm using regular sockets and I can only connect to IPs. So, how would I resolve the IP for say " I really dont feel like writting a DNS client cause I know it Isn't necissary. here's what I'm doing:

SOCKET Connecter;

sockaddr_in Addrs;
Connecter = socket(AF_INET,SOCK_STREAM,0);
Addrs.sin_family = AF_INET;
Addrs.sin_port = htons(80);
Addrs.sin_addr.s_addr = inet_addr("forevercode.cjb.net");

connect(Connecter,(LPSOCKADDR)&Addrs, sizeof(Addrs));

Sleep(100);
send(Connecter, "GET / HTTP/1.0\r\nHost: 55, 0);

I know inet_addr(); is only for dotted IPs, but I dont know what else to do.

-Micah
 
Read up on the [tt]gethostbyname[/tt] function - that should help.
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
yeah, I just got it:) I dont know why I didnt think of gethostbyname(); duh.

If anyone is wondering how its done:

struct hostent *host;
host = gethostbyname("
struct sockaddr_in Addrs;
Connect = socket(AF_INET,SOCK_STREAM,0);
Addrs.sin_family = AF_INET;
Addrs.sin_port = htons(80);
Addrs.sin_addr = *(struct in_addr*)host->h_addr;

connect(Connect,(LPSOCKADDR)&Addrs, sizeof(Addrs));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top