📄 netcomputedlg.cpp
字号:
// NetComputeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "NetCompute.h"
#include "NetComputeDlg.h"
const unsigned port = 34567;
#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()
/////////////////////////////////////////////////////////////////////////////
// CNetComputeDlg dialog
CNetComputeDlg::CNetComputeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNetComputeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNetComputeDlg)
m_dData2 = 0.0;
m_dData1 = 0.0;
m_serverType = 0;
m_protocolType = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strLocalName = _T("");
m_strLocalIP = _T("");
}
void CNetComputeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNetComputeDlg)
DDX_Control(pDX, IDC_CONTENT, m_lcContent);
DDX_Control(pDX, IDC_IPCTL, m_ipCtl);
DDX_Text(pDX, IDC_DATA2, m_dData2);
DDX_Text(pDX, IDC_DATA1, m_dData1);
DDX_Radio(pDX, IDC_SERVER, m_serverType);
DDX_Radio(pDX, IDC_TCP, m_protocolType);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNetComputeDlg, CDialog)
//{{AFX_MSG_MAP(CNetComputeDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_BN_CLICKED(IDC_TCP, OnTCP)
ON_BN_CLICKED(IDC_UDP, OnUDP)
ON_BN_CLICKED(IDC_SERVER, OnServer)
ON_BN_CLICKED(IDC_CLIENT, OnClient)
ON_BN_CLICKED(IDC_CLOSE, OnClose)
ON_MESSAGE(SERVER_TCP_EVENT, OnServerTCPEvent)
ON_MESSAGE(SERVER_UDP_EVENT, OnServerUDPEvent)
ON_MESSAGE(CLIENT_TCP_EVENT, OnClientTCPEvent)
ON_MESSAGE(CLIENT_UDP_EVENT, OnClientUDPEvent)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNetComputeDlg message handlers
BOOL CNetComputeDlg::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
GetLocalHostName(m_strLocalName);
SetWindowText(_T("TCP协议服务器端------你好!") + m_strLocalName);
GetIpAddress(m_strLocalName, m_strLocalIP);
m_ipCtl.SetWindowText(m_strLocalIP);
//
m_lcContent.InsertColumn(0, _T("")"", LVCFMT_LEFT, 1500);
//
GetDlgItem(IDC_CLOSE)->EnableWindow(FALSE);
GetDlgItem(IDC_DATA1)->EnableWindow(FALSE);
GetDlgItem(IDC_DATA2)->EnableWindow(FALSE);
GetDlgItem(IDC_SEND)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CNetComputeDlg::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 CNetComputeDlg::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 CNetComputeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/////////////////////////////////////////////////////////////
//启动网络服务,服务器端以及客户端
void CNetComputeDlg::OnStart()
{
// TODO: Add your control notification handler code here
UpdateData();
//服务器端
if (m_serverType == 0)
{
//TCP协议
if (m_protocolType == 0)
{
WSADATA wsaData;
BOOL ret = WSAStartup(MAKEWORD(2,1), &wsaData);
if (ret != 0)
{
m_lcContent.InsertItem(0,_T("初始化套接字失败!"));
return;
}
m_lcContent.InsertItem( 0, _T("成功初始化套接字!"));
m_serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (m_serverSocket == INVALID_SOCKET)
{
m_lcContent.InsertItem(0,_T("创建套接字失败!"));
closesocket(m_serverSocket);
WSACleanup();
return;
}
m_lcContent.InsertItem(0,_T("成功创建套接字!"));
sockaddr_in localaddr;
localaddr.sin_family = AF_INET;
localaddr.sin_port = htons(port);
CString str;
m_ipCtl.GetWindowText(str);
localaddr.sin_addr.s_addr = inet_addr(str);
if (bind(m_serverSocket, (const struct sockaddr*)&localaddr, sizeof(sockaddr)) == SOCKET_ERROR)
{
m_lcContent.InsertItem(0,_T("服务器端绑定地址失败!"));
closesocket(m_serverSocket);
WSACleanup();
return;
}
m_lcContent.InsertItem(0,_T("服务器端成功绑定地址!"));
//注册网络异步事件,m_hWnd为应用程序的主对话框或主窗口的句柄
WSAAsyncSelect(m_serverSocket, m_hWnd, SERVER_TCP_EVENT, FD_CLOSE | FD_READ | FD_ACCEPT);
//设置侦听模式
listen(m_serverSocket, 1);
m_lcContent.InsertItem( 0, _T("监听中。。。"));
}
//UDP协议
else
{
WSADATA wsaData;
BOOL ret = WSAStartup(MAKEWORD(2,1), &wsaData);
if (ret != 0)
{
m_lcContent.InsertItem(0,_T("初始化套接字失败!"));
return;
}
m_lcContent.InsertItem( 0, _T("成功初始化套接字!"));
m_serverSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (m_serverSocket == INVALID_SOCKET)
{
m_lcContent.InsertItem(0,_T("创建套接字失败!"));
closesocket(m_serverSocket);
WSACleanup();
return;
}
m_lcContent.InsertItem(0, _T("成功创建套接字!"));
m_serveraddr.sin_family = AF_INET;
m_serveraddr.sin_port = htons(port);
CString str;
m_ipCtl.GetWindowText(str);
m_serveraddr.sin_addr.s_addr = inet_addr(str);
//本地主机与本机IP相连
if (bind(m_serverSocket, (const sockaddr*)&m_serveraddr, sizeof(sockaddr)) == SOCKET_ERROR)
{
m_lcContent.InsertItem(0,_T("服务器端绑定地址失败!"));
closesocket(m_serverSocket);
WSACleanup();
return;
}
m_lcContent.InsertItem(0,_T("服务器端成功绑定地址!"));
//注册网络事件
WSAAsyncSelect(m_serverSocket, m_hWnd, SERVER_UDP_EVENT, FD_READ);
}
}
//客户端
else
{
//TCP协议
if (m_protocolType == 0)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,1),&wsaData))
{
WSACleanup();
m_lcContent.InsertItem(0,_T("初始化套接字失败!"));
return;
}
m_lcContent.InsertItem(0,_T("成功初始化套接字!"));
m_serverSocket = socket(AF_INET,SOCK_STREAM,0);
if(m_serverSocket == INVALID_SOCKET)
{
m_lcContent.InsertItem(0,_T("创建套接字失败!"));
return;
}
m_lcContent.InsertItem(0,_T("成功创建套接字!"));
CString str;
m_ipCtl.GetWindowText(str);
m_serveraddr.sin_family=AF_INET;
m_serveraddr.sin_port = htons(port);
m_serveraddr.sin_addr.S_un.S_addr = inet_addr(str);
if(connect(m_serverSocket,(const sockaddr*)&m_serveraddr,sizeof(m_serveraddr)) == SOCKET_ERROR)
{
m_lcContent.InsertItem(0,_T("连接服务器失败!"));
return;
}
m_lcContent.InsertItem(0,_T("成功连接服务器!"));
WSAAsyncSelect(m_serverSocket, m_hWnd, CLIENT_TCP_EVENT, FD_READ);
}
//UDP协议
else
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,1),&wsaData))
{
WSACleanup();
m_lcContent.InsertItem(0,_T("初始化套接字失败!"));
return;
}
m_lcContent.InsertItem(0,_T("成功初始化套接字!"));
m_serverSocket = socket(AF_INET,SOCK_DGRAM,0);
if(m_serverSocket == INVALID_SOCKET)
{
m_lcContent.InsertItem(0,_T("创建套接字失败!"));
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -