#include <afx.h> 		// header file for MFC
#include <afxinet.h> 	// header file for WinInet
#include <iostream.h>	// header file for input/output stream
#include <stdlib.h>	// header file for standard library functions


int main(int argc, char* argv[])
{
	CInternetSession* m_InternetSession = NULL;
	
	CString serverName("saturn");
	CString sUrlAdress;

	if (argc < 2)
	{
		cout << "Programm format: ConsoleFtp <URL>" << endl;
		cout << "  <URL> - URL address of FTP" << endl << endl;
		return -1;
	}

	sUrlAdress = argv[1];
	
	cout << "URL address: " << sUrlAdress << endl << endl;

	
	CString sServer;
	CString sObject;
	INTERNET_PORT nPort;
	DWORD dwServiceType;
	

	if (!AfxParseURL(sUrlAdress, dwServiceType, sServer, sObject, nPort))
	{
		cout << "AfxParseURL Error" << endl;
		
		return -1;
	}

	if(dwServiceType != AFX_INET_SERVICE_FTP)
	{
			cout << "URL Address not FTP server" << endl;
			
			return -1;
	}

	m_InternetSession = new CInternetSession("Connecter");	
		
	try
	{
		CFtpConnection*  m_FtpConnection = NULL;
			
		m_FtpConnection = m_InternetSession -> GetFtpConnection( sServer );

		cout << "Connect to FTP server established" << endl;

		m_FtpConnection -> Close();

		delete( m_FtpConnection );
	}

	catch (CInternetException* pEx)
	{
		// Обрабатываем исключение CInternetException
		TCHAR szErr[1024];  // временный буфер для сообщения

		// Выводим сообщение об ошибке
		if (pEx->GetErrorMessage(szErr, 1024))
			cout << "Error: " << szErr <<endl <<endl;

		// Удаляем иссключение
		pEx->Delete();
	}

	m_InternetSession -> Close();
	delete(m_InternetSession);

	return 0;
} 
