📄 chatdlg.cpp
字号:
// ChatDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Chat.h"
#include "ChatDlg.h"
#include "PortSet.h"
#include "FileTransfer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CChatDlg dialog
CChatDlg::CChatDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChatDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChatDlg)
m_theirport = 0;
m_infotosend = _T("");
m_file = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChatDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChatDlg)
DDX_Control(pDX, IDC_LIST1, m_List);
DDX_Control(pDX, IDC_IPADDRESS1, m_theirip);
DDX_Text(pDX, IDC_PORT, m_theirport);
DDV_MinMaxUInt(pDX, m_theirport, 0, 65535);
DDX_Text(pDX, IDC_INFOTOSEND, m_infotosend);
DDX_Text(pDX, IDC_FILE_OPEN, m_file);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
//{{AFX_MSG_MAP(CChatDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_BN_CLICKED(IDC_SHUTCLOSE, OnShutclose)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON2, OnChooseFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatDlg message handlers
BOOL CChatDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
//获取主机的IP地址信息
m_mysocket=socket(AF_INET,SOCK_STREAM,0);
if(m_mysocket==SOCKET_ERROR)
{
MessageBox("创建套接字失败!");
ExitProcess(0);
}
m_myipinfo.sin_family=AF_INET;
hostent *phost=NULL;
char name[128];
gethostname(name,128);
phost=gethostbyname(name);
if(!phost)
{
CString ss;
ss.Format("获得主机:%s的IP地址失败!",name);
MessageBox(ss.GetBuffer(ss.GetLength()),"应用程序提示");
}
m_myipinfo.sin_addr=*(in_addr*)phost->h_addr_list[0];
PortSet ps((in_addr)m_myipinfo.sin_addr,this);
ps.SetDlgItemText(IDC_STATIC_IP,inet_ntoa((in_addr)m_myipinfo.sin_addr));
int dw=ps.DoModal();
if(dw==IDOK)
{
m_myipinfo.sin_port=htons(ps.m_port);
}
m_mylisten=socket(AF_INET,SOCK_STREAM,0);
if(m_mylisten==SOCKET_ERROR)
{
MessageBox("创建套接字失败!");
ExitProcess(0);
}
dw=-1;
dw=bind(m_mylisten,(struct sockaddr*)&m_myipinfo,sizeof(sockaddr));
if(dw==SOCKET_ERROR)
{
MessageBox("IP地址和端口绑定套接字失败!");
ExitProcess(0);
}
m_workthread=CreateThread(NULL,0,RecvThreadFunc,this,0,&ThreadId);
GetDlgItem(IDC_SHUTCLOSE)->EnableWindow(false);
GetDlgItem(IDOK)->EnableWindow(false);
// GetDlgItem(IDC_PORT)->EnableWindow(false);
GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CChatDlg::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 CChatDlg::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 CChatDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChatDlg::OnOK()
{
//检查对方IP是否为空
//if(!CheckTheirIp())
// {
// AfxMessageBox(_TEXT("对方IP地址不能为空!"));
// return ;
// }
//检查对方端口号是否设置
// UpdateData();
// if(m_theirport==0)
// {
// AfxMessageBox(_TEXT("请确定对方端口号!"));
// return ;
// }
//检查发送文本是否为空
UpdateData();
if(m_infotosend.IsEmpty())
{
AfxMessageBox(_TEXT("发送的信息不能为空"));
return ;
}
//设置对方地址信息
sockaddr_in to;
to.sin_family=AF_INET;
m_theirip.GetAddress(to.sin_addr.S_un.S_addr);
to.sin_addr.S_un.S_addr=htonl(to.sin_addr.S_un.S_addr);
to.sin_port=htons(m_theirport);
sendto(s,m_infotosend.GetBuffer(0),m_infotosend.GetLength(),0,(struct sockaddr*)&to,sizeof(to));
m_infotosend.Empty();
UpdateData(FALSE);
::SetFocus(::GetDlgItem(m_hWnd,IDC_INFOTOSEND));
}
//BOOL CChatDlg::CheckTheirIp()
//{
// DWORD ipaddr=0;
// m_theirip.GetAddress(ipaddr);
//
// if(!ipaddr)
// {
// return false;
// }
// m_theirintip=ipaddr;
// return true;
//}
void CChatDlg::OnConnect()
{
SOCKADDR_IN addr={0};
UpdateData();
addr.sin_family=AF_INET;
if(m_theirport<1024||m_theirport>65535)
{
AfxMessageBox("端口号范围应设置在1024到65535之间!");
return;
}
addr.sin_port=htons(m_theirport);
m_theirip.GetAddress(addr.sin_addr.S_un.S_addr);
if (addr.sin_addr.S_un.S_addr==0)
{
AfxMessageBox("请输入对方的IP地址!");
return;
}
addr.sin_addr.S_un.S_addr=htonl(addr.sin_addr.S_un.S_addr);
CString ss;
ss.Format("Local IP:%s,端口:%d",inet_ntoa((in_addr)m_myipinfo.sin_addr),htons(m_myipinfo.sin_port));
AfxMessageBox(ss.GetBuffer(0));
if(connect(m_mysocket,(sockaddr*)&addr,sizeof(addr)))
{
AfxMessageBox("连接失败!");
return;
}
SuspendThread(m_workthread);
GetDlgItem(IDC_CONNECT)->EnableWindow(FALSE);
GetDlgItem(IDC_SHUTCLOSE)->EnableWindow(FALSE);
GetDlgItem(IDOK)->EnableWindow(TRUE);
GetDlgItem(IDC_FILE_SERACH)->EnableWindow(TRUE);
}
void CChatDlg::OnShutclose()
{
closesocket(m_mysocket);
}
void CChatDlg::OnClose()
{
int i=0;
if( (!PostThreadMessage(ThreadId,WM_USER,0,0)) && (i<5) )
{
Sleep(100);
}
DWORD dw=WaitForSingleObject(m_workthread,300);
if(dw!=WAIT_OBJECT_0)
TerminateThread(m_workthread,100);
closesocket(m_mysocket);
CDialog::OnClose();
}
void CChatDlg::OnChooseFile()
{
CFileDialog dlg(TRUE,"*.*","*.*",OFN_HIDEREADONLY,"All files(*.*)|*.*||");
if(dlg.DoModal()==IDOK)
{
m_file=dlg.GetPathName();
}
else return ;
// Invalidate();
UpdateData(FALSE);
AfxMessageBox("1");
CString filepath(m_file.GetBuffer(0));
sockaddr_in ipaddr={0};
ipaddr.sin_family=AF_INET;
m_theirip.GetAddress(ipaddr.sin_addr.S_un.S_addr);
ipaddr.sin_addr.S_un.S_addr=htonl(ipaddr.sin_addr.S_un.S_addr);
ipaddr.sin_port=htons(m_theirport);
AfxMessageBox("2");
FileTransfer fTdlg(filepath.GetBuffer(0),ipaddr,m_mysocket,NULL);
AfxMessageBox("3");
int dw=fTdlg.DoModal();
m_file=_T("");
UpdateData(FALSE);
if(dw==IDCANCEL)
{
ResumeThread(m_workthread);
GetDlgItem(IDC_FILE_SERACH)->EnableWindow(FALSE);
GetDlgItem(IDC_CONNECT)->EnableWindow(TRUE);
closesocket(m_mysocket);
return;
}
AfxMessageBox("4");
// WaitForSingleObject(fTdlg.m_thSender->m_hThread,INFINITE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -