The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem with socket reading swap007 UNIX for Advanced & Expert Users 2 05-21-2008 01:08 AM
Problem Connecting to Socket Stevhp High Level Programming 6 04-30-2007 07:27 PM
Problem in HP-Unix while writing into socket AshokG HP-UX 0 02-25-2005 02:19 AM
Socket Problem Agent007 High Level Programming 3 04-03-2004 08:15 PM
[Problem]Reuse port in BSD socket Namely High Level Programming 1 11-28-2003 11:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-29-2008
imdupeng imdupeng is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1
Question HTTP Keep-Alive socket problem

Hello everyone, I am a newbie in UNIX/Linux socket programming. This is a class project that I had trouble with.

==================================================

I was trying to make “Keep-Alive” HTTP connections to the server in a tiny web crawler project. Here is the problem: when I tried to recv() the first page, it succeeded. However, the 2nd consecutive recv() will receive zero bytes, for which I really have no idea. I did put a “Keep-Alive” field in the request message when I called send().

I don't quite know how to implement HTTP persistent connection by using Keep-Alive. I extracted the socket-related code into a small program as attached.

Would you please take a look at the code and explain what was wrong with it to me? That would be of great help since I have stuck on this problem for days.

By the way, the program was compiled under SunOS using g++ and -lsocket.
==================================================

Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <string>
#include <iostream>

using namespace std;

// ------------------------------------------------------
const short SOCKET_ERROR = -1;

const short RECV_BUFFER_SIZE = 20;
const short REQUEST_BUFFER_SIZE = 255;

int sock;
char recv_buf [RECV_BUFFER_SIZE + 1];
char req_buf [REQUEST_BUFFER_SIZE + 1];

const static char REQUEST_TEMPLATE [] = 
{
	"GET %s HTTP/1.1\r\n"
	"Host: xxx.xxx.xxx\r\n" // should be replaced with a really host
	"Connection: Keep-Alive\r\n"
	"\r\n"
};

// ------------------------------------------------------
void create_socket ();
void download (const string& path, string& response);

// ------------------------------------------------------

int main (void)
{
	string first_addr = "/~pdu/index.html"; // should be replaced with a really URL
	string second_addr = "/~pdu/a.html"; // should be replaced with a really URL
	string response;

	create_socket ();
	download (first_addr, response);
	cout << response << endl << endl;
	response = "";
	download (second_addr, response);
	if (response.size() > 0)
	{
		cout << response << endl;
	}
	else
	{
		cout << "### The 2nd recv() failed to receive any bytes from the socket!" << endl << endl;
	}
	
	close (sock);

	return 0;
}

void create_socket ()
{
	struct sockaddr_in addr;

	// ------------------------------------------------------
	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(sock == SOCKET_ERROR)
    {
        perror ("Could not make a socket.\n");
		exit (-1);
    }

	cout << ">>> Socket created!" << endl;

	// ------------------------------------------------------
	struct hostent* host_info = gethostbyname("cse.unl.edu");	
	
	cout << ">>> DNS done!" << endl;

	long host_addr;

	/* copy address into long */
	memcpy(&host_addr, host_info->h_addr,
		host_info->h_length);

	/* fill address struct */
	addr.sin_addr.s_addr = host_addr;
	addr.sin_port = htons(80);
	addr.sin_family = AF_INET;

	// ------------------------------------------------------
	if( connect(sock, (struct sockaddr*)(&addr),
		sizeof(addr)) == SOCKET_ERROR )
    {
        perror("Could not connect to HTTP server.\n");
		exit (-1);
    }

	cout << ">>> Connection established!" << endl;
}

void download (const string& path, string& response) 
{
	size_t nBytes = snprintf(
		req_buf, 
		REQUEST_BUFFER_SIZE, 
		REQUEST_TEMPLATE, 
		path.c_str());

	if (nBytes >= REQUEST_BUFFER_SIZE)
	{
        cerr << "Buffer is too small for making a request message" << endl;
		exit (-1);
	}

	if (send(sock, req_buf, nBytes, 0) != nBytes)
	{
        perror("Could not send request to the HTTP server.\n");
		exit (-1);
	}
	cout << ">>> Request sent! -> " << path << endl << req_buf << endl;
	
	ssize_t size = 0;
	
	while ((size = recv(sock, recv_buf, 
		RECV_BUFFER_SIZE, 0)) > 0) 
	{
		recv_buf[size] = '\0';
		response.append(recv_buf);
    }

	cout << ">>> Response received!" << endl;
}
Thank you!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0