You are here:

ActiveXperts.com > ActiveSocket > How to Use ActiveSocket > Telnet > Visual C++ 5.x/6.x

ActiveSocket Toolkit Add network capabilities to any Windows or .NET application

Quicklinks


Visual C++ 5.x/6.x Telnet Sample Source Code

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: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, 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.

Step 1: Download and install the ActiveSocket Toolkit

Download the ActiveSocket Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual C++ project

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':

Telnet Visual C

(Click on the picture to enlarge)

Select the kind of project, for instance a 'Hello, world!' application and click 'Finish':

Telnet Visual C

(Click on the picture to enlarge)

Step 3: Refer to the ActiveSocket Library and declare the objects

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;

Step 4: Create the objects

Since the ActiveSocket Toolkit is a COM object, you must initialize the COM library before they can call COM library functions (e.g. ActiveSocket functions):

CoInitialize(NULL);

Create the object in the following way:

CoCreateInstance(CLSID_Tcp, NULL, CLSCTX_INPROC_SERVER, IID_ITcp, (void**) &pSocket );

Step 5: Setting up a TCP/IP connection with a server

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 );

	ReadFromPort( pSocket, 3000 );	// Receive data for 3 seconds

	printf( "Send string: '%s'\n", ( char * ) bstrPassword );
	pSocket->SendString( bstrPassword );

	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/network-component.