Click here to Skip to main content
15,893,722 members
Articles / Desktop Programming / MFC

How to write a simple but effective TCP/IP port scanner for Win32

Rate me:
Please Sign up or sign in to vote.
4.82/5 (15 votes)
27 Oct 20017 min read 163.1K   7.3K   101  
An article on how to write a TCP/IP port scanner with a GUI, based on the MFC's property sheet paradigm
/*
	CTextFile.cpp
	Classe derivata per interfaccia file testo (SDK).
	Luca Piergentili, 06/07/98
	lpiergentili@yahoo.com
	http://www.geocities.com/lpiergentili/
*/
#include "env.h"
#include "pragma.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "window.h"
#include "CBinFile.h"
#include "CTextFile.h"

/*
	ReadLine()

	Legge una linea dal file di input.
	Elimina la coppia CRLF dal buffer di lettura e restituisce la lunghezza della linea, restituisce
	FILE_EOF per fine file.
*/
DWORD CTextFile::ReadLine(LPSTR lpBuffer,DWORD dwToRead)
{
	LONG nCrlf = 0L;
	char* crlf;
	DWORD ofs = CBinFile::Seek(0L,FILE_CURRENT);

	if((dwToRead = CBinFile::Read(lpBuffer,dwToRead))!=FILE_EOF)
	{
		if(dwToRead > 0)
		{
			if((crlf = (char*)memchr(lpBuffer,'\r',dwToRead))!=(char*)NULL)
			{
				if(*(crlf + 1)=='\n')
				{
					nCrlf = 2L;
					dwToRead = (crlf-lpBuffer);
					*crlf = '\0';
				}
				else
					lpBuffer[dwToRead] = '\0';
			}
			else
			{
#if 0
				lpBuffer[dwToRead] = '\0';
#else
				if((crlf = (char*)memchr(lpBuffer,'\n',dwToRead))!=(char*)NULL)
				{
					nCrlf = 1L;
					dwToRead = (crlf-lpBuffer);
					*crlf = '\0';
				}
				else
					lpBuffer[dwToRead] = '\0';
#endif
			}
			
			CBinFile::Seek((LONG)ofs + (LONG)dwToRead + nCrlf,FILE_BEGIN);
		}
		else
			dwToRead = FILE_EOF;
	}

	return(dwToRead);
}

/*
	WriteLine()

	Scrive una linea nel file di input.
*/
DWORD CTextFile::WriteLine(LPCSTR lpcBuffer,DWORD dwToWrite)
{
	char* crlf;

	if(dwToWrite==(DWORD)-1)
		dwToWrite = strlen(lpcBuffer);

	if((crlf = (char*)memchr(lpcBuffer,'\r',dwToWrite))!=(char*)NULL)
		if(*(crlf + 1)=='\n')
			dwToWrite = (crlf-lpcBuffer);

	if((dwToWrite = CBinFile::Write(lpcBuffer,dwToWrite))!=FILE_EOF)
		if(CBinFile::Write("\r\n",2)!=FILE_EOF)
			dwToWrite += 2;

	return(dwToWrite);
}

DWORD CTextFile::WriteFormattedLine(LPSTR fmt,...)
{
	LPSTR pArgs;
	DWORD dwToWrite;
	char buffer[8192];
	char buf[4096];

	// ricava gli argomenti
	pArgs = (LPSTR)&fmt + sizeof(fmt);
	memset(buf,'\0',sizeof(buf));
	_vsnprintf(buf,sizeof(buf)-1,fmt,pArgs);
	dwToWrite = _snprintf(buffer,sizeof(buffer)-1,"%s\r\n",buf);
	return(CBinFile::Write(buffer,dwToWrite));
}

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


Written By
Web Developer
Italy Italy
I like C and C++, Acid Jazz, James Brown, gli Spaghetti Aglio e Olio, alla Bolognesa, alla Puttanesca e le Fettuccine alla Matriciana ('Maccaroni' over the world). Of course I like beautiful big tits girls too, my little car, Frank Zappa, the art of Zen, italian coffee and much more...

Comments and Discussions