// 2010001Dlg.cpp : implementation file
//#include "stdafx.h"
#include "2010001.h"
#include "2010001Dlg.h"
#include <Winsock2.h>
#include <stdio.h>
#include "afxmt.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWinThread *m_pUDPListenThread;//线程变量
static CCriticalSection m_Mutex;//锁变量static UINT UDPServerThread(LPVOID lpVoid);//线程回调函数UINT UDPServerThread(LPVOID lpVoid)
{
while (1)
{
m_Mutex.Lock();//
//在此处添加代码实现将数据写入后台数据库并从后台数据库中调入所有信息显示到Listcontrol控件上
m_Mutex.Unlock();
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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()/////////////////////////////////////////////////////////////////////////////
// CMy2010001Dlg dialogCMy2010001Dlg::CMy2010001Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy2010001Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy2010001Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CMy2010001Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy2010001Dlg)
DDX_Control(pDX, IDC_LIST1, m_list);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CMy2010001Dlg, CDialog)
//{{AFX_MSG_MAP(CMy2010001Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMy2010001Dlg message handlersBOOL CMy2010001Dlg::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
m_list.InsertColumn(0,"IP Address",LVCFMT_CENTER,100);
m_list.InsertColumn(1,"Message",LVCFMT_CENTER,200);
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT |LVS_EX_GRIDLINES);
WORD wVersionRequested; WSADATA wsaData; wVersionRequested=MAKEWORD(2,2); if(WSAStartup(wVersionRequested,&wsaData)!=0)//初始化ws2_32.dll动态库
{
AfxMessageBox("error1"); exit(-1);
} if(wsaData.wVersion!=wVersionRequested)
{
AfxMessageBox("error2");
WSACleanup();//结束对ws2_32.dll的调用 exit(-2); }
//说明ws2_32.dll正确加载
// printf("Load ws2_32.dll successfully!\n"); //获取本机IP地址
char PCname[100]={""};
char *IPaddress=NULL;
gethostname(PCname,sizeof(PCname));
// printf("Local Hostname is %s.\n",PCname); struct hostent FAR * lpHostEnt=gethostbyname(PCname);
if(lpHostEnt==NULL)
{
//产生错误
// printf("gethostbyname failed!\n");// return;
}
//获取IP
LPSTR lpAddr=lpHostEnt->h_addr_list[0];
if(lpAddr)
{
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
//转换为标准格式
IPaddress=inet_ntoa(inAddr);//将一个32位数字表示的IP地址转换成点分十进制IP地址字符串
if(sizeof(IPaddress)==0)
AfxMessageBox("get host IP failed!");
else
{ CString str;
str.Format("本机IP地址:%s",IPaddress);
AfxMessageBox(str);
}
} //创建套接字
SOCKET servsock,clisock;
// printf("Create Socket...\n");
servsock=socket(AF_INET,SOCK_DGRAM,0);//数据报套接字 int servport=5555;
int iSockErr=0; //定义服务器地址结构
sockaddr_in udpaddr,cliaddr;
memset(&udpaddr,0,sizeof(udpaddr));
memset(&cliaddr,0,sizeof(cliaddr)); int clilen =sizeof(cliaddr);
udpaddr.sin_family=AF_INET;
udpaddr.sin_port=htons(servport);
//将一个点分十进制IP地址字符串转换成32位数字表示的IP地址
udpaddr.sin_addr.s_addr=inet_addr(IPaddress);//"127.0.0.1"//INADDR_ANY //绑定套接字到服务器地址结构
// printf("Binding...\n");
iSockErr=bind(servsock,(sockaddr *)&udpaddr,sizeof(udpaddr));
if (iSockErr==SOCKET_ERROR)
{
// printf("Binding failed:%d\n",WSAGetLastError());//根据不同的错误类型进行不同的处理
exit(-3);
}
AfxOleInit();//// 初始化COM,创建ADO连接等操作 // 先判断连接是否存在以及连接的状态,已经连接则返回
if((m_pConnection != NULL) && (m_pConnection->State != adStateClosed))
{
AfxMessageBox("请先断开正在连接的数据库");
}
CString strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=qq.mdb"; //创建Connection对象
HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection));
if( FAILED(hr) )
{
AfxMessageBox("创建Connect实例失败");
}
//设置等待时间
m_pConnection->put_ConnectionTimeout(long(5));
//用try...catch()来捕获错误信息。 try
{
m_pConnection-> Open(LPCSTR(strconn),"","",adConnectUnspecified);
}
catch(_com_error& e)
{
AfxMessageBox(e.ErrorMessage());
// return;
} AfxMessageBox("数据库qq.mdb连接成功!");
return TRUE; // return TRUE unless you set the focus to a control
}void CMy2010001Dlg::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 CMy2010001Dlg::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 CMy2010001Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CMy2010001Dlg::OnButton1()
{
// 开始UDP服务线程
m_pUDPListenThread = AfxBeginThread(UDPServerThread, (LPVOID) this);
if(!m_pUDPListenThread)
AfxMessageBox("UDP Listen Thread Failed!");
}void CMy2010001Dlg::OnButton2()
{
//关闭线程
if (m_pUDPListenThread)
TerminateThread(m_pUDPListenThread->m_hThread, -1);
}
说明:本人完全是菜鸟,哪位大神能帮帮我实现红体字部分的功能 ,最好有注释 谢谢了
//#include "stdafx.h"
#include "2010001.h"
#include "2010001Dlg.h"
#include <Winsock2.h>
#include <stdio.h>
#include "afxmt.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWinThread *m_pUDPListenThread;//线程变量
static CCriticalSection m_Mutex;//锁变量static UINT UDPServerThread(LPVOID lpVoid);//线程回调函数UINT UDPServerThread(LPVOID lpVoid)
{
while (1)
{
m_Mutex.Lock();//
//在此处添加代码实现将数据写入后台数据库并从后台数据库中调入所有信息显示到Listcontrol控件上
m_Mutex.Unlock();
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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()/////////////////////////////////////////////////////////////////////////////
// CMy2010001Dlg dialogCMy2010001Dlg::CMy2010001Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy2010001Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy2010001Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CMy2010001Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy2010001Dlg)
DDX_Control(pDX, IDC_LIST1, m_list);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CMy2010001Dlg, CDialog)
//{{AFX_MSG_MAP(CMy2010001Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMy2010001Dlg message handlersBOOL CMy2010001Dlg::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
m_list.InsertColumn(0,"IP Address",LVCFMT_CENTER,100);
m_list.InsertColumn(1,"Message",LVCFMT_CENTER,200);
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT |LVS_EX_GRIDLINES);
WORD wVersionRequested; WSADATA wsaData; wVersionRequested=MAKEWORD(2,2); if(WSAStartup(wVersionRequested,&wsaData)!=0)//初始化ws2_32.dll动态库
{
AfxMessageBox("error1"); exit(-1);
} if(wsaData.wVersion!=wVersionRequested)
{
AfxMessageBox("error2");
WSACleanup();//结束对ws2_32.dll的调用 exit(-2); }
//说明ws2_32.dll正确加载
// printf("Load ws2_32.dll successfully!\n"); //获取本机IP地址
char PCname[100]={""};
char *IPaddress=NULL;
gethostname(PCname,sizeof(PCname));
// printf("Local Hostname is %s.\n",PCname); struct hostent FAR * lpHostEnt=gethostbyname(PCname);
if(lpHostEnt==NULL)
{
//产生错误
// printf("gethostbyname failed!\n");// return;
}
//获取IP
LPSTR lpAddr=lpHostEnt->h_addr_list[0];
if(lpAddr)
{
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
//转换为标准格式
IPaddress=inet_ntoa(inAddr);//将一个32位数字表示的IP地址转换成点分十进制IP地址字符串
if(sizeof(IPaddress)==0)
AfxMessageBox("get host IP failed!");
else
{ CString str;
str.Format("本机IP地址:%s",IPaddress);
AfxMessageBox(str);
}
} //创建套接字
SOCKET servsock,clisock;
// printf("Create Socket...\n");
servsock=socket(AF_INET,SOCK_DGRAM,0);//数据报套接字 int servport=5555;
int iSockErr=0; //定义服务器地址结构
sockaddr_in udpaddr,cliaddr;
memset(&udpaddr,0,sizeof(udpaddr));
memset(&cliaddr,0,sizeof(cliaddr)); int clilen =sizeof(cliaddr);
udpaddr.sin_family=AF_INET;
udpaddr.sin_port=htons(servport);
//将一个点分十进制IP地址字符串转换成32位数字表示的IP地址
udpaddr.sin_addr.s_addr=inet_addr(IPaddress);//"127.0.0.1"//INADDR_ANY //绑定套接字到服务器地址结构
// printf("Binding...\n");
iSockErr=bind(servsock,(sockaddr *)&udpaddr,sizeof(udpaddr));
if (iSockErr==SOCKET_ERROR)
{
// printf("Binding failed:%d\n",WSAGetLastError());//根据不同的错误类型进行不同的处理
exit(-3);
}
AfxOleInit();//// 初始化COM,创建ADO连接等操作 // 先判断连接是否存在以及连接的状态,已经连接则返回
if((m_pConnection != NULL) && (m_pConnection->State != adStateClosed))
{
AfxMessageBox("请先断开正在连接的数据库");
}
CString strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=qq.mdb"; //创建Connection对象
HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection));
if( FAILED(hr) )
{
AfxMessageBox("创建Connect实例失败");
}
//设置等待时间
m_pConnection->put_ConnectionTimeout(long(5));
//用try...catch()来捕获错误信息。 try
{
m_pConnection-> Open(LPCSTR(strconn),"","",adConnectUnspecified);
}
catch(_com_error& e)
{
AfxMessageBox(e.ErrorMessage());
// return;
} AfxMessageBox("数据库qq.mdb连接成功!");
return TRUE; // return TRUE unless you set the focus to a control
}void CMy2010001Dlg::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 CMy2010001Dlg::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 CMy2010001Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CMy2010001Dlg::OnButton1()
{
// 开始UDP服务线程
m_pUDPListenThread = AfxBeginThread(UDPServerThread, (LPVOID) this);
if(!m_pUDPListenThread)
AfxMessageBox("UDP Listen Thread Failed!");
}void CMy2010001Dlg::OnButton2()
{
//关闭线程
if (m_pUDPListenThread)
TerminateThread(m_pUDPListenThread->m_hThread, -1);
}
说明:本人完全是菜鸟,哪位大神能帮帮我实现红体字部分的功能 ,最好有注释 谢谢了
解决方案 »
- 关于MAC地址的转换暑促问题
- 关于画弧算法的问题,请进!~
- 处理OpenOffice文档字符串的问题--查找字符失败????
- 如何构造数据包?
- 怎样将CString转换为const std::string &sFileName(加急)
- 请教各位大侠,如何在CDialogBar中的位图按钮上添加提示条?
- 一个关于数据转换的小问题!
- 经典书籍打包贱卖!
- MFC创建了一个对话框(有状态栏和编辑框) 如何在对话框底部的状态栏中显示编辑框中的行号和列号
- 关于ODBC与数据库交换数据时的数据类型的问题。
- vc用CAsyncSocket写得程序只能使用回路IP 127.0.0.1连接服务器,希望大家帮帮我!
- 关于最小化托盘图标NOTIFYICONDATAA?
m_DataGrid.SetRow(); DATAGRID 设置列
然后循环写入数据 m_DataGrid.UpData(); //刷新数据 写入到数据库
// 刷新数据我也没试过 你试下 听别人说可以进库。
之后读就
m_Rst->Open(sql,m_con,m_Con.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
if(!m_Rst.adoEof)
{
while(!m_rst.adoEof)
{
这里面取数据就行了。
}
}