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

📄 gprsserverdlg.cpp

📁 gprs
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// GprsServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GprsServer.h"
#include "GprsServerDlg.h"

#include "protocol.h"

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


#define FLAG		2
#define SIZEFILE	1024
UINT _SendMsgThread(LPVOID lparam);	//TCP发送信息线程
UINT _ListenTcpThread(LPVOID lparam);

extern uchar g_cSendLen;
extern uchar g_cSendBuf[50];

extern CString sServerIP;
extern CString sSrcPath;
extern CString sDstPath;
extern UINT iPort;
extern UINT iVersion;

extern uchar g_bLink;


extern void Client_ReceiveFileMsg(LPVOID lparam, CSocket &recSo);
extern void Server_MsgHandler(LPVOID lparam, CSocket &recSo);


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/************************************************************
* ADD BY DAVIDLI
*************************************************************/

#define PORT		2020
#define FLAG		2
#define SIZEFILE	1024


CWinThread	*pThreadSendFile;		//发送文件线程-->_SendFileThread
CWinThread	*pThreadclient;	//发送消息线程
CWinThread	*pThreadLisen;			//监听线程-->_ListenTcpThread

CWinThread	*pReceiveThread;		//接受线程-->_ReceiveThread
//CWinThread  *pThreadCaputre;		//抓取屏幕线程
CWinThread  *pSearchSendFile;

extern PACKAGE_HEAD packData; 
/////////////////////////////////////////////////////////////////////////////
// CGprsServerDlg dialog

CGprsServerDlg::CGprsServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGprsServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGprsServerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_nSockType = SOCK_TCP;   //TCP
	m_WorkType = CLIENT_MODE;   //client
	m_client = 1;
	m_server = 2;
	FileWork = false;
	FileStop = false;
	StopServer = false;
	ReadConfigFileInfo();
//	AfxMessageBox(sDstPath);
}

void CGprsServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGprsServerDlg)
	DDX_Control(pDX, IDC_PROGRESS_SEND_FILECOUNT, m_ProgressFileCnt);
	DDX_Control(pDX, IDC_PROGRESS_SEND_FILE, m_Progress);
	DDX_Control(pDX, IDC_LIST_BOX_ADDMSG, m_AddMsgList);
	DDX_Control(pDX, IDC_IPADDRESS, m_YourIP);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGprsServerDlg, CDialog)
	//{{AFX_MSG_MAP(CGprsServerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RADIO_TCP, OnRadioTcp)
	ON_BN_CLICKED(IDC_RADIO_UDP, OnRadioUdp)
	ON_BN_CLICKED(IDC_RADIO_SERVER, OnRadioServer)
	ON_BN_CLICKED(IDC_RADIO_CLIENT, OnRadioClient)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, OnButtonDisconnect)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
	ON_BN_CLICKED(IDC_BUTTON_FILE_STOP, OnButtonFileStop)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_UPDATE_PIC, OnButtonUpdatePic)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGprsServerDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	/////////////////////////////////////////////////////////
	CString strLocalName;
	GetLocalHostName(strLocalName);
	CString strLocalIP;
	GetIpAddress(strLocalName,strLocalIP);
	m_YourIP.SetWindowText(strLocalIP);	//设置默认IP为本机
	this->AddMsgList(strLocalIP+"->"+strLocalName,"创建初始化成功");

	/////////////////////////////////////////////////////////
	((CButton*)GetDlgItem(IDC_RADIO_CLIENT))->SetCheck(BST_CHECKED);//默认为服务器、客户端一体
	SetWindowText("GPRS TCP方式");

	GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("启    动");
	GetDlgItem(IDC_BUTTON_DISCONNECT)->SetWindowText("关    闭");
	
	((CButton*)GetDlgItem(IDC_RADIO_TCP))->SetCheck(BST_CHECKED);	//默认为TCP
	GetDlgItem(IDC_RADIO_UDP)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON_UPDATE_PIC)->EnableWindow(false);		//发送消息不可用
	GetDlgItem(IDC_BUTTON_FILE_STOP)->EnableWindow(false);		//发送文件不可用
	GetDlgItem(IDC_BUTTON_CLEAR)->EnableWindow(true);			//清除不可用
	GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(false);		//断开连接不可用
	GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_PROGRESS_SEND_FILECOUNT)->ShowWindow(SW_HIDE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGprsServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CGprsServerDlg::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 CGprsServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGprsServerDlg::OnRadioTcp() 
{
	// TODO: Add your control notification handler code here
	// Add By DavidLi
	m_nSockType=SOCK_TCP;
	CString text;
	if(m_WorkType==SERVER_MODE) 
		text="GPRS服务器";
	else 
		text="GPRS客户端";
	SetWindowText(text+" TCP方式");
}

void CGprsServerDlg::OnRadioUdp() 
{
	// TODO: Add your control notification handler code here
	// *********Add By DavidLi************
	m_nSockType=SOCK_UDP;
	CString text;
	if(m_WorkType==SERVER_MODE) 
		text="GPRS服务器";
	else 
		text="GPRS客户端";
	SetWindowText(text+" UDP方式");
}

void CGprsServerDlg::OnRadioServer() 
{
	// TODO: Add your control notification handler code here
	// TODO: Add your control notification handler code here
	CString text;

	if(m_nSockType==SOCK_TCP) 
		text="TCP方式";
	else 
		text="UDP方式";

	m_client = 2;
	m_server = 1;
	m_WorkType = SERVER_MODE;				//服务器模式

	SetWindowText("GPRS服务器 "+text);
	GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("启动服务");
	GetDlgItem(IDC_BUTTON_DISCONNECT)->SetWindowText("关闭服务");
	GetDlgItem(IDC_BUTTON_UPDATE_PIC)->EnableWindow(true);
}

void CGprsServerDlg::OnRadioClient() 
{
	// TODO: Add your control notification handler code here
	CString text;

	if(m_nSockType==SOCK_TCP)
		text="TCP方式";
	else 
		text="UDP方式";

	m_client = 1;
	m_server = 2;
	m_WorkType = CLIENT_MODE;

	SetWindowText("GPRS客户端 "+text);
	GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("连    接");
	GetDlgItem(IDC_BUTTON_DISCONNECT)->SetWindowText("断开连接");
	
	GetDlgItem(IDC_BUTTON_UPDATE_PIC)->EnableWindow(false);
}

CString CGprsServerDlg::GetError(DWORD error)	//返回错误信息
{
	CString strError;
	switch(error)
	{
	case WSANOTINITIALISED:
		strError="初始化错误";
		break;
	case WSAENOTCONN:
		strError="对方没有启动";
		break;
	case WSAEWOULDBLOCK :
		strError="对方已经关闭";
		break;
	case WSAECONNREFUSED:
		strError="连接的尝试被拒绝";
		break;
	case WSAENOTSOCK:
		strError="在一个非套接字上尝试了一个操作";
		break;
	case WSAEADDRINUSE:
		strError="特定的地址已在使用中";
		break;
	case WSAECONNRESET:
		strError="与主机的连接被关闭";
		break;
	default:
		strError="一般错误";	
	}
	return strError;
	
}


/**********addition functions writed by davidli*******************/

int CGprsServerDlg::GetLocalHostName(CString &sHostName)	//获得本地计算机名称
{
	char szHostName[256];
	int nRetCode;
	nRetCode = gethostname(szHostName,sizeof(szHostName));
	if(nRetCode!=0)
	{
		//产生错误
		sHostName=_T("没有取得");

⌨️ 快捷键说明

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