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

📄 serverdlg.cpp

📁 本系统用VC++ 6.0编译写成 系统主要包含局域网聊天和文件传输功能! 聊天可以实现屏蔽和私聊功能 屏蔽可以只接受发给自己的信息。 私聊时发送信息只让放送方知道。 界面可以最小化到托
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include "ClearDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog

CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CServerDlg)
	m_strMsg = _T("");
	//}}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_Text(pDX, IDC_EDIT4, m_strMsg);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
	//{{AFX_MSG_MAP(CServerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_MESSAGE(WM_SERVER_ACCEPT, OnServerAccept)
	ON_MESSAGE(WM_SHOWTASK,onShowTask)
	ON_BN_CLICKED(IDC_BUTTON_MIN, OnButtonMin)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
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
	// TODO: Add extra initialization here
	if(InitSocket()==false)
	{
		AfxMessageBox("请检查服务器是否已经打开");
		CServerDlg::OnCancel();
	}
	OnButtonMin();
	this->SetWindowText("QC服务器");
	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);
	}
}

// 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;
}


bool CServerDlg::InitSocket()
{
	WSADATA wsaData; 		//初始化TCP协议 
	BOOL ret = WSAStartup(MAKEWORD(2,2), &wsaData); 
	if(ret != 0) 
	{ 
		MessageBox("Initial TCP Protocol Failed!"); 
		return false; 
	} 
	for(int i=0;i<MAX1;i++)
	{
		SocketAccept[i]=INVALID_SOCKET;
	}
	ListeningSocket=socket(AF_INET,SOCK_STREAM,0);
	if(ListeningSocket==INVALID_SOCKET)
	{
		MessageBox("Create SOCKET Failed");
		WSACleanup(); 
		closesocket(ListeningSocket);
		return false;
	}
	serveraddr.sin_family=AF_INET;
	serveraddr.sin_addr.s_addr=0;//INADDR_ANY
	serveraddr.sin_port=htons(4009);
	WSAAsyncSelect(ListeningSocket,m_hWnd,WM_SERVER_ACCEPT,FD_ACCEPT|FD_READ|FD_CLOSE);
	/*设置为异步通讯模式,并为它注册各种网络异步事件,
	  其中m_hWnd 为应用程序的主对话框或主窗口的句柄
	  当检测到相应时间后才为窗口句柄发送消息*/
	if(bind(ListeningSocket,(sockaddr *)&serveraddr,sizeof(serveraddr))==SOCKET_ERROR)
	{
		MessageBox("Bind Failed");
		WSACleanup(); 
		closesocket(ListeningSocket);
		return false;
	}
	if(listen(ListeningSocket,20)==SOCKET_ERROR)
	{
		MessageBox("Listen Failed");
		WSACleanup(); 
		closesocket(ListeningSocket);
		return false;
	}
	HostInfo();
	m_strMsg="Now server is working!\r\nWaiting for client's connecting .......";
	UpdateData(false);
	return true;
}
LRESULT CServerDlg::OnServerAccept(WPARAM wParam,LPARAM lParam)
//wParam参数标识了网络事件发生的套接口.lParam的低字指明了发生的网络事件.
{
	int iEvent=WSAGETSELECTEVENT(lParam); //调用Winsock API函数,得到网络事件类型
//	SOCKET CurSock= (SOCKET)wParam;//调用Winsock API函数,得到发生此事件的客户端套接字 
	switch(iEvent) 
	{ 
		case FD_ACCEPT://客户端连接请求事件 
			OnAccept(wParam,lParam); 
			break; 
		case FD_CLOSE://客户端断开事件: 
			OnClose(wParam,lParam); 
			break; 
		case FD_READ://网络数据包到达事件 
			OnReceive(wParam,lParam); 
			break; 
		default: break; 
	} 
	return 0;
}

void CServerDlg::OnAccept(WPARAM wParam,LPARAM lParam)
{
	int i,addrlen;
	for(i=0;i<MAX1&&SocketAccept[i]!=INVALID_SOCKET;i++){}
	if(i==MAX1)      
		return ;
	sockaddr_in clientsocket;
	addrlen=sizeof(clientsocket);
	SocketAccept[i]=accept(ListeningSocket,(sockaddr *)&clientsocket,&addrlen);
	/*accept函数从处于监听状态的流套接字s的客户连接请求队列中取出排在最前的一个客户请求,并且创建一个新的套接字来与客户套接字创建连接通道,
	如果连接成功,就返回新创建的套接字的描述符,以后与客户套接字交换数据的是新创建的套接字;
	如果失败就返回 INVALID_SOCKET。该函数的第一个参数指定处于监听状态的流套接字;
	操作系统利用第二个参数来返回新创建的套接字的地址结构;
	操作系统利用第三个参数来返回新创建的套接字的地址结构的长度。*/
	if(i==MAX)
	{
		sendinfo err;
		err.type=4;//异常
		CString m;
		m="服务器已达到最大连接数";
		memcpy(err.msg,m,m.GetLength()+1);
		send(SocketAccept[i],(char*)&err,sizeof(err),0);
		Sleep(500);
		closesocket(SocketAccept[i]);
		SocketAccept[i]=INVALID_SOCKET;
		return;
	}
	uinfo[i].userip=inet_ntoa(clientsocket.sin_addr);//32位ip转字符型ip;
	people.Format("%d",atoi(people)+1);
	GetDlgItem(IDC_STATIC2)->SetWindowText(people);

⌨️ 快捷键说明

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