热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

C++简单QQ程序服务器端的实现代码

这篇文章主要为大家详细介绍了C++简单QQ程序服务器端的实现代码,感兴趣的朋友可以参考一下

本文实例为大家分享了C++简单QQ程序服务器端的实现代码,供大家参考,具体内容如下

// ServerDlg.cpp : implementation file
//
#include "StdAfx.h"
#include "Server.h"
#include "ServerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
#define WM_SHOWTASK  WM_USER+1
#define WM_SERVER_ACCEPT WM_USER+2
/////////////////////////////////////////////////////////////////////////////
// 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()
 
/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog
 
CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
  : CDialog(CServerDlg::IDD, pParent)
{
  //{{AFX_DATA_INIT(CServerDlg)
  m_strShow = _T("");
  //}}AFX_DATA_INIT
  // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  m_People = 0;
}
 
void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CServerDlg)
  DDX_Control(pDX, IDC_STATIC_PEOPLE, m_strPeople);
  DDX_Control(pDX, IDC_STATIC_HOST, m_strHost);
  DDX_Text(pDX, IDC_EDIT_SHOW, m_strShow);
  //}}AFX_DATA_MAP
}
 
BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
  //{{AFX_MSG_MAP(CServerDlg)
  ON_WM_SYSCOMMAND()
  ON_WM_PAINT()
  ON_WM_QUERYDRAGICON()
  ON_MESSAGE(WM_SHOWTASK,OnShowTask)
  ON_MESSAGE(WM_SERVER_ACCEPT,OnServerAccept)
  ON_WM_DESTROY()
  ON_COMMAND(ID_EXIT,OnDlgExit)
  ON_COMMAND(ID_SHOW,OnDlgShow)
  ON_COMMAND(ID_HIDE,OnDlgHide)
  //}}AFX_MSG_MAP
  ON_STN_CLICKED(IDC_STATIC_HOST, &CServerDlg::OnStnClickedStaticHost)
END_MESSAGE_MAP()
 
/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers
 
BOOL CServerDlg::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  
  InitNotify();
  SetWindowText("服务器");//在对话框上面显示标题
          //1 加载Socket库
  InitSocket(); //2 初始化Socket库
  return TRUE; // return TRUE unless you set the focus to a control
}
 
VOID CServerDlg::GetHostInfor()
{
  char szHostName[128];  //获得Shine-PC                                      // 存储主机名
  if (gethostname(szHostName,128)==0)                          //正确的情况
  {
    hostent* pHost;  //Host Content
    pHost = gethostbyname(szHostName); //网卡信息                          //pHost返回的是指向主机的列表
    LPCSTR psz = inet_ntoa(*(struct in_addr *)pHost->h_addr_list[0]);//得到指向ip的psz变量
    CString strTemp;
    strTemp += "主机名:";
    strTemp += szHostName;
    strTemp += " ";
    strTemp+="IP地址:";
    strTemp+=psz;
    m_strHost.SetWindowText(strTemp);
    CString strPeople;
    strPeople.Format("%d",m_People);
    m_strPeople.SetWindowText(strPeople);
  }
}
 
VOID CServerDlg::InitSocket()
{
  WSADATA wsaData;
  WSAStartup(MAKEWORD(2,2),&wsaData);
  GetHostInfor(); 
  //创建监听套接字
  m_sListen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 
  if(m_sListen==INVALID_SOCKET)
  {
    MessageBox("Create SOCKET Failed");
    WSACleanup(); 
    return;
  }
  //初始化网卡
  //sockaddr_in(2.2) sockaddr(1.0)
  m_ServerAddr.sin_family = AF_INET;
  m_ServerAddr.sin_addr.s_addr = INADDR_ANY;
  m_ServerAddr.sin_port = htons(9527); 
  //1.0
  //绑定
  if(bind(m_sListen,
    (sockaddr*)&m_ServerAddr,sizeof(m_ServerAddr))==SOCKET_ERROR)
  {
    MessageBox("Bind Failed");
    WSACleanup(); 
    closesocket(m_sListen);
    return;
  }
  //监听
  if(listen(m_sListen,20)==SOCKET_ERROR)
  {
    MessageBox("Listen Failed");
    WSACleanup(); 
    closesocket(m_sListen);
    return;
  }
 
  WSAAsyncSelect(m_sListen,m_hWnd,WM_SERVER_ACCEPT,
    FD_ACCEPT|FD_READ|FD_CLOSE);
  for(int i=0;im_hWnd,SW_NORMAL);
}
 
//FD_ACCEPT FD_CLOSE  FD_READ
 
LRESULT CServerDlg::OnServerAccept(WPARAM wParam,LPARAM lParam)
{
  // WSAGETSELECTEVENT == LOWORD(lParam)
  int iEvent = WSAGETSELECTEVENT(lParam);
  //调用Winsock API函数,得到网络事件类型
  switch(iEvent) 
  { 
  case FD_ACCEPT://客户端连接请求事件 
    {
      OnAccept();  //用户的上线请求  Connect
      break;
    }
  case FD_CLOSE://客户端断开事件: 
    {
 
      OnClose(wParam);  //WParam 谁被关闭
      break;
    }
   
  case FD_READ://网络数据包到达事件 
    {
      OnRecive(wParam);  //Send 
      break;
    }
  default: break; 
  } 
  return 0;
}
 
VOID CServerDlg::OnRecive(WPARAM wParam)
{
  SENDINFOR Data = {0};
  int i = 0;
  for (i=0;i12
      //5 12 11|1.txt|5|
      //5 12 192.168.0.100|11|1.txt|5|  --->12
 
      CString strTemp;  
      strTemp+=m_PeopleInfor[i].strIp;
      strTemp+="|";
      strTemp+=Data.szMsg;
       
      strcpy(Data.szMsg,strTemp.LockBuffer());
 
 
 
      int j = 0;
      for (j=0;j

以上就是本文的全部内容,希望对大家的学习有所帮助。


推荐阅读
author-avatar
尽做好风水_549_881
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有