📄 chatsrvdlg.cpp
字号:
// ChatSrvDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ChatSrv.h"
#include "ChatSrvDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
UINT recvthread(LPVOID lpparam)
{
CChatSrvDlg *pdlg=(CChatSrvDlg*)AfxGetApp()->GetMainWnd();
SOCKET sock=pdlg->m_nsocket;
sockaddr_in addsrv,addcli;
int num=0;
int messnum=0;
CString temp="";
CString name="";
CString mess="";
addsrv=pdlg->m_addsrv;
int err=0;
int len=sizeof(SOCKADDR);
char recvbuff[200];
memset(recvbuff,0,200);
while(1)
{
// UpdateData(FALSE);
if(pdlg->m_ctrrecv.GetLineCount()>50)
{
pdlg->m_nstrrecv.Empty();
// m_ReceiveData="***The Length of the Text is too long, Emptied Automaticly!!!***\r\n";
// UpdateData(FALSE);
}
err=recvfrom(sock,recvbuff,200,0,(SOCKADDR *)&addcli,&len);
if(SOCKET_ERROR ==err)
{
MessageBox(NULL,"接收信息失败",0,0);
}
temp.Format("%s",recvbuff);
num=temp.Find("!!!");
name=temp.Left(num+1);
mess=temp.Right(temp.GetLength()-num-4);
messnum=pdlg->m_ctrrecv.GetWindowTextLength();
pdlg->m_ctrrecv.SetSel(messnum,messnum);
// pdlg->m_nstrrecv+=name+"对你说"+mess+"\r\n";
pdlg->m_ctrrecv.ReplaceSel(pdlg->m_nstrrecv);
// UpdateData(TRUE);
pdlg->m_nstrrecv.Empty();
}
return 0;
}
class 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()
/////////////////////////////////////////////////////////////////////////////
// CChatSrvDlg dialog
CChatSrvDlg::CChatSrvDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChatSrvDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChatSrvDlg)
m_nstrsend = _T("");
m_nstrrecv = _T("");
m_nstrport = _T("");
m_nstrname = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChatSrvDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChatSrvDlg)
DDX_Control(pDX, IDC_EDIT_RECVMESS, m_ctrrecv);
DDX_Control(pDX, IDC_LIST2, m_ctrlist);
DDX_Control(pDX, IDC_IPADDRESS1, m_nctrip);
DDX_Text(pDX, IDC_EDIT_SENDMESS, m_nstrsend);
DDX_Text(pDX, IDC_EDIT_RECVMESS, m_nstrrecv);
DDX_Text(pDX, IDC_EDIT_PORT, m_nstrport);
DDX_Text(pDX, IDC_EDIT_NAME, m_nstrname);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChatSrvDlg, CDialog)
//{{AFX_MSG_MAP(CChatSrvDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_START, OnBtnStart)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatSrvDlg message handlers
BOOL CChatSrvDlg::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
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return FALSE;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return FALSE;
}
memset(m_nuser,0,sizeof(m_nuser));
// memset(m_addcli,0,sizeof(m_addcli));
GetDlgItem(IDC_EDIT_PROTOL)->SetWindowText("UDP协议");
((CButton*)GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);
m_nstrname="";
// ::ZeroMemory(m_nuser, 0, sizeof(m_nuser));当成员是CString时,没有出错
return TRUE; // return TRUE unless you set the focus to a control
}
void CChatSrvDlg::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 CChatSrvDlg::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 CChatSrvDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChatSrvDlg::OnBtnStart()
{
// TODO: Add your control notification handler code here
m_nsocket=socket(AF_INET,SOCK_DGRAM,0);
// m_sendsocket=socket(AF_INET,SOCK_DGRAM,0);
m_addsrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
m_addsrv.sin_family=AF_INET;
if(((CButton*)GetDlgItem(IDC_RADIO2))->GetCheck())
{
UpdateData(TRUE);
m_addsrv.sin_port=htons(atoi((LPCSTR)m_nstrport));
}
else
m_addsrv.sin_port=htons(60000);
GetDlgItem(IDC_EDIT_NAME)->GetWindowText(m_nstrname);
if(""==m_nstrname)
{
MessageBox("输入昵称");
}
else
if(SOCKET_ERROR ==bind(m_nsocket,(SOCKADDR*)&m_addsrv,sizeof(SOCKADDR)))
{
MessageBox("绑定套接字失败");
closesocket(m_nsocket);
}
AfxBeginThread(&recvthread,0);
}
void CChatSrvDlg::OnRadio2()
{
// TODO: Add your control notification handler code here
((CButton*)GetDlgItem(IDC_RADIO1))->SetCheck(FALSE);
}
void CChatSrvDlg::OnRadio1()
{
// TODO: Add your control notification handler code here
((CButton*)GetDlgItem(IDC_RADIO2))->SetCheck(FALSE);
m_addsrv.sin_port=htons(60000);
GetDlgItem(IDC_EDIT_PORT)->SetWindowText("");
}
void CChatSrvDlg::OnBtnSend()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
int err=0;
CString temp="";
CString strIp="";
DWORD dwIp=0;
GetDlgItem(IDC_EDIT_SENDMESS)->GetWindowText(m_nstrsend);
if(!m_nstrsend.GetLength())
MessageBox("不允许发空白消息");
else
GetDlgItem(IDC_IPADDRESS1)->GetWindowText(strIp);
int ip=atoi(strIp);
// m_nctrip.GetAddress(dwIp);
// strIp.Format("%s",dwIp);
if(!ip)
{
MessageBox("请输入IP地址");
}
else
temp=m_nstrname;
temp+="!!!"+m_nstrsend;
m_addcli.sin_addr.S_un.S_addr=inet_addr(strIp);
m_addcli.sin_family=AF_INET;
m_addcli.sin_port=htons(60000);
err=sendto(m_nsocket,temp,temp.GetLength(),0,
(SOCKADDR*)&m_addcli,sizeof(SOCKADDR));
// MessageBox(temp);
if(SOCKET_ERROR ==err)
{
MessageBox("发送失败");
}
// m_nstrsend.Empty();
temp.Empty();
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -