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

📄 serverdlg.cpp

📁 本书通过实例介绍了嵌入式编程的方法与技巧书中例子具有实用性
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include "server.h"
#include "serverDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
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()
CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CServerDlg)
// 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_iNextFree = 0;
m_iMaxConnects = 5;
m_iWorkProt = 0;
m_vtMsgBuf = new VTMSG; 
memset((void*)m_vtMsgBuf, 0, sizeof(SIZEVTMSG));
}
CServerDlg::~CServerDlg()
{
}
void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CServerDlg)
DDX_Control(pDX, IDC_CLIENT, m_ClientList);
DDX_Control(pDX, IDC_PROTOCOL, m_ProtList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_MESSAGE(VTM_CONNECTED, OnVTConnected)
ON_MESSAGE(VTM_DATAREADY, OnVTDataReady)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CServerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
if(!InitClientSocks()) 
{
AfxMessageBox("Can't build connection sockets!");
return FALSE;
}	
if(!InitServSocks())
{
AfxMessageBox("Can't build server sockets!");
return FALSE;
}	
char strProt[128]; 
for(int iLoop = 0; iLoop < m_iWorkProt; iLoop++)
{
 		wsprintf(strProt, "%s:  %d", m_lpServSocks[iLoop].m_lpProtocolName, 
                m_lpServSocks[iLoop].m_iConnects);
      m_ProtList.AddString(strProt); 
}
return TRUE;  // return TRUE  unless you set the focus to a control
}
void CServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CServerDlg::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();
}
}
HCURSOR CServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CServerDlg::InitServSocks(void) 
{
unsigned long  sizeProtBuf = 0; 
int            cNumProt;
int            iLoop1;
int            iLoop2;
int            iAddrLen;
WORD           wVersionRequested;
WSADATA        wsaData;
BOOL           bCastStat;
wVersionRequested = MAKEWORD(1, 1);
if(WSAStartup(wVersionRequested, &wsaData) != 0)
{
    return FALSE;
}
if(EnumProtocols(NULL, NULL, &sizeProtBuf) != SOCKET_ERROR)
{
 	 return FALSE;
}
// Allocate buffer of appropriate size
if((m_lpProtBuf = (LPPROTOCOL_INFO)VirtualAlloc(NULL, sizeProtBuf, 
                     MEM_COMMIT, PAGE_READWRITE)) == NULL)
{
return FALSE;
}
// Enumerate numbers of protocols 
if((cNumProt = EnumProtocols(NULL, m_lpProtBuf, &sizeProtBuf)) == 
                 SOCKET_ERROR)
{
return FALSE;
}
// Allocate the buffer for server sockets
if((m_lpServSocks = (LPVTSOCKET)VirtualAlloc(NULL, 
cNumProt * sizeof(VTSOCKET), MEM_COMMIT,PAGE_READWRITE)) == NULL)
{
return FALSE;
}
for(iLoop1 = 0, iLoop2 = 0; iLoop1 < cNumProt; iLoop1++)
{
if((m_lpProtBuf[iLoop1].dwServiceFlags & XP_CONNECTIONLESS) == 0)  
{
if(BuildServer(this->GetSafeHwnd(), &m_lpServSocks[iLoop2],  
                &m_lpProtBuf[iLoop1]))
{
 	iLoop2++;
}
}
}
m_iWorkProt =  iLoop2;
if((m_SAPSocket = socket(AF_IPX, SOCK_DGRAM, NSPROTO_IPX + 4)) != 
      INVALID_SOCKET)
{
  for(iLoop1 = 0; iLoop1 < m_iWorkProt; iLoop1++)
  {
     if(m_lpServSocks[iLoop1].m_iProtocol == NSPROTO_SPXII)
     {
       m_vtSAPData.m_iOperation = 0x200;  
       m_vtSAPData.m_iService = VT_NETID; 
       strcpy(m_vtSAPData.m_sServName, "VOICE TALK");  
       memcpy((void *)m_vtSAPData.m_sNetID,
               (void *)(m_lpServSocks[iLoop1].m_sockAddr.sa_data),12); 
       vtSAPData.m_iHops = 0;                 
       m_SAPSockAddr.sa_family = AF_IPX;
      Ascii2Hex("0452", (char *)&m_SAPSockAddr.sa_socket, 2);
      if(bind(m_SAPSocket, (PSOCKADDR)&m_SAPSockAddr,sizeof(SOCKADDR_IPX)) 
           == SOCKET_ERROR)
        {
         return FALSE;
        }
      else
      {
        m_SAPDestSockAddr.sa_family = AF_IPX;
        Ascii2Hex("00000000",m_SAPDestSockAddr.sa_netnum, 4);
        Ascii2Hex("FFFFFFFFFFFF", m_SAPDestSockAddr.sa_nodenum, 6);
        Ascii2Hex("0452", (char *)&m_SAPDestSockAddr.sa_socket, 2);
        bCastStat = TRUE;
        iAddrLen = sizeof(bCastStat);
        setsockopt(m_SAPSocket, SOL_SOCKET, SO_BROADCAST,                   
                    (char *)&bCastStat, sizeof(bCastStat));
        sendto(m_SAPSocket, (char *)&m_vtSAPData, sizeof(m_vtSAPData), 
                MSG_DONTROUTE, (struct sockaddr *)&m_SAPDestSockAddr, 
                sizeof(m_SAPDestSockAddr));
               SetTimer(1, 60000, NULL);
        }
       break;
    }
}
}
return TRUE;
}
BOOL CServerDlg::InitClientSocks(void)
{
if((m_hConnectHeap = HeapCreate(0, sizeof(VTSOCKET) * 5, sizeof(VTSOCKET) 
    * 100)) == NULL)
{
return FALSE;
}
if((m_lpClientSocks = (LPVTSOCKET)HeapAlloc(m_hConnectHeap, 0,  
    sizeof(VTSOCKET) * 5)) == NULL)
{
return FALSE;
}
return TRUE;
}
void CServerDlg::DeRegisterClient(char* sClientName)
{
int iLoop;
m_vtMsgBuf->m_ucIdentity = VT_IDENTITY;
m_vtMsgBuf->m_ucMsgType = VT_TEXT;
m_vtMsgBuf->m_lLength = REALLEN(sClientName) + SIZEVTMSGHDR;
m_vtMsgBuf->m_ucCmd = VTCMD_DEREGNAME;
memcpy((void*)m_vtMsgBuf->m_pData, (void*)sClientName, 
         REALLEN(sClientName));
for(iLoop = 0; iLoop < m_iNextFree; iLoop++)
{
    if(m_lpClientSocks[iLoop].m_iStatus== VTSOCKET_AVAILABLE)
   {  
     SendVTMessage(m_lpClientSocks[iLoop].m_hSock, m_vtMsgBuf);
  }
}
}
void CServerDlg::UpdateProtList(int index)
{
int iReVal;
char strProt[128]; 
iReVal = m_ProtList.FindString(-1,m_lpServSocks[index].m_lpProtocolName);
if(iReVal == LB_ERR)
   return;
m_ProtList.DeleteString(iReVal);
wsprintf(strProt, "%s:  %d", m_lpServSocks[index].m_lpProtocolName, 
          m_lpServSocks[index].m_iConnects);
m_ProtList.InsertString(iReVal, strProt); 
}
void CServerDlg::UpdateClientList(char* name,int status, char* peername)
{
int index;
char strClient[128];
index = m_ClientList.FindString(-1, name);
if(index != LB_ERR)
{
m_ClientList.DeleteString(index);  
}
switch(status)
{
 case VTSOCKET_CLOSED:
 return;  
case VTSOCKET_AVAILABLE:  
   wsprintf(strClient, "%s  AVAILABLE", name);
   break;
case VTSOCKET_REQSESSION: 
   wsprintf(strClient, "%s  SESSION SETUP  %s",name, peername);
   break;
case VTSOCKET_INSESSION: 
   wsprintf(strClient, "%s  in SESSION", name);
   break;
}
m_ClientList.InsertString(index, strClient);
}
void CServerDlg::OnTimer(UINT nIDEvent) 
{
// Add message handler code 
// Send out SAP package
sendto(m_SAPSocket, (char *)&m_vtSAPData, sizeof(m_vtSAPData), 
        MSG_DONTROUTE, (struct sockaddr *)&m_SAPDestSockAddr, 
        sizeof(m_SAPDestSockAddr));
CDialog::OnTimer(nIDEvent);
}
LONG CServerDlg::OnVTConnected(WPARAM wparam, LPARAM lparam)
{
int index, addrlen;

⌨️ 快捷键说明

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