ActiveSocket provides an easy-to-use development interface to a variety of IP protocols. By using ActiveSocket, you can very easily create or enhance applications with network features.
ActiveSocket features the following: ICMP, HTTP and HTTPs with support for proxy servers and secure web sites, Telnet, NTP time protocol, RSH remote shell script interface, SNMP (Simple Network Management Protcol), SNMP Traps, Sockets (TCP and UDP), WOL (Wake-On-LAN), and more.
ActiveSocket can be well integrated into Visual C++ environments. This document describes how the ActiveSocket Toolkit can be integrated into Visual Studio C++ 5.x/6.x projects.
Download the ActiveSocket Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Launch 'Microsoft Visual C++' from the Start menu, and choose 'New' from the 'File Menu'. The 'New' dialog appears. Select the type of project (for instance: 'Win32 Console Application'), enter a 'Project name' and select the 'Location':
(Click on the picture to enlarge)
Select the kind of project, for instance a 'Hello, world!' application and click 'Finish':
(Click on the picture to enlarge)
A new Project is created now.
Before you can use ActiveSocket, you need to refer to the ActiveSocket library. The actually reference files are shipped with the product and are located in the following directory:
C:\Program Files\ActiveXperts\ActiveSocket\Examples\Visual C++\Include
Copy all files in the above directory ('ASocket.h', 'ASocket_i.c' and 'ASocketConstants.h') to your project directory.
On top of your code, declare the following object:
ITcp *pSocket = NULL;
Since the ActiveComport Toolkit is a COM object, you must initialize the COM library before they can call COM library functions (e.g. ActiveComport functions):
CoInitialize(NULL);
Create the object in the following way:
CoCreateInstance(CLSID_Tcp, NULL, CLSCTX_INPROC_SERVER, IID_ITcp, (void**) &pSocket );
You can now setup a TCP/IP connection with a server.
The following code shows how to setup a TCP/IP connection with a Telnet server using Visual C++:
#include <comdef.h>
#include <atlbase.h>
#include <windows.h>
#include <stdio.h>
#include "..\include\ASocketConstants.h"
#include "..\include\ASocket_i.c"
#include "..\include\ASocket.h"
VOID ReadFromPort( ITcp *pSocket, DWORD dwMaxMSecs );
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
ITcp *pSocket = NULL;
LONG lLastError;
HRESULT hr;
_bstr_t bstrHost = "library.uah.edu";
_bstr_t bstrLogin = "guest";
_bstr_t bstrPassword = "guest";
// Initialize COM
CoInitialize(NULL);
hr = CoCreateInstance(CLSID_Tcp, NULL, CLSCTX_INPROC_SERVER, IID_ITcp, (void**) &pSocket );
if( ! SUCCEEDED( hr ) )
{
pSocket = NULL;
printf( "Unable to create instance of the object.\n" );
goto _EndMain;
}
pSocket->put_Protocol( asPROTOCOL_TELNET ); // Telnet protocol
// Connect
pSocket->Connect( bstrHost, 23 );
pSocket->get_LastError( &lLastError );
printf( "Connect to '%s', result: %d\n", ( char * ) bstrHost, lLastError );
if( lLastError ) goto _EndMain;
printf( "Wait for 5 seconds before telnet connection is established...\n" );
Sleep( 5000 ); // It usually takes some seconds before a connection is established
ReadFromPort( pSocket, 7000 ); // Receive data for 3 seconds
printf( "Send string: '%s'\n", ( char * ) bstrLogin );
pSocket->SendString( bstrLogin, TRUE );
ReadFromPort( pSocket, 3000 ); // Receive data for 3 seconds
printf( "Send string: '%s'\n", ( char * ) bstrPassword );
pSocket->SendString( bstrPassword, TRUE );
ReadFromPort( pSocket, 3000 ); // Receive data for 3 seconds
pSocket->Disconnect();
printf( "Session disconnected by client.\n" );
printf( "Ready.\n" );
_EndMain:
if( pSocket != NULL )
{
pSocket->Release();
pSocket = NULL;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
VOID ReadFromPort( ITcp *pSocket, DWORD dwMaxMSecs )
{
DWORD dwStartTime = GetTickCount();
BSTR bstrTemp;
_bstr_t bstrStringReceived;
BOOL bSomethingRead = FALSE;
LONG lConnectionState;
printf( "Attempting to receive data...\n" );
do
{
pSocket->get_ConnectionState( &lConnectionState );
if( lConnectionState != 3 ) // 1=asCONN_DISCONNECTED, 2=asCONN_LISTENING, 3=asCONN_CONNECTED
{
printf( "Unable to receive, no connection established\n" );
return;
}
Sleep( 200 );
pSocket->ReceiveString( &bstrTemp );
bstrStringReceived = bstrTemp;
if( strlen( ( char * ) bstrStringReceived ) > 0 )
{
printf( "%s", ( char * ) bstrStringReceived );
}
SysFreeString( bstrTemp );
} while( GetTickCount() < dwStartTime + dwMaxMSecs );
printf( "\n" );
}
////////////////////////////////////////////////////////////////////////////////
There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/asocket.