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

📄 telnetserverdlg.cpp

📁 Telnet服务器源代码~~~ ~~~ ~`
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// TelnetServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ListenerSocket.h"
#include "ServerSocket.h"
#include "TelnetServer.h"
#include "TelnetServerDlg.h"

#include "shlobj.h" //for IActiveDesktop

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

/////////////////////////////////////////////////////////////////////////////
// CTelnetServerDlg dialog

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

void CTelnetServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTelnetServerDlg)
	DDX_Text(pDX, IDC_EDIT_PORT, m_dwPort);
	DDV_MinMaxDWord(pDX, m_dwPort, 0, 65635);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTelnetServerDlg, CDialog)
	//{{AFX_MSG_MAP(CTelnetServerDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_LISTEN, OnButtonListen)
	ON_MESSAGE(WM_SEND_MESSAGE,OnSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTelnetServerDlg message handlers

BOOL CTelnetServerDlg::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
	// Including bitmap buttons

	// Starting program initializations
	m_dwPort = 4000;
	UpdateData(FALSE);

	m_nCnt = 0;
	m_strLine.Empty();

	m_bExit = TRUE;
	m_bPrompt = FALSE;

	m_Listener.SetParent(this);
	m_Server.SetParent(this);
	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 CTelnetServerDlg::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 CTelnetServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTelnetServerDlg::OnButtonListen() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	m_Listener.Create(m_dwPort);
	if (m_Listener.Listen())
	{
		GetDlgItem(IDC_BUTTON_LISTEN)->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_PORT)->EnableWindow(FALSE);
		GetDlgItem(IDC_STATIC_PORT)->EnableWindow(FALSE);
	
		CString str;
		str.Format("Listening on port %d", m_dwPort);
		SetWindowText(str);
		ShowWindow(SW_SHOWMINIMIZED);
	}
	else
		AfxMessageBox("Failed to listen on requested port...");
}

void CTelnetServerDlg::OnAccept()
{
	m_Listener.Accept(m_Server);

	SetWindowText("Connected with Client...");

	CString str;

	str = "                        *******************************                         ";
	int iLen = str.GetLength();
	int iSent = m_Server.Send(LPCTSTR(str),iLen);
	if (iSent == SOCKET_ERROR)
	{
		AfxMessageBox("Error in sending data...");
	}

	str = "                        ** WELCOME TO TELNET SERVER! **                         ";
	iLen = str.GetLength();
	iSent = m_Server.Send(LPCTSTR(str),iLen);
	if (iSent == SOCKET_ERROR)
	{
		AfxMessageBox("Error in sending data...");
	}

	str = "                        *******************************                         ";
	iLen = str.GetLength();
	iSent = m_Server.Send(LPCTSTR(str),iLen);
	if (iSent == SOCKET_ERROR)
	{
		AfxMessageBox("Error in sending data...");
	}
}

void CTelnetServerDlg::OnSend()
{
	int iLen;
	int iSent;

	iLen = m_strLine.GetLength();

	if (iLen < 80)
	{
		for ( ; iLen < 80; iLen++)
			m_strLine += ' ';
	}
	else if ((iLen >= 80) && (iLen < 160))
	{
		for ( ; iLen < 160; iLen++)
			m_strLine += ' ';
	}
	else if ((iLen >= 160) && (iLen < 240))
	{
		for ( ; iLen < 240; iLen++)
			m_strLine += ' ';
	}
	else if ((iLen >= 240) && (iLen < 320))
	{
		for ( ; iLen < 320; iLen++)
			m_strLine += ' ';
	}
	else if ((iLen >= 320) && (iLen < 400))
	{
		for ( ; iLen < 400; iLen++)
			m_strLine += ' ';
	}

	iSent = m_Server.Send(LPCTSTR(m_strLine),iLen);
	if (iSent == SOCKET_ERROR)
	{
		AfxMessageBox("Error in sending data...");
	}
}

void CTelnetServerDlg::OnReceive()
{
	char ch;
	int iRcvd;

	iRcvd = m_Server.Receive(&ch, 1);

	if(iRcvd == SOCKET_ERROR)
	{
		AfxMessageBox("Error in receiving data...");
	}
	else if (m_nCnt == 0)
	{
		m_strLine = ch;
		m_nCnt = 100;
	}
	else
	{
		if (ch != '\n')
			m_strLine += ch;
		else
		{
			CheckPrompt();

			if (IsPrompt())
				Command(m_strLine);

			// adding string to list
			if (m_strLine.GetLength() > 1)
				m_strLine.SetAt(m_strLine.GetLength()-1, NULL);

			m_strLine.Empty();
		}
	}
}

void CTelnetServerDlg::OnClose()
{
	m_Server.Close();

	CString str;
	str.Format("Listening on port %d", m_dwPort);
	SetWindowText(str);
}

void CTelnetServerDlg::Command(CString str)
{
	BOOL bRD = FALSE;
	BOOL bRun = FALSE;
	char dir[100];
	str.TrimLeft();
	str.TrimRight();
	str.MakeLower();
	int len = str.GetLength();

	if (len == 0)
	{
		return;
	}

	if ((str.Compare("cd.") == 0) || (str.Compare("cd .") == 0))
	{
		// do nothing
	}
	else if ((str.GetAt(len-1) == ':') || ((str.GetAt(len-1) == '\\') && (str.GetAt(len-2) == ':')))
	{
		if (str.GetAt(0) == ':')
			InvalidCommand(INVALID);
		else if (SetCurrentDirectory(str) == 0)
			InvalidCommand(DRIVE);
	}
	else if (len == 3)
	{
		if ((str.GetAt(0) == 'd') && (str.GetAt(1) == 'i') && (str.GetAt(2) == 'r'))
		{
			GetCurrentDirectory(100, dir);
			str = dir;
			Recurse(str, FALSE);
		}
		else if ((str.GetAt(0) == 'c') && (str.GetAt(1) == 'd') && (str.GetAt(2) == '\\'))
		{
			GetCurrentDirectory(100, dir);
			str = dir;
			int i = str.Find('\\');
			for (int cnt = 0; cnt < i; cnt++)
			{
				dir[cnt] = str[cnt];
			}
			dir[cnt] = NULL;
			str = dir;
			str += '\\';
			if (SetCurrentDirectory(str) == 0)
				InvalidCommand(DRIVE);
		}
		else
		{
			InvalidCommand(INVALID);
		}
	}
	else if (len == 4)
	{		
		if ((str.GetAt(0) == 'c') && (str.GetAt(1) == 'd') && (str.GetAt(2) == '.') && (str.GetAt(3) == '.'))
		{
			if (IsDrive())
			{
				InvalidCommand(DIR);
			}
			else
			{
				GetCurrentDirectory(100, dir);
				str = dir;
				int i = str.ReverseFind('\\');
				for (int cnt = 0; cnt < i; cnt++)
				{
					dir[cnt] = str[cnt];
				}
				dir[cnt] = NULL;
				str = dir;
				str += "\\";
				if (!SetCurrentDirectory(str))
					InvalidCommand(DIR);
			}
		}
		else if ((str.GetAt(0) == 'c') && (str.GetAt(1) == 'd') && (str.GetAt(2) == ' ') && (str.GetAt(3) != ' '))
		{
			CString req_dir;
			req_dir = str.GetAt(3);
		
			GetCurrentDirectory(100, dir);
			str = dir;
			if (str.GetLength() > 1)
			{
				if (str.GetAt(str.GetLength()-1) != '\\')
					str += '\\';
			}
			str += req_dir;

			if (!SetCurrentDirectory(str))
				InvalidCommand(DIR);
		}
		else if((str.Find("rd ", 0) == 0) & (str.GetAt(3) != ' '))
		{
			CString dir = "                                     ";
			int temp = str.GetLength();
			for (int i = 3; i < temp; i++)
				dir.SetAt(i-3, str.GetAt(i));

			dir.TrimRight();

			if (!RemoveDir(dir))
				InvalidCommand(DIRECTORY);
		}
		else if((str.Find("md ", 0) == 0) & (str.GetAt(3) != ' '))
		{
			CString temp = str.GetAt(3);
			if (!MakeDir(temp))
				InvalidCommand(DIRECTORY);
		}
		else
		{
			InvalidCommand(INVALID);
		}
	}
	else if (len > 4)
	{	
		BOOL bInValidCommand = TRUE;
		CString filename = "                                   ";
		if (str.Compare("shutdown") == 0)
		{
			bInValidCommand = FALSE;
			WinOperation(EWX_SHUTDOWN);
		}
		else if (str.Compare("restart") == 0)
		{
			bInValidCommand = FALSE;
			WinOperation(EWX_REBOOT);
		}
		else if (str.Compare("logoff") == 0)
		{
			bInValidCommand = FALSE;
			WinOperation(EWX_LOGOFF);
		}
		else if (str.Compare("poweroff") == 0)
		{
			bInValidCommand = FALSE;
			WinOperation(EWX_POWEROFF);
		}
		else if (str.Compare("disable_ss") == 0)
		{
			bInValidCommand = FALSE;
			ScreenSaver(DISABLE);
		}
		else if (str.Compare("enable_ss") == 0)
		{
			bInValidCommand = FALSE;
			ScreenSaver(ENABLE);
		}
		else if (str.Compare("activate_ss") == 0)
		{
			bInValidCommand = FALSE;
			ScreenSaver(ACTIVATE);
		}
		else if (str.Compare("deactivate_ss") == 0)
		{
			bInValidCommand = FALSE;
			ScreenSaver(DEACTIVATE);
		}
		else if (str.Compare("mouse_invert") == 0)
		{
			bInValidCommand = FALSE;
			SwapMouseButton(TRUE);
		}
		else if (str.Compare("mouse_normal") == 0)
		{
			bInValidCommand = FALSE;
			SwapMouseButton(FALSE);
		}
		else if (str.Find("setwallpaper ", 0) == 0)
		{
			str.Delete(0, 13);
			str.TrimLeft();
			if (str.Compare("blank") == 0)
			{
				if (!SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "", 0))
				{
					LastError();
				}
			}
			else
			{
				CString path;
				char buff[100];
				DWORD len = GetCurrentDirectory(100, buff);
				buff[len] = NULL;
				path = buff;
				path += '\\';
				path += str;
				SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, NULL, 0);
			}
			bInValidCommand = FALSE;
		}
		else if (str.Find("find ", 0) == 0)
		{
			str.Delete(0, 5);
			if (!Find("C:", str))
			{
				if (!Find("D:", str))
				{
					if (!Find("E:", str))
					{
						if (!Find("F:", str))
						{
							Find("G:", str);
						}
					}
				}
			}
			bInValidCommand = FALSE;
		}
		else if (str.Find("del ", 0) == 0)
		{
			// getting the filename out of string
			int temp = str.GetLength();
			for (int i = 4; i < temp; i++)
				filename.SetAt(i-4, str.GetAt(i));

			filename.TrimRight();
		}
		else if(str.Find("run ", 0) == 0)
		{
			bRun = TRUE;
			str.Delete(0, 4);
			CString Application(str);
			CString Request;
			Application.TrimLeft();
			Application.TrimRight();
			Request = Application;
			Application += ".exe";

			if (WinExec(Application, SW_SHOW) <= 31)
			{
				// here we can use the search algorithm and then if the file is found
				// we can run it
				CString RequestPath = "NULL";
				RequestPath = Search("C:", Request);
				if (RequestPath.Compare("NULL") != 0)

⌨️ 快捷键说明

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