Ok, so my task is to Write a simple TCP client that fetches pages
from web servers.The client should be invoked with the command
(for example) >>
webclient www.yahoo.com/ >>
webclient www.msstate.edu/academics/
• The client should get the response from the HTTP server and print it on the screen.
Here is my code:
#include <winsock.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wsock32.lib")
#define NO_FLAGS_SET 0
#define PORT 80
#define MAXBUFLEN 20480 // How much is printed out to the screen
using namespace std;
int main(void)
{
WSADATA Data;
SOCKADDR_IN recvSockAddr;
SOCKET recvSocket;
int status;
int numrcv = 0;
hostent* remoteHost;
char* ip;
char* host_name;
char buffer[MAXBUFLEN];
memset(buffer,0,MAXBUFLEN);
// Initialize Windows Socket DLL
status = WSAStartup(MAKEWORD(2, 2), &Data);
if(status != 0)
{
cerr << "ERROR: WSAStartup unsuccessful" << endl;
return 0;
}
// User inputs web address
cout << "Input name of host: ";
host_name = (char*) malloc(sizeof(char*)*128);
cin >> host_name;
// Get IP address from host name
host_name[strlen(host_name)] = '\0'; // NULL terminated
remoteHost = gethostbyname(host_name);
cout<<remoteHost;
ip = inet_ntoa(*(struct in_addr *)*remoteHost->h_addr_list);
printf("IP address is: %s.\n", ip);
memset(&recvSockAddr, 0, sizeof(recvSockAddr)); // zero the sockaddr_in structure
recvSockAddr.sin_port=htons(PORT); // specify the port portion of the address
recvSockAddr.sin_family=AF_INET; // specify the address family as Internet
recvSockAddr.sin_addr.s_addr= inet_addr(ip); // specify ip address
// Create socket
recvSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(recvSocket == INVALID_SOCKET)
{
cerr << "ERROR: socket unsuccessful" << endl;
system("pause");
return 0;
}
// Connect
if(connect(recvSocket,(SOCKADDR*)&recvSockAddr,sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
{
cout << "ERROR: socket could not connect" << endl;
closesocket(recvSocket);
WSACleanup();
return 0;
}
// Send request
send(recvSocket, "GET \r\n\r\n", 12, 0);
// Receieve
numrcv = recv(recvSocket, buffer, MAXBUFLEN, NO_FLAGS_SET);
if (numrcv == SOCKET_ERROR)
{
cerr << "ERROR: recvfrom unsuccessful" << endl;
status = closesocket(recvSocket);
if(status == SOCKET_ERROR)
cerr << "ERROR: closesocket unsuccessful" << endl;
status = WSACleanup();
if (status == SOCKET_ERROR)
cerr << "ERROR: WSACleanup unsuccessful" << endl;
system("pause");
return(1);
}
// Print out receieved socket data
cout << buffer << endl;
system("pause");
}
ON cmd -- www.google.com
so now the thing is my code works with simple website like google.com, gmail.com , msstate.edu but is not able to pull html code from sites like daniweb.com , yahoo.com etc.( it connects fine but just doesnt gets the html code)
I am guessing it has something to do with how i am processing the URL coz it is weird, that it works with some and doesn't work with others!!!
PLEASE HELPPPPP!!!!
and this is how i set up the project on visual studio---
1. Create a new win32 console application.
2. Go to project ->project properties
3. Then configuration properties-> linker->input
4. Then add win32.lib to additional dependencies.