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
#if !defined(ServerSocket_H)
#define ServerSocket_H

#include "Socket.h"
//#include "ConnectSocket.h"



////////////////////////////////////////////////////////////////////////////////
// ServerSocket
//
// Purpose: class for reading/writing a socket


class ServerSocket :
	public Socket
{
protected:

	long			_noQued;

public:


	ServerSocket ( long noQued ) :
		Socket(),
		_noQued(noQued)
	{
		
	}

	ServerSocket ( ServerSocket & sock ) :
		Socket( (Socket &) sock ),
		_noQued(0)
	{
		_noQued = sock._noQued;
	}




	virtual ~ServerSocket ()
	{
		close();
	}

	// open methods
	bool open ( long port )
	{
		// show using local host
		string host = "";//"localhost";

		// get socket
		_socket = socket(PF_INET,SOCK_STREAM,0);

		if ( _socket == INVALID_SOCKET )
			return false;

		// init address in
		initSockAddrIn( host, port, _addrIn );

		// bind socket
		if ( bind( _socket, (const SOCKADDR *) &_addrIn, sizeof(_addrIn) ) == SOCKET_ERROR )
		{
			int error = getLastError();
			return false;
		}

		return true;
	}


	bool listen ()
	{
		// listen for connection
		if ( ::listen( _socket, _noQued ) != 0 )
		{
			long error = getLastError();

			return false;
		}
		else
			return true;
	}

	bool accept ( Socket & socket )
	{

		// get new socket
		SOCKADDR addr;
		int		 size = sizeof(addr);
		memset( &addr, 0, sizeof(addr) );

		SOCKET sock =
		::accept( _socket, &addr, &size );

		if ( sock == INVALID_SOCKET )
		{
			int err = getLastError();

			return false;
		}
		else
		{
			socket.create( sock, addr );
			return true;
		}
	}

};



#endif

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
Web02 | 2.8.140916.1 | Last Updated 9 May 2000
Article Copyright 2000 by David Hubbard
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid