hey there..i'm a beginner in C++ and i'm written the following code using vc++ compiler..actually i took a little help from a book
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<winsock.h>
#define NETWORK_ERROR -1
#define NETWORK_OK 0
using namespace std;
int main(void)
{
WSADATA ws;
int nret;
WSAStartup(MAKEWORD(1,1),&ws);
SOCKET commsocket;
commsocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(commsocket==SOCKET_ERROR)
{
cout<<"Could not create communication socket"<<endl;
WSACleanup();
return NETWORK_ERROR;
}
//fill a SOCKADDR_IN structure with the information of the server
SOCKADDR_IN serverInfo;
serverInfo.sin_family=AF_INET;
serverInfo.sin_addr.s_addr = inet_addr("209.191.93.53");
serverInfo.sin_port=htons(80);
nret=connect(commsocket,(struct sockaddr *) &serverInfo,sizeof(serverInfo));
if(nret==SOCKET_ERROR)
{
cout<<"Could not connect to the server"<<endl;
WSACleanup();
return NETWORK_ERROR;
}
char sendbuffer[256]="GET / \r\n";
char recvbuffer[256];
char str[10000]="";
nret=send(commsocket,sendbuffer,sizeof(sendbuffer),0);
if(nret==SOCKET_ERROR)
{
cout<<"Could not send GET request"<<endl;
WSACleanup();
return NETWORK_ERROR;
}
do
{
strset(recvbuffer,' ');
nret=recv(commsocket,recvbuffer,sizeof(recvbuffer),0);
strcat(str,recvbuffer);
}while(nret!=0);
cout<<str<<endl;
closesocket(commsocket);
WSACleanup();
return NETWORK_OK;
}
the above code runs alright but when i do the following..
SOCKADDR_IN serverInfo;
HOSTENT *h;
serverInfo.sin_family=AF_INET;
h=gethostbyname("www.yahoo.com");
serverInfo.sin_addr.s_addr= *((unsigned long *)h->h_addr);
serverInfo.sin_port=htons(80);
..this generates an error..could anyone please help me understand where i am going wrong??