// WinSockUDPDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WinSockUDP.h"
#include "WinSockUDPDlg.h"
#include "Help.h"
//#include "AboutDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CWinSockUDPDlg dialog
CWinSockUDPDlg::CWinSockUDPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWinSockUDPDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWinSockUDPDlg)
m_strChat = _T("");
m_strData = _T("");
m_iLPort = 1001;
m_strName = _T("");
m_iSPort = 1001;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWinSockUDPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWinSockUDPDlg)
DDX_Control(pDX, IDC_Exit, m_btnExit);
DDX_Control(pDX, IDC_ServPort, m_ctlSPort);
DDX_Control(pDX, IDC_ServName, m_ctlName);
DDX_Control(pDX, IDC_Send, m_btnSend);
DDX_Control(pDX, IDC_Open, m_btnOpen);
DDX_Control(pDX, IDC_LocalPort, m_ctlLPort);
DDX_Control(pDX, IDC_DisConn, m_btnDisConn);
DDX_Control(pDX, IDC_Data, m_ctlData);
DDX_Control(pDX, IDC_Clear, m_btnClear);
DDX_Control(pDX, IDC_Chat, m_lstChat);
DDX_LBString(pDX, IDC_Chat, m_strChat);
DDX_Text(pDX, IDC_Data, m_strData);
DDX_Text(pDX, IDC_LocalPort, m_iLPort);
DDX_Text(pDX, IDC_ServName, m_strName);
DDX_Text(pDX, IDC_ServPort, m_iSPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWinSockUDPDlg, CDialog)
//{{AFX_MSG_MAP(CWinSockUDPDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_Clear, OnClear)
ON_BN_CLICKED(IDC_Open, OnOpenPort)
ON_BN_CLICKED(IDC_DisConn, OnDisConn)
ON_BN_CLICKED(IDC_Send, OnMsgSend)
ON_BN_CLICKED(IDC_Exit, OnExit)
ON_BN_CLICKED(IDC_HELP_INFORMATION, OnHelpInformation)
ON_BN_CLICKED(IDC_SOFT_INFORMATION, OnSoftInformation)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWinSockUDPDlg message handlers
BOOL CWinSockUDPDlg::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
//设置自己派生的Socket类与对话框相关联
m_uSocket.SetParent(this);
//刚启动的时候,设置发送信息的按钮不可用
m_btnSend.EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CWinSockUDPDlg::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 CWinSockUDPDlg::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 CWinSockUDPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWinSockUDPDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
void CWinSockUDPDlg::OnCancel()
{
// TODO: Add extra cleanup here
//CDialog::OnCancel();
}
void CWinSockUDPDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnCancel();
}
void CWinSockUDPDlg::OnReceive()
{
char *pBuf = new char[1025];
int iBufSize = 1024;
int iRecvd;
CString strRecvd;//接收的数据信息字符串
int iLen;
// 接收信息,并返回接收到的字节数
iRecvd = m_uSocket.Receive(pBuf, iBufSize);
// 判断是否有错误发生
if (iRecvd == SOCKET_ERROR)
{
}
else
{
// 利用NULL来截去接收缓存区中多余的字符
pBuf[iRecvd] = NULL;
// 将接收到的有用的信息赋值给CString变量
strRecvd = pBuf;
iLen = strRecvd.GetLength();
// 将接收到的信息加入到列表框中去,SendMessage将字符串添加到末尾
m_lstChat.SendMessage(LB_INSERTSTRING, -1, (LPARAM)(LPCTSTR(strRecvd)));
//将对应的变量的变化显示在控件上
UpdateData(FALSE);
delete pBuf;
}
}
void CWinSockUDPDlg::OnSocketClose()
{
m_uSocket.Close();
}
void CWinSockUDPDlg::OnClear()
{
// TODO: Add your control notification handler code here
//清空列表框内容
m_lstChat.SendMessage(LB_RESETCONTENT,0,0);
}
void CWinSockUDPDlg::OnOpenPort()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if (m_iLPort<=0)
{
MessageBox("请输入大于0的本地端口号!");
m_ctlLPort.SetFocus();
return;
}
m_uSocket.Create(m_iLPort, SOCK_DGRAM);
m_uSocket.Bind(m_iLPort);
//启动发送信息的按钮
m_btnSend.EnableWindow(true);
}
void CWinSockUDPDlg::OnDisConn()
{
// TODO: Add your control notification handler code here
m_uSocket.Close();
m_btnSend.EnableWindow(false);
}
void CWinSockUDPDlg::OnMsgSend()
{
// TODO: Add your control notification handler code here
int iLen; //发送字符串的长度
int iSend; //发送函数返回的结果
CString strIp; //IP地址
CString strName; //主机名
CString strRemoteName;
char *pName = new char[256];
struct hostent FAR *pHost, *pRhost;
char *addrIp ;
UpdateData(true);
//检查远程计算机名称和端口是否正确
if(m_iSPort<=0 || m_strName=="")
{
MessageBox("请输入正确的服务器名称和端口号!");
m_ctlName.SetFocus();
return;
}
//验证要发送的信息是否合法
if(""==m_strData)
{
MessageBox("不能发送空字符串!");
m_ctlData.SetFocus();
return;
}
//用来获取当前连接的Socket的本地主机名
gethostname(pName, 256);
strName = pName;
//下面三条语句用来获取当前连接的Socket的本地IP地址
pHost = gethostbyname(strName);
addrIp = inet_ntoa(*((in_addr *)pHost->h_addr));