16 12
发新话题
打印

telnet 程序[转自http://www.codeguru.com/Cpp/I-N/internet/article.php/c6243]

// SocketRx.cpp: implementation of the CSocketRx class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Telnet.h"
#include "ProtocolRx.h"
#include "SocketRx.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSocketRx::CSocketRx()
{

}

CSocketRx::CSocketRx(SOCKET hSocket,HANDLE &hThread)
{
DWORD dwRet;

        m_nExit = 0;
        m_hThread = NULL;
        m_hSocket = hSocket;

        m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) RdTh,(LPVOID)this,0,&dwRet);
        if ( m_hThread == NULL ) return;
        hThread = m_hThread;
}

CSocketRx::~CSocketRx()
{
        m_nExit = 1;
}
DWORD CSocketRx::RdTh(CSocketRx *pSocketRx)
{
char pBuf[256];
char* scan;
int nRet;

  while(1)
  {
        if ( pSocketRx->m_nExit == 1 ) { ExitThread(0); return 0; }
    nRet = recv(pSocketRx->m_hSocket,pBuf,sizeof(pBuf),0);
        if ( nRet == SOCKET_ERROR ) { TRACE( "\nRaed Fail.........!\n" ); pSocketRx->m_nExit = 1; continue;}
        if ( nRet == 0) Sleep(10);
        scan = pBuf;
        while(nRet--)
        {
                pSocketRx->m_Protocol.TelentProtcol(pSocketRx->m_hSocket,*scan++);
                TRACE("%c ",*scan);
        }

  }
  return 0;
}
http://changch84.blog.sohu.com/
dirkchang@hotmail.com

TOP

// SocketTx.cpp: implementation of the CSocketTx class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Telnet.h"
#include "conio.h"
#include "SocketTx.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern HANDLE stdin1;
extern HANDLE stdout1;
extern HANDLE stderr1;

CSocketTx::CSocketTx(SOCKET hSocket,HANDLE &hThread)
{
DWORD dwRet;

        m_nExit = 0;
        m_hThread = NULL;
        m_hSocket = hSocket;

        m_hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) SendTh,(LPVOID)this,0,&dwRet);
        if ( m_hThread == NULL ) return;
        hThread = m_hThread;
}
CSocketTx::~CSocketTx()
{
        m_nExit = 1;
}
DWORD CSocketTx::SendTh(CSocketTx *pSocketTx)
{
char pBuff[256];
unsigned long dwLen;
int nRet;
char ch;

        dwLen = 1;
        while(1)
        {
                if ( pSocketTx->m_nExit == 1 ) { ExitThread(0); return 0; }
                WaitForSingleObject(stdin1,INFINITE);
                ch = getch();
                nRet = send(pSocketTx->m_hSocket,&ch,dwLen,0);
                if ( nRet == SOCKET_ERROR ) { TRACE("\nSend Fail........!\n"); pSocketTx->m_nExit = 0; continue;}
        }
  return 0;
}
http://changch84.blog.sohu.com/
dirkchang@hotmail.com

TOP

// stdafx.cpp : source file that includes just the standard includes
//        Telnet.pch will be the pre-compiled header
//        stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
http://changch84.blog.sohu.com/
dirkchang@hotmail.com

TOP

#include "stdafx.h"
#include <windows.h>
#include <winsock.h>
#include <process.h>
#include "SocketRx.h"
#include "SocketDx.h"
#include "SocketTx.h"
#include <conio.h>
#include "Telnet.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CWinApp theApp;
using namespace std;
HANDLE stdin1;
HANDLE hHeap1;
HANDLE stdout1;
HANDLE stderr1;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRet;
WSADATA wd;
int nPort;
char strIP[256];
char strTitle[256];
SOCKET hSocket;
HANDLE hThread[2];

        nRet = AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
        if ( nRet == FALSE )
        {
                printf("\nAfxWinInit Fail..............!\n");
                return 0;
        }

        nPort = IPPORT_TELNET;
        SetConsoleTitle("Try To connect...");
          WSAStartup(0x0101,&wd);
   
        stdin1 = GetStdHandle(STD_INPUT_HANDLE);
        stdout1 = GetStdHandle(STD_OUTPUT_HANDLE);
        stderr1 = GetStdHandle(STD_ERROR_HANDLE);
        hHeap1 = GetProcessHeap();
        SetConsoleMode(stdin1,ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT);
       
        printf("\nPlease Enter IP/Domain.........:");
        scanf("%s",strIP);
        if ( strlen(strIP) < 3 ) { printf("\nBad IP/Domain\n"); getch(); return 0; }
        printf("\nPort...........%d :",nPort);
       
        CSocketDx        SocketDx(strIP,nPort);
        hSocket = SocketDx.TelnetConnect();
        if ( hSocket == NULL ) { printf("\nUnable To Connect\n");getche(); return 0; }

        CSocketRx        SocketRx(hSocket,hThread[0]);
        CSocketTx        SocketTx(hSocket,hThread[1]);

        wsprintf(strTitle,"Connecting to %s:%d",strIP,nPort);
        SetConsoleTitle(strTitle);

        WaitForMultipleObjects(2,hThread,FALSE,INFINITE);
        return 0;
}
http://changch84.blog.sohu.com/
dirkchang@hotmail.com

TOP

//Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE 9, 1
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE DISCARDABLE
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE DISCARDABLE
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE DISCARDABLE
BEGIN
    "\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// String Table
//

STRINGTABLE DISCARDABLE
BEGIN
    IDS_HELLO               "Hello from MFC!"
END

#endif
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED
http://changch84.blog.sohu.com/
dirkchang@hotmail.com

TOP

<?xml version="1.0" encoding="gb2312"?>
<VisualStudioProject
        ProjectType="Visual C++"
        Version="8.00"
        Name="Telnet"
        ProjectGUID="{242E67F9-AFE5-4452-8CD8-0AE938FC500F}"
        Keyword="MFCProj"
        >
        <Platforms>
                <Platform
                        Name="Win32"
                />
        </Platforms>
        <ToolFiles>
        </ToolFiles>
        <Configurations>
                <Configuration
                        Name="Release|Win32"
                        OutputDirectory=".\Release"
                        IntermediateDirectory=".\Release"
                        ConfigurationType="1"
                        InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC60.vsprops"
                        UseOfMFC="2"
                        ATLMinimizesCRunTimeLibraryUsage="false"
                        CharacterSet="2"
                        >
                        <Tool
                                Name="VCPreBuildEventTool"
                        />
                        <Tool
                                Name="VCCustomBuildTool"
                        />
                        <Tool
                                Name="VCXMLDataGeneratorTool"
                        />
                        <Tool
                                Name="VCWebServiceProxyGeneratorTool"
                        />
                        <Tool
                                Name="VCMIDLTool"
                                TypeLibraryName=".\Release/Telnet.tlb"
                                HeaderFileName=""
                        />
                        <Tool
                                Name="VCCLCompilerTool"
                                Optimization="2"
                                InlineFunctionExpansion="1"
                                PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
                                StringPooling="true"
                                RuntimeLibrary="2"
                                EnableFunctionLevelLinking="true"
                                UsePrecompiledHeader="2"
                                PrecompiledHeaderThrough="stdafx.h"
                                PrecompiledHeaderFile=".\Release/Telnet.pch"
                                AssemblerListingLocation=".\Release/"
                                ObjectFile=".\Release/"
                                ProgramDataBaseFileName=".\Release/"
                                WarningLevel="3"
                                SuppressStartupBanner="true"
                        />
                        <Tool
                                Name="VCManagedResourceCompilerTool"
                        />
                        <Tool
                                Name="VCResourceCompilerTool"
                                PreprocessorDefinitions="NDEBUG"
                                Culture="1033"
                        />
                        <Tool
                                Name="VCPreLinkEventTool"
                        />
                        <Tool
                                Name="VCLinkerTool"
                                AdditionalDependencies="kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib"
                                OutputFile=".\Release/Telnet.exe"
                                LinkIncremental="1"
                                SuppressStartupBanner="true"
                                ProgramDatabaseFile=".\Release/Telnet.pdb"
                                SubSystem="1"
                                TargetMachine="1"
                        />
                        <Tool
                                Name="VCALinkTool"
                        />
                        <Tool
                                Name="VCManifestTool"
                        />
                        <Tool
                                Name="VCXDCMakeTool"
                        />
                        <Tool
                                Name="VCBscMakeTool"
                                SuppressStartupBanner="true"
                                OutputFile=".\Release/Telnet.bsc"
                        />
                        <Tool
                                Name="VCFxCopTool"
                        />
                        <Tool
                                Name="VCAppVerifierTool"
                        />
                        <Tool
                                Name="VCWebDeploymentTool"
                        />
                        <Tool
                                Name="VCPostBuildEventTool"
                        />
                </Configuration>
                <Configuration
                        Name="Debug|Win32"
                        OutputDirectory=".\Debug"
                        IntermediateDirectory=".\Debug"
                        ConfigurationType="1"
                        InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC60.vsprops"
                        UseOfMFC="2"
                        ATLMinimizesCRunTimeLibraryUsage="false"
                        CharacterSet="2"
                        >
                        <Tool
                                Name="VCPreBuildEventTool"
                        />
                        <Tool
                                Name="VCCustomBuildTool"
                        />
                        <Tool
                                Name="VCXMLDataGeneratorTool"
                        />
                        <Tool
                                Name="VCWebServiceProxyGeneratorTool"
                        />
                        <Tool
                                Name="VCMIDLTool"
                                TypeLibraryName=".\Debug/Telnet.tlb"
                                HeaderFileName=""
                        />
                        <Tool
                                Name="VCCLCompilerTool"
                                Optimization="0"
                                PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
                                MinimalRebuild="true"
                                BasicRuntimeChecks="3"
                                RuntimeLibrary="3"
                                UsePrecompiledHeader="2"
                                PrecompiledHeaderThrough="stdafx.h"
                                PrecompiledHeaderFile=".\Debug/Telnet.pch"
                                AssemblerListingLocation=".\Debug/"
                                ObjectFile=".\Debug/"
                                ProgramDataBaseFileName=".\Debug/"
                                BrowseInformation="1"
                                WarningLevel="3"
                                SuppressStartupBanner="true"
                                DebugInformationFormat="4"
                        />
                        <Tool
                                Name="VCManagedResourceCompilerTool"
                        />
                        <Tool
                                Name="VCResourceCompilerTool"
                                PreprocessorDefinitions="_DEBUG"
                                Culture="1033"
                        />
                        <Tool
                                Name="VCPreLinkEventTool"
                        />
                        <Tool
                                Name="VCLinkerTool"
                                AdditionalDependencies="kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib wsock32.lib"
                                OutputFile=".\Debug/Telnet.exe"
                                LinkIncremental="2"
                                SuppressStartupBanner="true"
                                GenerateDebugInformation="true"
                                ProgramDatabaseFile=".\Debug/Telnet.pdb"
                                SubSystem="1"
                                TargetMachine="1"
                        />
                        <Tool
                                Name="VCALinkTool"
                        />
                        <Tool
                                Name="VCManifestTool"
                        />
                        <Tool
                                Name="VCXDCMakeTool"
                        />
                        <Tool
                                Name="VCBscMakeTool"
                                SuppressStartupBanner="true"
                                OutputFile=".\Debug/Telnet.bsc"
                        />
                        <Tool
                                Name="VCFxCopTool"
                        />
                        <Tool
                                Name="VCAppVerifierTool"
                        />
                        <Tool
                                Name="VCWebDeploymentTool"
                        />
                        <Tool
                                Name="VCPostBuildEventTool"
                        />
                </Configuration>
        </Configurations>
        <References>
        </References>
        <Files>
                <Filter
                        Name="Source Files"
                        Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
                        >
                        <File
                                RelativePath="ProtocolRx.cpp"
                                >
                                <FileConfiguration
                                        Name="Release|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                                <FileConfiguration
                                        Name="Debug|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                        </File>
                        <File
                                RelativePath="SocketDx.cpp"
                                >
                                <FileConfiguration
                                        Name="Release|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                                <FileConfiguration
                                        Name="Debug|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                        </File>
                        <File
                                RelativePath="SocketRx.cpp"
                                >
                                <FileConfiguration
                                        Name="Release|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                                <FileConfiguration
                                        Name="Debug|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                        </File>
                        <File
                                RelativePath="SocketTx.cpp"
                                >
                                <FileConfiguration
                                        Name="Release|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                                <FileConfiguration
                                        Name="Debug|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                        </File>
                        <File
                                RelativePath="StdAfx.cpp"
                                >
                                <FileConfiguration
                                        Name="Release|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                                UsePrecompiledHeader="1"
                                        />
                                </FileConfiguration>
                                <FileConfiguration
                                        Name="Debug|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                                UsePrecompiledHeader="1"
                                        />
                                </FileConfiguration>
                        </File>
                        <File
                                RelativePath="Telnet.cpp"
                                >
                                <FileConfiguration
                                        Name="Release|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                                <FileConfiguration
                                        Name="Debug|Win32"
                                        >
                                        <Tool
                                                Name="VCCLCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                        </File>
                        <File
                                RelativePath="Telnet.rc"
                                >
                                <FileConfiguration
                                        Name="Release|Win32"
                                        >
                                        <Tool
                                                Name="VCResourceCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                                <FileConfiguration
                                        Name="Debug|Win32"
                                        >
                                        <Tool
                                                Name="VCResourceCompilerTool"
                                                PreprocessorDefinitions=""
                                        />
                                </FileConfiguration>
                        </File>
                </Filter>
                <Filter
                        Name="Header Files"
                        Filter="h;hpp;hxx;hm;inl"
                        >
                        <File
                                RelativePath="ProtocolRx.h"
                                >
                        </File>
                        <File
                                RelativePath="Resource.h"
                                >
                        </File>
                        <File
                                RelativePath="SocketDx.h"
                                >
                        </File>
                        <File
                                RelativePath="SocketRx.h"
                                >
                        </File>
                        <File
                                RelativePath="SocketTx.h"
                                >
                        </File>
                        <File
                                RelativePath="StdAfx.h"
                                >
                        </File>
                        <File
                                RelativePath="Telnet.h"
                                >
                        </File>
                        <File
                                RelativePath="Type.h"
                                >
                        </File>
                </Filter>
                <Filter
                        Name="Resource Files"
                        Filter="ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
                        >
                </Filter>
                <File
                        RelativePath="ReadMe.txt"
                        >
                </File>
        </Files>
        <Globals>
        </Globals>
</VisualStudioProject>
http://changch84.blog.sohu.com/
dirkchang@hotmail.com

TOP

 16 12
发新话题