⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 telnetdlg.cpp

📁 用c++开发的telnet程序。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// TelnetDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Telnet.h"
#include "TelnetDlg.h"
#include "io.h"

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

#define WSA_MAJOR_VERSION 1
#define WSA_MINOR_VERSION 1
#define WSA_VERSION MAKEWORD(WSA_MAJOR_VERSION, WSA_MINOR_VERSION)

#define SOCKADDR_LEN sizeof(struct sockaddr)
#define MY_BUF_SIZE 500
#define MY_TIMEOUT  2000
#define MTU_SIZE    1460

#define TNC_SE				  240			 // F0 - End of subnegotiation 
#define TNC_NOP				241			   // F1 - No operation 
#define TNC_DM				 242		   // F2 - Data Mark 
#define TNC_BRK              243		   // F3 - Break signal 
#define TNC_IP                 244			// F4 - Interrupt process 
#define TNC_AO               245		  // F5 - Abort output 
#define TNC_AYT             246			 // F6 - Are you there function 
#define TNC_EC                247		   // F7 - Erase character 
#define TNC_EL                248		   // F8 - Erase line 
#define TNC_GA               249		  // F9 - The go ahead signal 
#define TNC_SB               250		  // FA - Subnegotiation start 
#define TNC_WILL           251		   // FB - we want to do this 
#define TNC_WONT        252			// FC - we won't do that 
#define TNC_DO               253		// FD - We want you to do this 
#define TNC_DONT         254		// FE - we want you not to do that 
#define TNC_IAC              255	   // FF - Interpret as command 
#define TNO_ECHO              1		   // 01 - Echo received characters 
#define TNO_SGA                 3		// 03 - Suppress go ahead 
#define TNO_TTYTYPE      24       // 18 - terminal type 
#define TTYTYPE_SEND       1       // 01 - 'send' terminal type 
#define TTYTYPE_IS            0       // 00 - terminal type 'is' .... 

/////////////////////////////////////////////////////////////////////////////
// CTelnetDlg dialog

CTelnetDlg::CTelnetDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTelnetDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTelnetDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CTelnetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTelnetDlg)
	DDX_Control(pDX, IDC_IPADDRESS, m_IPAddr);
	DDX_Control(pDX, IDC_PASSWORD, m_password);
	DDX_Control(pDX, IDC_LOGIN_NAME, m_loginname);
	DDX_Control(pDX, IDC_CONNECT, m_connect);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTelnetDlg, CDialog)
	//{{AFX_MSG_MAP(CTelnetDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTelnetDlg message handlers

BOOL CTelnetDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTelnetDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTelnetDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTelnetDlg::OnConnect() 
{
	int nRet;
	long lcmd = FIONBIO;
	WSADATA stWSAData;
	char lpInBuf[MY_BUF_SIZE];
	CString strMyBuf, strTemp;
	LPSTR lpOutBuf = strMyBuf.GetBuffer(50);
	char *pCommand;

	m_IPAddr.GetWindowText(m_ipaddr);
	m_loginname.GetWindowText(m_strLoginName);
	m_password.GetWindowText(m_strPassword);

	nRet = WSAStartup(WSA_VERSION, &stWSAData);
	if (nRet) 
	{
		AfxMessageBox("Could not connect.");
	} 

	if(LOBYTE(stWSAData.wVersion) != 1 || HIBYTE(stWSAData.wVersion) != 1) 
	{
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */    
		WSACleanup( );
	}
	else
	{
		hSock = ConnectTCP(m_ipaddr);

		if(hSock != INVALID_SOCKET)
		{
			ioctlsocket(hSock, lcmd, (u_long*)&lcmd);

			nRet = RecvData(hSock, lpInBuf, MY_BUF_SIZE, MY_TIMEOUT);

			lpInBuf[nRet] = '\x0';
			
			lpOutBuf[0] = (char)TNC_IAC;
			lpOutBuf[1] = (char)TNC_WONT;
			lpOutBuf[3] = 0x00;
			
			while(nRet)
			{
				char *pTemp;
				pCommand = strstr(lpInBuf, "login:");
				if(pCommand)
					break;
				
				pTemp = strchr(lpInBuf, TNC_IAC);
				
				while(pTemp)
				{
					if(pTemp[1] == (char)TNC_DO)
					{
						pCommand = ++pTemp;
						lpOutBuf[2] = pCommand[1];
						nRet = SendData(hSock, lpOutBuf, 3);
						
						pTemp = pCommand + 2;
						pCommand = strchr(pTemp, TNC_IAC);
						pTemp = pCommand;
					}
					else
					{
						pCommand = strchr(++pTemp , TNC_IAC);
						pTemp = pCommand;
					}
				}
				nRet = RecvData(hSock, lpInBuf, MY_BUF_SIZE, MY_TIMEOUT);
				lpInBuf[nRet] = '\x0';
			}

			int lCount = 3;
			while(lCount)
			{
				strMyBuf = CString("\r\n\x0");
				m_strLoginName = m_strLoginName + strMyBuf;
				lpOutBuf = m_strLoginName.GetBuffer(50);
				m_strLoginName.ReleaseBuffer();
				
				nRet = SendData (hSock, lpOutBuf, m_strLoginName.GetLength());
				
				nRet = RecvData(hSock, lpInBuf, MY_BUF_SIZE, MY_TIMEOUT);
				lpInBuf[nRet] = '\x0';
				if(strstr(lpInBuf, "login:"))
				{
					AfxMessageBox("ERROR : Please check the login name.");
				}
				
				m_strPassword = m_strPassword + strMyBuf;
				lpOutBuf = m_strPassword.GetBuffer(50);
				m_strPassword.ReleaseBuffer();
				
				nRet = SendData (hSock, lpOutBuf, m_strPassword.GetLength());
				nRet = RecvData(hSock, lpInBuf, MY_BUF_SIZE, MY_TIMEOUT);
				lpInBuf[nRet] = '\x0';

				if(strstr(lpInBuf, "Login incorrect"))
				{
					lCount--;
					continue;
				}

				while(nRet > 0)
				{
					nRet = RecvData(hSock, lpInBuf, MY_BUF_SIZE, MY_TIMEOUT);
				}
				break;
			}
			if(!lCount)
			{
				AfxMessageBox("Invalid Login name or password.");
			}
			else
			{
				AfxMessageBox("Connection successful.");
			}
		}
		else
		{
			AfxMessageBox("Could not connect.");
		}
		AfxMessageBox("Closing the connection.");
		CloseTCP(hSock, 0, 0);
		WSACleanup();
	}
}

/**********************************************************************************/
// Get a TCP socket and connect to host.
/**********************************************************************************/
SOCKET CTelnetDlg::ConnectTCP(CString& strDest)
{
	int nRet;
	SOCKADDR_IN stRmtName;
	LPSERVENT lpServent;
	
	/* Get a TCP socket */
	hSock = socket(AF_INET, SOCK_STREAM, 0);
	if (hSock == INVALID_SOCKET) 
	{
		AfxMessageBox("ERROR");
	} 
	else 
	{
		/* Get destination address */
		LPSTR szDestination = (LPSTR)strDest.GetBuffer(strDest.GetLength());
		stRmtName.sin_addr.s_addr = GetAddr(szDestination);
		strDest.ReleaseBuffer();
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -