64,649
社区成员
发帖
与我相关
我的任务
分享
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;
}