程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++簡略QQ法式辦事器真個完成代碼

C++簡略QQ法式辦事器真個完成代碼

編輯:關於C++

C++簡略QQ法式辦事器真個完成代碼。本站提示廣大學習愛好者:(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;i<MAX;i++)
  {
    m_sClient[i] = INVALID_SOCKET;
  }
  m_strShow="Now server is working!\r\nWaiting for client's connecting .......";
  UpdateData(false);
  return;
}
 
void CServerDlg::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 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();
  }
}
 
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CServerDlg::OnQueryDragIcon()
{
  return (HCURSOR) m_hIcon;
}
 
VOID CServerDlg::InitNotify()
{
  m_nid.cbSize = sizeof(NOTIFYICONDATA);
  m_nid.hWnd = m_hWnd;
  m_nid.uID = IDI_ICON_NOTIFY; 
  m_nid.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP; 
  m_nid.uCallbackMessage = WM_SHOWTASK;  //自界說新聞  在托盤上處置鼠標舉措
  m_nid.hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON_NOTIFY)); 
  strcpy(m_nid.szTip,"點我");//當鼠標放在下面時,所顯示的內容 
  Shell_NotifyIcon(NIM_ADD,&m_nid);//在托盤區添加圖標 
}
 
LRESULT CServerDlg::OnShowTask(WPARAM wParam,LPARAM lParam)
{
//wParam吸收的是圖標的ID,而lParam吸收的是鼠標的行動  
  if (wParam!=IDI_ICON_NOTIFY)
  {
    return 1;
  }
  switch(lParam)
  {
  case WM_RBUTTONUP:
    {
      CMenu Menu;
      Menu.CreatePopupMenu();
      Menu.AppendMenu(MF_STRING, ID_SHOW,"顯示");
      Menu.AppendMenu(MF_STRING, ID_HIDE,"隱蔽");  
      Menu.AppendMenu(MF_STRING, ID_EXIT,"加入");  
      CPoint Pt;
      GetCursorPos(&Pt);     //獲得鼠標地位
      Menu.TrackPopupMenu(TPM_RIGHTBUTTON, Pt.x, Pt.y, this);
      Menu.DestroyMenu();    //燒毀資本
      break;
    }
  }
  return 0;
}
 
void CServerDlg::OnDestroy() 
{
  CDialog::OnDestroy();
  Shell_NotifyIcon(NIM_DELETE,&m_nid);
}
 
void CServerDlg::OnDlgExit()
{
  SendMessage(WM_CLOSE,NULL,NULL);
}
 
void CServerDlg::OnDlgHide()
{
  ShowWindow(SW_HIDE);
}
 
void CServerDlg::OnDlgShow()
{
  ::ShowWindow(this->m_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;i<MAX;i++)
  {
    if (m_sClient[i]==wParam)
    {
      break;
    }
  }
  if (i==MAX) 
  {
    return;
  }
  int iRet = recv(m_sClient[i],(char*)&Data,sizeof(Data),0);
  //0    關羽   
  //新聞類型 用戶稱號 信息內容
  //剖析數據包
  switch(Data.iType)
  {
  case 20:
  case 6:
    {
      //6 11 對方已接收要求,樹立銜接.......
 
      int j = 0;
      for (j=0;j<MAX;j++)
      {
        if (m_sClient[j]!=INVALID_SOCKET&&
          strcmp(m_PeopleInfor[j].strName.LockBuffer(),Data.szName)==0)
        {
          send(m_sClient[j],(char*)&Data,sizeof(Data),0);
        }
      }
      break;
    }
  case 7:
    {
      int j = 0;
      for (j=0;j<MAX;j++)
      {
        if (m_sClient[j]!=INVALID_SOCKET&&
          strcmp(m_PeopleInfor[j].strName.LockBuffer(),Data.szName)==0)
        {
          send(m_sClient[j],(char*)&Data,sizeof(Data),0);
        }
      }
      break;
    }
  case 5:  //11 -12  
    {
      //11--->12
      //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<MAX;j++)
      {
        if (m_sClient[j]!=INVALID_SOCKET&&
          strcmp(m_PeopleInfor[j].strName.LockBuffer(),Data.szName)==0)
        {
          send(m_sClient[j],(char*)&Data,sizeof(Data),0);
        }
      }
 
      break;
    }
 
  case 120:
    {
      int j = 0;
      for (j=0;j<MAX;j++)
      {
        if (m_sClient[j]!=INVALID_SOCKET&&
          strcmp(m_PeopleInfor[j].strName.LockBuffer(),Data.szName)==0)
        {
          send(m_sClient[j],(char*)&Data,sizeof(Data),0);
        }
      }
      break;
    }
  case 110:
    {
      int j = 0;
      for (j=0;j<MAX;j++)
      {
        if (m_sClient[j]!=INVALID_SOCKET&&j!=i)
        {
          send(m_sClient[j],(char*)&Data,sizeof(Data),0);
        }
      }
      break;
    }
  case 2:
    {
      int j = 0;
      for (j=0;j<MAX;j++)
      {
        if (strcmp(m_PeopleInfor[j].strName,Data.szName)==0
          &&m_sClient[j]!=INVALID_SOCKET)
        {
          send(m_sClient[j],(char*)&Data,sizeof(Data),0);
          break;
        }
      }
      break;
    }
  case 1:  
    {
      int j = 0;
      for (j=0;j<MAX;j++)
      {
        if (m_sClient[j]!=INVALID_SOCKET&&j!=i)
        {
          send(m_sClient[j],(char*)&Data,sizeof(Data),0);
        }
      }
      break;
    }
  case 0:  //New People
    {
      m_PeopleInfor[i].strName = Data.szName;
      //向控件添加數據
      OnlineOrOutline(i,"上線");
      //刷新用戶鏈表
      //1向老的告訴新的用戶(本身獲得本身)
      int j = 0;
      for (j=0;j<MAX;j++)
      {
        if (m_sClient[j]!=INVALID_SOCKET)
        {
          CString Temp;
          Data.iType = 0;
          strcpy(Data.szName,m_PeopleInfor[i].strName); 
          Temp +="\r\n";
          GetCurrentTime();
          Temp+=m_Timer.LockBuffer();
          Temp+="體系新聞:\r\n ";
          Temp+=m_PeopleInfor[i].strName;
          Temp+="進入聊天室";
          strcpy(Data.szMsg,Temp.LockBuffer());
          send(m_sClient[j],(char*)&Data,sizeof(Data),0);
        }
      }
      //向新的告訴老的用戶
      j = 0;
      for (j=0;j<MAX;j++)
      {
        if (m_sClient[j]!=INVALID_SOCKET && j!=i)
        {
          Data.iType = 0;
          memset(Data.szMsg,0,400);
          strcpy(Data.szName,m_PeopleInfor[j].strName); 
          send(m_sClient[i],(char*)&Data,sizeof(Data),0);
        }
      }
      break;
    }
  }
}
 
 
VOID CServerDlg::OnAccept()
{
  int i = 0;
  for (i=0;i<MAX;i++)
  {
    if (m_sClient[i]==INVALID_SOCKET)
    {
      break;
    }
  }
  if (i==MAX)
  {
    return;
  }
  sockaddr_in ClientAddr;
  int nLen = sizeof(sockaddr_in);
  m_sClient[i] = accept(m_sListen,(sockaddr*)&ClientAddr,&nLen);
  LPCSTR strIP = inet_ntoa(*(struct in_addr*)&ClientAddr.sin_addr);
  m_PeopleInfor[i].strIp = strIP;
  m_People++;
  CString strPeople;
  strPeople.Format("%d",m_People);
  m_strPeople.SetWindowText(strPeople);
  UpdateData(FALSE);
}
 
VOID CServerDlg::GetCurrentTime()
{
  SYSTEMTIME st = {0};
  GetLocalTime(&st); 
  int j = 0;
  j = sprintf(m_Timer.LockBuffer(),"%d年",st.wYear);
  j += sprintf(m_Timer.LockBuffer()+j,"%d月",st.wMonth);
  j += sprintf(m_Timer.LockBuffer()+j,"%d日",st.wDay);
  j += sprintf(m_Timer.LockBuffer()+j,"%d時",st.wHour);
  if (st.wMinute<10) 
  {
    int i = 0;
    j += sprintf(m_Timer.LockBuffer()+j,"%d",i); 
  }
  j += sprintf(m_Timer.LockBuffer()+j,"%d分鐘",st.wMinute);
  if (st.wSecond<10) 
  {
    int i = 0;
    j += sprintf(m_Timer.LockBuffer()+j,"%d",i); 
  }
  j += sprintf(m_Timer.LockBuffer()+j,"%d秒",st.wSecond);
}
 
VOID CServerDlg::OnlineOrOutline(int iIndex,CString YesOrNo)
{
  CString strTemp;
  GetCurrentTime();
  strTemp+=m_Timer.LockBuffer();
  strTemp+=' ';
  strTemp+="用戶:";
  strTemp+= m_PeopleInfor[iIndex].strName;
  strTemp+=" ";
  strTemp+=m_PeopleInfor[iIndex].strIp;
  strTemp+= YesOrNo;
  m_strShow +="\r\n";
  m_strShow += strTemp;
  UpdateData(FALSE);
}
 
VOID CServerDlg::OnClose(WPARAM wParam)
{
  int i = 0;
  for (i=0;i<MAX;i++)
  {
    if (m_sClient[i]==wParam)
    {
      break;
    }
  }
  if (i==MAX)
  {
    return;
  }
  OnlineOrOutline(i,"下線");
  m_People--;
  CString strPeople;
  strPeople.Format("%d",m_People);
  m_strPeople.SetWindowText(strPeople);
  SENDINFOR Data;
  int j = 0;
  for (j=0;j<MAX;j++)
  {
    if (m_sClient[j]!=INVALID_SOCKET&&j!=i)
    {
      Data.iType = -1;
      CString strMsg;
      GetCurrentTime();
      strMsg+=m_Timer.LockBuffer();
      strMsg+=m_PeopleInfor[i].strName;
      strMsg+="加入了該聊天室"; 
      strcpy(Data.szName,m_PeopleInfor[i].strName.LockBuffer());
      strcpy(Data.szMsg,strMsg.LockBuffer()); 
      send(m_sClient[j],(char*)&Data,sizeof(Data),0);
    }
  }
  //清空加入用戶的資本
  closesocket(m_sClient[i]);
  m_sClient[i] = INVALID_SOCKET;
  m_PeopleInfor[i].strIp = "";
  m_PeopleInfor[i].strName = "";
}
 
 
 
 
void CServerDlg::OnStnClickedStaticHost()
{
  // TODO: 在此添加控件告訴處置法式代碼
}

以上就是本文的全體內容,願望對年夜家的進修有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved