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

📄 spserdlg.cpp

📁 中国联通短信平台服务器端
💻 CPP
字号:
// SPSerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SPSer.h"
#include "SPSerDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CSPSerDlg dialog

CSPSerDlg::CSPSerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSPSerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSPSerDlg)
		// 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);
}

void CSPSerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSPSerDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSPSerDlg, CDialog)
	//{{AFX_MSG_MAP(CSPSerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_STARTSERVICE, OnStartService)
	ON_MESSAGE(NETWORK_EVENT, OnNetEvent)
	ON_MESSAGE(IP_EVENT, OnClientNetEvent)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSPSerDlg message handlers

BOOL CSPSerDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CSPSerDlg::OnStartService() 
{
	// TODO: Add your control notification handler code here
	this->InitNetwork ();
}

BOOL CSPSerDlg::InitNetwork()
{
	WSADATA wsaData;

    //初始化TCP协议
    BOOL ret = WSAStartup(MAKEWORD(2,2), &wsaData);
    if(ret != 0)
    {
        MessageBox("初始化网络协议失败!");
        return FALSE;
    }

    //创建服务器端套接字
    ServerSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(ServerSock == INVALID_SOCKET)
    {
        MessageBox("创建套接字失败!");
        closesocket(ServerSock);
        WSACleanup();
        return FALSE;
    }

    //绑定到本地一个端口上
    sockaddr_in localaddr;
    localaddr.sin_family = AF_INET;
    localaddr.sin_port = htons(8801);  //端口号不要与其他应用程序冲突
    localaddr.sin_addr.s_addr = 0;
	int nRet = bind(ServerSock ,(struct sockaddr*)&localaddr,sizeof(sockaddr));
	if (nRet == SOCKET_ERROR)
    {
        MessageBox("绑定地址失败!");
        closesocket(ServerSock);
        WSACleanup();
        return FALSE;
    }
 
    //将SeverSock设置为异步非阻塞模式,并为它注册各种网络异步事件,其中m_hWnd      
    //为应用程序的主对话框或主窗口的句柄
	nRet = WSAAsyncSelect(ServerSock, this->m_hWnd , NETWORK_EVENT, FD_ACCEPT|FD_READ|FD_CLOSE);
    if (nRet == SOCKET_ERROR)
	{
        MessageBox("注册网络异步事件失败!");
        WSACleanup();
        return FALSE;
    }

    listen(ServerSock, 5);// file://设置侦听模式
    return TRUE;

}


//下面定义网络异步事件的回调函数
LRESULT CSPSerDlg::OnClientNetEvent(WPARAM wParam, LPARAM lParam)
{
    //调用Winsock API函数,得到网络事件类型
    int iEvent = WSAGETSELECTEVENT(lParam);

    //调用Winsock API函数,得到发生此事件的客户端套接字
    SOCKET CurSock= (SOCKET)wParam;

    switch(iEvent)
    {
        case FD_CLOSE:       //客户端断开事件:
            OnClientClose(CurSock);
            break;
        case FD_READ:        //网络数据包到达事件
            OnClientReceive(CurSock);
            break;
         default: 
			 break;
     }

	return 1;

}


//下面定义网络异步事件的回调函数
LRESULT CSPSerDlg::OnNetEvent(WPARAM wParam, LPARAM lParam)
{
    //调用Winsock API函数,得到网络事件类型
    int iEvent = WSAGETSELECTEVENT(lParam);

    //调用Winsock API函数,得到发生此事件的客户端套接字
    SOCKET CurSock= (SOCKET)wParam;

    switch(iEvent)
    {
        case FD_ACCEPT:      //客户端连接请求事件
            OnAccept(CurSock);
            break;
        case FD_CLOSE:       //客户端断开事件:
            OnClose(CurSock);
            break;
        case FD_READ:        //网络数据包到达事件
            OnReceive(CurSock);
            break;
         case FD_WRITE:      //发送网络数据事件
            OnSend(CurSock);
            break;
         default: break;
    }

	return 1;

}

void CSPSerDlg::OnAccept(SOCKET CurSock)
{
    //接受连接请求,并保存与发起连接请求的客户端进行通信Socket
    //为新的socket注册异步事件,注意没有Accept事件
	int len = sizeof (sockaddr);

  //  sockaddr_in			ClientAddr;
//    ClientAddr.sin_family = AF_INET;
//    ClientAddr.sin_port = htons(8801);  //端口号不要与其他应用程序冲突
//    ClientAddr.sin_addr.s_addr = 0;

	SOCKET  ClientSocket = accept (CurSock, (struct sockaddr*)&ClientAddr, &len);

	if (WSAAsyncSelect (ClientSocket, this->m_hWnd ,IP_EVENT, FD_CLOSE|FD_READ) == SOCKET_ERROR)
	{
		AfxMessageBox ("注册网络异步事件失败");
		return;
	}
}
 


void CSPSerDlg::OnSend(SOCKET CurSock)
{
    //在给客户端发数据时做相关预处理
}

void CSPSerDlg::OnClose(SOCKET CurSock)
{
    //结束与相应的客户端的通信,释放相应资源
}

void CSPSerDlg::OnReceive (SOCKET CurSock)
{
    //读出网络缓冲区中的数据包
	Sgip_Bind recvMMM;
	ZeroMemory (&recvMMM, sizeof(Sgip_Bind));

	DWORD dwSize = 0;

	int nRet = ioctlsocket(CurSock, FIONREAD, &dwSize);
	if (nRet == SOCKET_ERROR)//接收出错
		return ;

	if (dwSize == 0)//无数据接收
		return ;


	char buf[200];
	ZeroMemory (buf, sizeof(buf));

//	char *pData = new char[dwSize+1];
//	ZeroMemory (pData, sizeof(pData));
	
	int nRead = recv(CurSock, buf, dwSize, 0);
	if (nRead == SOCKET_ERROR)
		return ;
///////////////////////////////////////////////////////

//	pData[dwSize] = '\0';


//	CString  str = pData;

//	delete [] pData;

	memcpy (&recvMMM, buf, sizeof(Sgip_Bind));


}


void CSPSerDlg::OnClientClose(SOCKET CurSock)
{
    //结束与相应的客户端的通信,释放相应资源
}

void CSPSerDlg::OnClientReceive (SOCKET CurSock)
{
    //读出网络缓冲区中的数据包
    //读出网络缓冲区中的数据包
	MMM recvMMM;

	DWORD dwSize = 0;

	int nRet = ioctlsocket(CurSock, FIONREAD, &dwSize);
	if (nRet == SOCKET_ERROR)//接收出错
		return ;

	if (dwSize == 0)//无数据接收
		return ;


	char *pData = new char[dwSize+1];
	ZeroMemory (pData, sizeof(pData));
	
	int nRead = recv(CurSock, pData, dwSize, 0);
	if (nRead == SOCKET_ERROR)
		return ;


	pData[dwSize] = '\0';


	CString  str = pData;

	delete [] pData;

	memcpy (&recvMMM, str, sizeof(MMM));
}

⌨️ 快捷键说明

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