Back to the WFC main page

CListeningSocket : CSimpleSocket

$Revision: 28 $

Description

This class creates a socket that others can connect to.

Methods

unsigned long GetBindToAddress( void ) const
Returns the address of the local end of the socket. By default, this is INADDR_ANY meaning any network transport on the local machine will be the end point for the socket.
int GetConnectionBacklog( void ) const
Returns the number of simultaneous connections that TCP/IP will pause before sending ECONNREFUSED errors to the would-be connectors. This is set to SOMAXCONN by default.
void OnNewConnection( SOCKET socket_id, LPCTSTR host_name, LPCTSTR dotted_ip_address ) = 0
This method is called when a connection has been made to your socket. It is a pure virtual function because CListeningSocket has no idea what you want to do when someone connects to you. This is where you will put your business logic.
BOOL Open( void )
BOOL Open( LPCTSTR machine_name_or_address, UINT port_number = 23, CFileException * = NULL )
Creates the listening socket. It does not wait for someone to connect to this fledgling socket.
void SetBindToAddress( unsigned long address )
Let's you specify which network transport will be used to establish the local end of the socket. If you have two (or more) LAN cards in your computer, each with their own IP address, you can specify which one of those cards will be used for the socket by putting its address in the address parameter before calling Open.
void SetConnectionBacklog( int backlog )
Let's you specify the number of incoming connections that will be queued up. If two clients attempt to connect at the exact same time, one will be immediately accepted while the other will be queued (waiting for OnNewConnection to return).
BOOL WaitForConnection( LPCTSTR port_name )
BOOL WaitForConnection( const int port_number )
BOOL WaitForConnection( void )
Begins the waiting process. Program execution will halt here until someone connects to your socket. OnNewConnection() will be called by this method before it returns. It will return TRUE if the connection was made or FALSE if there was a problem.

Example

#include <wfc.h>

class CTimeSocket : CListeningSocket
{
   public:

      virtual void OnNewConnection( SOCKET socket_id, const char *host_name, const char *dotted_ip_address_string );
};

void CTimeSocket::OnNewConnection( SOCKET socket_id, const char *host_name, const char *dotted_ip_address )
{
   WFCTRACEINIT( TEXT( "CTimeSocket::OnNewConnection()" ) );

   // This class sends the new client the time then disconnects

   CSimpleSocketFile socket( socket_id, host_name, dotted_ip_address );

   DWORD number_of_seconds_since_1970 = 0;

   CTime time_now = CTime::GetCurrentTime();

   number_of_seconds_since_1970 time_now.GetTime();

   socket.Write( (const VOID *) &number_of_seconds_since_1970, (long) 4 );
   // the socket is closed in the CSimpleSocketFile class destructor
}

int _tmain( int number_of_command_line_arguments, LPCTSTR command_line_arguments[] )
{
   WFCTRACEINIT( TEXT( "_tmain()" ) );

   CTimeSocket time_service;

   // Wait on port 37 (the "time" port)

   while( time_service.WaitForConnection( 37 ) != FALSE )
   {
      _tprintf( TEXT( "We had a connection\n" ) );
   }

   return( EXIT_SUCCESS );
}

API's Used

CListeningSocket uses the following API's:
Copyright, 2000, Samuel R. Blackburn
$Workfile: lsockets.cpp $
$Modtime: 2/14/00 5:03a $