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

📄 serverdlg.cpp

📁 这是一个服务器端的程序
💻 CPP
字号:
// 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

/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog
UINT RevMsgThread(LPVOID v);
CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CServerDlg)
	m_ip = _T("");
	m_port = 5000;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CServerDlg)
	DDX_Control(pDX, IDC_EDIT_REV_MSG, m_revmsg);
	DDX_Control(pDX, IDC_EDIT_SEND_MSG, m_sendmsg);
	DDX_Text(pDX, IDC_EDIT_IP, m_ip);
	DDX_Text(pDX, IDC_EDIT_PORT, m_port);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
	//{{AFX_MSG_MAP(CServerDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_CREATE, OnButtonCreate)
	ON_EN_CHANGE(IDC_EDIT_REV_MSG, OnChangeEditRevMsg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers

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

	// 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
  GetDlgItem(IDOK)->EnableWindow(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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::OnDestroy() 
{
  CDialog::OnDestroy();
  
  // TODO: Add your message handler code here
  WSACleanup();
}

void CServerDlg::OnOK() 
{
  // TODO: Add extra validation here
  
  CDialog::OnOK();
}

void CServerDlg::OnButtonSend() 
{
  // TODO: Add your control notification handler code here
  CString str;
  m_sendmsg.GetWindowText(str);
  send(m_sockrev,str,100,0);
  m_sendmsg.SetWindowText("");
}

UINT RevMsgThread(LPVOID v)
{
  CServerDlg *dlg=(CServerDlg*)AfxGetApp()->GetMainWnd();
  dlg->m_sockrev=accept(dlg->m_sockserver,(sockaddr*)&(dlg->m_sacserver),&(dlg->m_naddlen));
  while(dlg->m_sockrev==INVALID_SOCKET)
  {
    dlg->m_sendmsg.SetWindowText("Accept 失败");
    dlg->m_sockrev=accept(dlg->m_sockserver,(sockaddr*)&(dlg->m_sacserver),&(dlg->m_naddlen));
    //空循环
    for (int i=0;i<=1000;i++)
      for(int j=0;j<=400;j++);
  }
  int s=1;
  char buff[100];
  memset(buff,0,100); 
  while(s!=SOCKET_ERROR)
  {
    //循环接收数据
    s=recv(dlg->m_sockrev,buff,100,0);
    if (s!=SOCKET_ERROR)
      dlg->m_revmsg.SetWindowText(buff);
  }
  	closesocket(dlg->m_sockserver);

  return 0;
}



void CServerDlg::OnButtonCreate() 
{
	// TODO: Add your control notification handler code here
  
  GetDlgItem(IDC_BUTTON_CREATE)->EnableWindow(FALSE);
  GetDlgItem(IDOK)->EnableWindow(TRUE);	
  WORD VersionRequested = MAKEWORD(2,2);
  WSADATA wsaData;
  WSAStartup(VersionRequested, &wsaData);	// 启动winsock服务
  if(VersionRequested!=wsaData.wVersion)
  {
    AfxMessageBox("初始化 WS2_32.DLL 失败");
    
  }

  m_sacserver.sin_addr.s_addr=htonl(INADDR_ANY);
  m_sacserver.sin_family=AF_INET;
  m_sacserver.sin_port=5000;//htons(5000);
  m_naddlen=sizeof(m_sacserver);
  //创建socket
  m_sockserver=socket(AF_INET,SOCK_STREAM,0);
  if (bind(m_sockserver,(sockaddr*)&m_sacserver,m_naddlen))
  {
    m_revmsg.SetWindowText("绑定错误");
  }else
  {
    //m_list.InsertItem(count++,inet_ntoa(serv.sin_addr));
    m_revmsg.SetWindowText("服务器创建成功");
    //开始侦听	
    listen(m_sockserver,5);
    //调用线程
    AfxBeginThread(&RevMsgThread,0);    
  }
}

void CServerDlg::OnChangeEditRevMsg() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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