我用的是UDP,
定义了类class CMySocket : public CAsyncSocket 并且重载了OnReceive(),
初始化Socket
char aHostName[128];
char IP[20];
CString name;
if(::gethostname(aHostName,128)!=SOCKET_ERROR)
{
hostent* struct_hostent;
struct_hostent=::gethostbyname(aHostName);
if(struct_hostent!=NULL)
name=::inet_ntoa(*(struct in_addr*)struct_hostent->h_addr_list[0]);
}
strcpy(IP,name.GetBuffer(name.GetLength()));
CString IPAddress = (LPCTSTR)IP;
m_SocketRecv.Create(2416,SOCK_DGRAM,NULL);
m_SocketRecv.Bind(2416,IPAddress);
我还需要做什么,才能让OnReceive()函数被执行。
定义了类class CMySocket : public CAsyncSocket 并且重载了OnReceive(),
初始化Socket
char aHostName[128];
char IP[20];
CString name;
if(::gethostname(aHostName,128)!=SOCKET_ERROR)
{
hostent* struct_hostent;
struct_hostent=::gethostbyname(aHostName);
if(struct_hostent!=NULL)
name=::inet_ntoa(*(struct in_addr*)struct_hostent->h_addr_list[0]);
}
strcpy(IP,name.GetBuffer(name.GetLength()));
CString IPAddress = (LPCTSTR)IP;
m_SocketRecv.Create(2416,SOCK_DGRAM,NULL);
m_SocketRecv.Bind(2416,IPAddress);
我还需要做什么,才能让OnReceive()函数被执行。
解决方案 »
- 如何修改spin control的背景色? MFC
- #include 路径的问题
- 菜鸟问题:使用GDI绘完图之后,删除了笔和刷后,还需要ReleaseDC吗?
- 为什么一定要在公司域里才能访问公司数据库?
- 如何保持窗口画了一条线切换到别的窗口再切换回来的时候刚刚画的线依然存在?
- 请问如何在一个Dialog(或窗口)中打开一个新的View?
- 关于GUID的生成和MIDL的使用
- 用VC++做的组件,如何制作Cab包?
- 我想用VC++6.0开发一个批量打印autocad图纸文件的应用
- 微软的技术专家看过来
- ATL 框架中的 一个小问题, 找不到定义
- 请问PostQueueCompletionStatus从多个方面考虑,它的好处是什么?
事件通知,例如:
iRet=WSAAsyncSelect(m_RecvSock,m_hWnd,WM_ON_RECV,FD_READ);
其中m_hWnd是处理WM_ON_RECV消息的窗口句柄,WM_ON_RECV是自定义消息类型
#define WM_ON_RECV WM_USER + 0x100
//mainfrm.cpp在MainFrm的消息映射宏中加上,假设OnRecv是WM_ON_RECV 消息处理函数
m_hWnd = CMainFrame::GetSafeHwnd(),
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_ON_RECV,OnRecv)
END_MESSAGE_MAP()
//mainfrm.h
class CMainFrame : public CFrameWnd
{ 略……
// Generated message map functions
protected:
//{{AFX_MSG(CMainFrame)
//}}AFX_MSG
afx_msg LRESULT OnRecv(DWORD wParam,DWORD lParam);//加上它
DECLARE_MESSAGE_MAP()
};
http://www.vckbase.com/document/viewdoc/?id=440
udp需要触发