// WinSockServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WinSockServer.h"
#include "WinSockServerDlg.h"
#include <winsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//CSocketListen *m_sAcceptSocket = new CSocketListen;
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()
/////////////////////////////////////////////////////////////////////////////
// CWinSockServerDlg dialog
CWinSockServerDlg::CWinSockServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWinSockServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWinSockServerDlg)
m_strChat = _T("");
m_iPort = 1001;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWinSockServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWinSockServerDlg)
DDX_Control(pDX, IDC_Exit, m_ctlExit);
DDX_Control(pDX, IDC_Chat, m_lstChat);
DDX_Control(pDX, IDC_End, m_ctlEnd);
DDX_Control(pDX, IDC_Clear, m_ctlClear);
DDX_Control(pDX, IDC_Start, m_btnStart);
DDX_Control(pDX, IDC_ServerPort, m_ctlPort);
DDX_LBString(pDX, IDC_Chat, m_strChat);
DDX_Text(pDX, IDC_ServerPort, m_iPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWinSockServerDlg, CDialog)
//{{AFX_MSG_MAP(CWinSockServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Start, OnStart)
ON_BN_CLICKED(IDC_End, OnEnd)
ON_BN_CLICKED(IDC_Exit, OnExit)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_Clear, OnClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWinSockServerDlg message handlers
BOOL CWinSockServerDlg::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
//设置窗体标题,
//AfxGetApp()->m_pMainWnd->SetWindowText();简写如下条语句。
SetWindowText("服务器端程序");
//将自定义的SocketListen类与对话框相关联
m_sListenSocket.SetParent(this);
//m_sAcceptSocket.SetParent(this);
//初始的时候,“结束服务”按钮不可用
m_ctlEnd.EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CWinSockServerDlg::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 CWinSockServerDlg::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 CWinSockServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWinSockServerDlg::OnOK()
{
// TODO: Add extra validation here
//屏蔽CDialog类的OnOk函数,不响应Enter按键
//CDialog::OnOK();
}
void CWinSockServerDlg::OnCancel()
{
// TODO: Add extra cleanup here
//屏蔽CDialog类的OnCancel函数,不响应Esc按键
//CDialog::OnCancel();
}
void CWinSockServerDlg::OnStart()
{
// TODO: Add your control notification handler code here
//获取端口号
UpdateData(true);
//服务开启后,端口号和该按钮禁用
m_ctlPort.EnableWindow(false);
m_btnStart.EnableWindow(false);
//“结束服务”按钮可用
m_ctlEnd.EnableWindow(true);
//创建服务器端套接字,Create函数的第二个关键字指明基于TCP协议
m_sListenSocket.Create(m_iPort, SOCK_STREAM);
m_sListenSocket.Listen();
}
void CWinSockServerDlg::OnEnd()
{
// TODO: Add your control notification handler code here
m_sListenSocket.Close();
int i;
for(i=0; i< m_sAcceptSocketA.GetCount(); i++)
{
m_sAcceptSocketA.GetAt(m_sAcceptSocketA.FindIndex(i))->Close();
}
//m_sAcceptSocket.Close();
//结束服务后,端口号和开启服务的按钮可用
m_ctlPort.EnableWindow(true);
m_btnStart.EnableWindow(true);
//“结束服务”按钮不可用
m_ctlEnd.EnableWindow(false);
}
void CWinSockServerDlg::OnExit()
{
// TODO: Add your control notification handler code here
OnEnd();
CDialog::OnOK();
}
void CWinSockServerDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
//在单击窗体上的关闭按钮的时候能够正确关闭窗体
OnEnd();
CDialog::OnCancel();
}
//与Socket类相关的事件函数
void CWinSockServerDlg::OnAccept()
{
int i;
i = m_sAcceptSocketA.GetCount();;
CSocketListen *m_sAcceptSocket = new CSocketListen;
m_sAcceptSocket->SetParent(this);
if(i < 5)
{
m_sAcceptSocketA.AddTail(m_sAcceptSocket);
i = m_sAcceptSocketA.GetCount();
}
else if(i >= 5 || i<=0)
{
AfxMessageBox("连接客户数太多了!");
delete m_sAcceptSocket;
return;
}
m_sListenSocket.Accept(*m_sAcceptSocketA.GetAt(m_sAcceptSocketA.FindIndex(i-1)));
}
void CWinSockServerDlg::OnSend()
{
}
void CWinSockServerDlg::OnSocketClose(CSocketListen *pSocket)
{
CString strSend;//发送的字符串
int iLen;
int iSend;
DWORD dwIp;//网络顺序形式的IP地址
CString strIp;//连接方IP地址
CString strName;//连接方主机名
char *pName = new char[256];
UINT iPort;
int iErr;
struct hostent FAR *pHost;
iErr = pSocket->GetPeerName(strIp, iPort);
//如果上条语句执行正确的话,执行下列语句
if(iErr != SOCKET_ERROR)
{
//取得网络顺序的IP值
dwIp = inet_addr(strIp);
//获取主机名称,注意其中第一个参数的转化
pHost = gethostbyaddr((LPSTR)&dwIp, 4, AF_INET);
memcpy(pName, pHost->h_name, 256);
strName = pName;
}
else
{
delete pName;
return;
}
strSend = strName + "["+ strIp+ "]: " + "已经退出本系统!";
iLen = strSend.GetLength();
m_sAcceptSocketA.RemoveAt(m_sAcceptSocketA.Find(pSocket));
pSocket->Close();
评论1