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
mysocket = socket(AF_INET,SOCK_DGRAM,0);
if (mysocket == INVALID_SOCKET)
{
MessageBox("MySocket failed");
return FALSE;
}
sockaddr_in socketinfo;
socketinfo.sin_family = AF_INET;
socketinfo.sin_port = htons(10000);
socketinfo.sin_addr.S_un.S_addr = htonl(INADDR_ANY);//指定接收对象IP
bind(mysocket,(sockaddr*)&socketinfo,sizeof(sockaddr_in));
CreateThread(NULL,0,ThreadRev,&mysocket,NULL,NULL);
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;
}
DWORD WINAPI ThreadRev(LPVOID lparam)
{
CUDPDlg *p = (CUDPDlg*)AfxGetApp()->GetMainWnd();
SOCKET *mysocket = (SOCKET*) lparam;
sockaddr_in socketinfo1;
int size = sizeof(sockaddr_in);
char *buf = new char[200];
CString recvip;
while (1)
{
memset(buf,0,200);
recvfrom(*mysocket,buf,200,0,(sockaddr*)&socketinfo1,&size);
recvip = inet_ntoa(socketinfo1.sin_addr);
p->m_edit_show += recvip + " to Me";
p->m_edit_show += ":";
p->m_edit_show += buf;
p->m_edit_show += "/r/n";
p->SetDlgItemText(IDC_EDIT1,p->m_edit_show);
}
return 0;
}
void CUDPDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData(TRUE);
sockaddr_in socketinfo2;
if (m_edit_ip == "")
{
MessageBox("Input IP");
return ;
}
socketinfo2.sin_family = AF_INET;
socketinfo2.sin_port = htons(10000);
socketinfo2.sin_addr.S_un.S_addr = inet_addr(m_edit_ip);//指定发送对象IP
if (m_edit_input == "")
{
MessageBox("Input message");
return ;
}
sendto(mysocket,m_edit_input,200,0,(sockaddr*)&socketinfo2,sizeof(sockaddr_in));
m_edit_show += "Me to " + m_edit_ip + ":" + m_edit_input + "/r/n";
m_edit_input = "";
UpdateData(FALSE);
}