用MFC编的程序:发送广播只能在本机接收,在同局域网内的机器收不到

tsair 2009-03-07 02:32:56
如题:本人程序要实现的功能是广播自己的主机名和ip地址(UDP广播)
问题:可以在本机收到广播,但是其他的机器收不到,盼解
代码如下:
CUdpDlg::CUdpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUdpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUdpDlg)
m_ip = _T("192.168.1.255");
m_sendport = 6000;
m_lsnport = 6000;//IP address for broadcasting , send port and listen port
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


BOOL CUdpDlg::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

UpdateData();
if (!m_socket.Create(0,SOCK_DGRAM))
{
AfxMessageBox("初始发送化套结字失败");
exit(0);
}

UpdateData();
if (!m_rsocket.Create(m_lsnport,SOCK_DGRAM))
{
AfxMessageBox("初始化监听套结字失败");
exit(0);
}
Buffer=GetLocalHost();
///////////////////////////////////////////

//AfxMessageBox(Buffer1);
//Buffer="I love you!";
//Buffer=GetLocalHost();
AfxMessageBox(Buffer);
////////////////////////////////////////////

return TRUE; // return TRUE unless you set the focus to a control
}

void CUdpDlg::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 CUdpDlg::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 CUdpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

#define RECV_BUF_LEN 100
void CUdpDlg::MsgRecv(){
CString recvip="";
UINT recvport ;
char RecvBuf[RECV_BUF_LEN];
CString showmsg;
// AfxMessageBox("就差一点点");
m_rsocket.ReceiveFrom(RecvBuf,RECV_BUF_LEN,recvip,recvport);
showmsg.Format("%s\r\n 发送端IP:%s 端口:%d\r\n",RecvBuf,recvip,recvport);
SetDlgItemText(IDC_SHOW,showmsg);
}

/*
*Click the send button to send localhost information
*/
void CUdpDlg::OnButtonSend()
{
UpdateData();
CString SendBuf;
SendBuf=Buffer;
//GetDlgItemText(IDC_EDITSEND,SendBuf);
m_socket.SendTo(SendBuf,SendBuf.GetLength()+1,m_sendport,m_ip);
SetDlgItemText(IDC_EDITSEND,"");
}

/*
*Get the ip address and hostname of localhost
*/
CString CUdpDlg::GetLocalHost()
{
//CString Buff;
char LocalHostName[50];
gethostname(LocalHostName,sizeof(LocalHostName));
//AfxMessageBox(LocalHostName);
hostent* hn;
hn=gethostbyname(LocalHostName);
CString Buffer1=inet_ntoa(*(struct in_addr *)hn->h_addr_list[0]);
CString Buffer2=Buffer1+" "+CString(LocalHostName);
//Buffer1=CString(LocalHostName);
//Buffer=GetLocalHost();
//AfxMessageBox(Buffer1);
//Buffer="I love you!";
//Buff=;
//return strIpAddr;
return Buffer2;
}
...全文
183 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjkkal 2009-03-18
  • 打赏
  • 举报
回复
CUdpDlg::CUdpDlg(CWnd* pParent /*=NULL*/) 
: CDialog(CUdpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUdpDlg)
m_ip = _T("192.168.1.255");
m_sendport = 6000;
m_lsnport = 6000;//IP address for broadcasting , send port and listen port
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


BOOL CUdpDlg::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

UpdateData();
if (!m_socket.Create(0,SOCK_DGRAM))
{
AfxMessageBox("初始发送化套结字失败");
exit(0);
}

UpdateData();
if (!m_rsocket.Create(m_lsnport,SOCK_DGRAM))
{
AfxMessageBox("初始化监听套结字失败");
exit(0);
}
Buffer=GetLocalHost();
///////////////////////////////////////////

//AfxMessageBox(Buffer1);
//Buffer="I love you!";
//Buffer=GetLocalHost();
AfxMessageBox(Buffer);
////////////////////////////////////////////

return TRUE; // return TRUE unless you set the focus to a control
}

void CUdpDlg::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 CUdpDlg::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 CUdpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

#define RECV_BUF_LEN 100
void CUdpDlg::MsgRecv(){
CString recvip="";
UINT recvport ;
char RecvBuf[RECV_BUF_LEN];
CString showmsg;
// AfxMessageBox("就差一点点");
m_rsocket.ReceiveFrom(RecvBuf,RECV_BUF_LEN,recvip,recvport);
showmsg.Format("%s\r\n 发送端IP:%s 端口:%d\r\n",RecvBuf,recvip,recvport);
SetDlgItemText(IDC_SHOW,showmsg);
}

/*
*Click the send button to send localhost information
*/
void CUdpDlg::OnButtonSend()
{
UpdateData();
CString SendBuf;
SendBuf=Buffer;
//GetDlgItemText(IDC_EDITSEND,SendBuf);
m_socket.SendTo(SendBuf,SendBuf.GetLength()+1,m_sendport,m_ip);
SetDlgItemText(IDC_EDITSEND,"");
}

/*
*Get the ip address and hostname of localhost
*/
CString CUdpDlg::GetLocalHost()
{
//CString Buff;
char LocalHostName[50];
gethostname(LocalHostName,sizeof(LocalHostName));
//AfxMessageBox(LocalHostName);
hostent* hn;
hn=gethostbyname(LocalHostName);
CString Buffer1=inet_ntoa(*(struct in_addr *)hn->h_addr_list[0]);
CString Buffer2=Buffer1+" "+CString(LocalHostName);
//Buffer1=CString(LocalHostName);
//Buffer=GetLocalHost();
//AfxMessageBox(Buffer1);
//Buffer="I love you!";
//Buff=;
//return strIpAddr;
return Buffer2;
}
zhangziliang04 2009-03-18
  • 打赏
  • 举报
回复
m_socket.SendTo(SendBuf,SendBuf.GetLength()+1,m_sendport,m_ip);
你在发送的时候,已经设定了广播的范围,是你自己的IP,所以其他机器收不到。你应该把m_ip指定为INADDR_BROADCAST。
tsair 2009-03-07
  • 打赏
  • 举报
回复
高手周末都休息啊?
tsair 2009-03-07
  • 打赏
  • 举报
回复
在线等了。。。
tsair 2009-03-07
  • 打赏
  • 举报
回复
那位帅哥指点一下,这个程序就折腾了半天
  • 打赏
  • 举报
回复
up过
tsair 2009-03-07
  • 打赏
  • 举报
回复
自己先顶了。。。。。
简介DbgView调试信息查看器。这是一款软件开发者常用的软件,往往一款软件在Debug下调试好了,可是在Release下可能还存在Bug,如何才能输出调试信息呢,全能调试器是解决您的问题的好帮手。DbgView调试信息查看器不仅仅能作为调试信息输出,而且可以作为一个信息输出软件,监视您的软件运行,目前支持mfc和windows下dos信息输出。在安装程序中附带有vc6的例子。 v1.7.0.206> 1、增加滚屏功能。 2、增加能够粘贴消息功能。v1.7.0.157 1、为方便使用,制作安装版。 2、过滤信息保存功能。 3、作为一个共享软件,增加注册功能,您可以免费使用30天,没有功能限制。 4、安装目录下有使用例子,方便您快速集成到您的应用中来,支持mfc和控制台程序调试信息输入。v1.6.0.93 1、增加中英文版 2、增加网络广播调试信息接收发送功能,支持局域网调试信息。v1.3.0.52 1、增加调试信息保存到文件功能。v1.2.1.1 1、增加过滤掉某些包含的字符串功能v1.1.0.1 1、如何集成到程序中 只需将原来的函数替换即可。 2、如何过滤。 过滤分为显示包含某些字符串的消息、过滤掉某些包含字符串的消息。 目前只实现了显示包含某些字符串的调试消息。 使用如例:如显示所有包含Hello的调试消息,只需在过滤器的包含中设置字符串Helllo;即可。 如显示所有包含Hello或haha的调试消息,只需设置如下Hello;haha;即可

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧