Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

STL WebServer

, 8 May 2000
A set of classes written in STL that implement a web server
// SocketTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "SocketHandler.h"
#include "HttpRequest.h"
#include "HttpResponse.h"



//////////////////////////////////////////////////////////////////////////////
// HttpResonseNotify
//
// Purpose:	process reponses for http requests

class HttpResonseNotify :
	public SocketNotify 
{
	Socket & _socket;
	string   _buffer;

public:

	HttpResonseNotify (Socket & sock) :
		SocketNotify (),
		_socket( sock )
	{}

	// notification method on read
	bool onReceive ( int error )
	{
		// get no of bytes
		long length = _socket.getRecieveSize();
		if ( length == 0 )
		{
			return false;
		}

		// resize buffer if needed
		if ( _buffer.size() < length )
		{
			StringUtil::getBufferString(_buffer,length+1);
		}

		LPTSTR buffer = (LPTSTR) _buffer.data();

		// terminate buffer
		buffer[length] = '\0';

		// recieve 
		length = _socket.recieve( buffer, length );
		if ( length == 0 )
		{
			return false;
		}

		printf("%s",buffer);


		return true;
	}




};


int main(int argc, char* argv[])
{
	
	// init for socket startup		
	WSADATA WSAData;
	WSAStartup( MAKEWORD(2,0), &WSAData );

	// create socket
	Socket sock;
	//if ( !sock.open( string("localhost"), 80 ) )
	if ( !sock.open( string("www.wrox.com"), 80 ) )
		return 0;

	HttpRequest request;
	request.create(idHttpGet);

	string str;
	request.getString(str);

	sock.send( str );

	// use notify handler
	HttpResonseNotify	notify(sock);
	SocketHandler		handler(sock, notify);

	handler.create();

	// handler handles reads so now
	// do nothing
	while ( true )
	{
		Sleep(1000);
	}
	

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

David Hubbard

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 9 May 2000
Article Copyright 2000 by David Hubbard
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid