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.

Can you put some log statements so that we can see the flow of control when you try to fetch a page from Daniweb ...
Paste the log output on the forum

thanks for the reply!! meanwhile i made some changes to my code and now it works for most of the websites ....
There was something wrong with my http request (changes are on line 75 and 76)

BUT still doesn't pull the code from daniweb.com or bing.com :(

Please take a look at the attached screenshots...


my updated code : (you can also find the code.cpp file in the attachment)

#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;
    }

	char aa[100];
	strcpy(aa, "GET / \r\n");
	strcat(aa, "HTTP/1.1 \r\n\r\n");

    // Send request
    send(recvSocket, aa,sizeof(aa), 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");
}

> There was something wrong with my http request (changes are on line 75 and 76)

Yes. You shouldn't split those two lines. The correct request is
GET / HTTP/1.1\r\n
\r\n
At the 1.1 level this is not enough. You must send additional headers, such as Host (I don't recommend using 1.1 until you master 1.0 level).

When receiving a response, do not expect to get everything in a single recv call.
Finally, do not send all 100 bytes of aa . There's a lot of garbage there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.