📄 socketdlg.cpp
字号:
// SocketDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Socket.h"
#include "SocketDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CSocketDlg dialog
CSocketDlg::CSocketDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSocketDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSocketDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSocketDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSocketDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSocketDlg, CDialog)
//{{AFX_MSG_MAP(CSocketDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON1Star, OnBUTTON1Star)
ON_BN_CLICKED(IDC_BUTTON1Send, OnBUTTON1Send)
ON_BN_CLICKED(IDC_BUTTON1Exit, OnBUTTON1Exit)
ON_MESSAGE(WM_SOCKETEX, SetSocketEx)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_CHECK1RecvS, OnCHECK1RecvS)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSocketDlg message handlers
BOOL CSocketDlg::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
m_bStatusRun = false;
m_Bitmap[0].LoadBitmap(IDB_BITMAP1S0);
m_Bitmap[1].LoadBitmap(IDB_BITMAP1S1);
m_Bitmap[2].LoadBitmap(IDB_BITMAP1S2);
m_Bitmap[3].LoadBitmap(IDB_BITMAP1Se);
CButton* pBtn = (CButton*)GetDlgItem(IDC_BUTTON1Star);
pBtn->SetBitmap(m_Bitmap[0]);
pBtn = (CButton*)GetDlgItem(IDC_BUTTON1Exit);
pBtn->SetBitmap(m_Bitmap[2]);
pBtn = (CButton*)GetDlgItem(IDC_BUTTON1Send);
pBtn->SetBitmap(m_Bitmap[3]);
CString strCp,strIp = CSocketEx::GetIpAddress(strCp);
GetDlgItem(IDC_EDIT1IpR )->SetWindowText(strIp );
GetDlgItem(IDC_EDIT1IpSA )->SetWindowText(strIp );
GetDlgItem(IDC_EDIT1PortR )->SetWindowText("6801");
GetDlgItem(IDC_EDIT1PortS )->SetWindowText("6800");
GetDlgItem(IDC_EDIT1PortSA)->SetWindowText("6801");
((CButton*)GetDlgItem(IDC_CHECK1RecvS))->SetCheck( TRUE);
GetDlgItem(IDC_STATIC1PortS)->ShowWindow(SW_HIDE);
GetDlgItem( IDC_EDIT1PortS)->ShowWindow(SW_HIDE);
OnBUTTON1Star(); //程序一打开就自动执行缺省调用;
return TRUE; // return TRUE unless you set the focus to a control
}
void CSocketDlg::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 CSocketDlg::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 CSocketDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CSocketDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_LBUTTONDBLCLK)
{
CEdit* p;
p = (CEdit*)GetDlgItem(IDC_EDIT1ContR);
if(p && p->m_hWnd==pMsg->hwnd&&p->IsWindowVisible())
{
p->SetSel(0,-1);
return TRUE;
}
}
if((pMsg->message==WM_KEYDOWN)&&(pMsg->wParam==13))
{
//return FALSE; //终止缺省消息处理;
CEdit* p;
p = (CEdit*)GetDlgItem(IDC_EDIT1IpSA);
if(p && p->m_hWnd==pMsg->hwnd&&p->IsWindowVisible())
{
return TRUE;
}
p = (CEdit*)GetDlgItem(IDC_EDIT1IpR);
if(p && p->m_hWnd==pMsg->hwnd&&p->IsWindowVisible())
{
return TRUE;
}
p = (CEdit*)GetDlgItem(IDC_EDIT1PortSA);
if(p && p->m_hWnd==pMsg->hwnd&&p->IsWindowVisible())
{
return TRUE;
}
p = (CEdit*)GetDlgItem(IDC_EDIT1PortS);
if(p && p->m_hWnd==pMsg->hwnd&&p->IsWindowVisible())
{
return TRUE;
}
p = (CEdit*)GetDlgItem(IDC_EDIT1PortR);
if(p && p->m_hWnd==pMsg->hwnd&&p->IsWindowVisible())
{
return TRUE;
}
p = (CEdit*)GetDlgItem(IDC_EDIT1ContSA);
if(p && p->m_hWnd==pMsg->hwnd&&p->IsWindowVisible())
{ OnBUTTON1Send();
return TRUE;
}
p = (CEdit*)GetDlgItem(IDC_EDIT1ContR);
if(p && p->m_hWnd==pMsg->hwnd&&p->IsWindowVisible())
{ p->SetWindowText("");
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
void CSocketDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
}
void CSocketDlg::OnBUTTON1Star()
{
CButton* pBtn = (CButton*)GetDlgItem(IDC_BUTTON1Star);
BOOL bChk = ((CButton*)GetDlgItem(IDC_CHECK1RecvS))->GetCheck();
if( m_bStatusRun)
{
pBtn->SetBitmap(m_Bitmap[0]);
m_sockRecv.StopAction();
if(!bChk) m_sockSend.StopAction();
CButton* pS = (CButton*)GetDlgItem(IDC_BUTTON1Send);pS->EnableWindow(FALSE);
((CEdit*)GetDlgItem(IDC_EDIT1PortR))->SetReadOnly(FALSE);
((CEdit*)GetDlgItem(IDC_EDIT1PortS))->SetReadOnly(FALSE);
GetDlgItem(IDC_CHECK1RecvS)->EnableWindow( TRUE);
GetDlgItem(IDC_CHECK1RecvS)->ShowWindow(SW_SHOW);
}
else
if(!m_bStatusRun)
{
CString szPortS,szPortR;
GetDlgItem(IDC_EDIT1PortR)->GetWindowText(szPortR);
GetDlgItem(IDC_EDIT1PortS)->GetWindowText(szPortS);
m_sockRecv.Init2( this,atoi(szPortR), TRUE ); //接收端口初始化;
if(!bChk) m_sockSend.Init2( this,atoi(szPortS),FALSE ); //发送端口初始化;
UINT nFlagA = 0, nFlagB = 0;
nFlagA = m_sockRecv.StarAction();
if(nFlagA==0)
{
MessageBox("接收端口打开失败!","提示",MB_ICONINFORMATION);
((CEdit*)GetDlgItem(IDC_EDIT1PortR))->SetFocus();
((CEdit*)GetDlgItem(IDC_EDIT1PortR))->SetSel(0,-1);
}
if(!bChk)
{
nFlagB = m_sockSend.StarAction();
if(nFlagB==0)
{
MessageBox("发送端口打开失败!","提示",MB_ICONINFORMATION);
((CEdit*)GetDlgItem(IDC_EDIT1PortS))->SetFocus();
((CEdit*)GetDlgItem(IDC_EDIT1PortS))->SetSel(0,-1);
}
}
if(!(nFlagA==0&&nFlagB==0))
{
CButton* pS = (CButton*)GetDlgItem(IDC_BUTTON1Send);pS->EnableWindow( TRUE);
GetDlgItem(IDC_CHECK1RecvS)->ShowWindow(SW_HIDE);
((CEdit*)GetDlgItem(IDC_EDIT1PortR))->SetReadOnly( TRUE);
((CEdit*)GetDlgItem(IDC_EDIT1PortS))->SetReadOnly(!bChk);
GetDlgItem(IDC_CHECK1RecvS)->EnableWindow(FALSE);
pBtn->SetBitmap(m_Bitmap[1]);
}
else
{
return;
}
}
m_bStatusRun = !m_bStatusRun;
}
void CSocketDlg::OnBUTTON1Exit()
{
// TODO: Add your control notification handler code here
::SendMessage(this->m_hWnd,WM_CLOSE,0,0);
}
void CSocketDlg::OnBUTTON1Send()
{
CSocketEx* pSock;
//如果对方的接收端口不自动发送回执消息的话,此处可用发送端口代替!
BOOL bChk = ((CButton*)GetDlgItem(IDC_CHECK1RecvS))->GetCheck();
if(bChk) pSock = &m_sockRecv;
else pSock = &m_sockSend;
CString strContSA;GetDlgItem(IDC_EDIT1ContSA)->GetWindowText(strContSA);
static iIndex=0;
char BufSend[256];
int nBufLen = strContSA.GetLength();
if(nBufLen==0) {MessageBox("未输入发送消息内容!"); return;}
sprintf(BufSend,"%s",strContSA);
BufSend[nBufLen] = '\0';
/* //看其接收队列数否有收到的回执消息;
if(pSock->m_FifoT.bHasNode())
{
CFifoY<CByteArray,CString>* odd = pSock->m_FifoT.lpop();
//……省略;
delete odd;
}
*/
//端口0发送数据到端口1:
CString sPortSA; GetDlgItem(IDC_EDIT1PortSA)->GetWindowText(sPortSA);
CString strIpSA; GetDlgItem(IDC_EDIT1IpSA )->GetWindowText(strIpSA);
int nPortSA = atoi(sPortSA);
int iSend= pSock->SendTo(BufSend,nBufLen,nPortSA,strIpSA,0);
CString strCont;
GetDlgItem(IDC_EDIT1ContR)->GetWindowText(strCont);
CWnd* pWnd=GetDlgItem(IDC_EDIT1ContR);
char sp[255];sprintf(sp,"Send to: %s, %d",strIpSA,nPortSA);
strCont+= "\r\n";
strCont+= sp;
strCont+= "\r\n";
char c[255];memcpy(c,BufSend,nBufLen);
strCont+= BufSend;
pWnd->SetWindowText(strCont);
((CEdit*)pWnd)->SetSel(0,-1,FALSE);
CEdit* pE = (CEdit*)GetDlgItem(IDC_EDIT1ContSA);
pE->SetSel(0,-1); pE->SetFocus();
}
LONG CSocketDlg::SetSocketEx(UINT wParam,LONG lParam)
{
CFifoY<CByteArray,CString>* odd = (CFifoY<CByteArray,CString>*)wParam; LONG flag = lParam;
if(odd)
{
CByteArray* pBA = odd->GetpValX();
BYTE* bt = pBA->GetData();
int nSize = pBA->GetSize();
char* s = new char[nSize+1];s[nSize] = 0;
for(int r=0;r<nSize;r++)
*(s+r) = (char)(*(bt+r)); //原数据本为char*类型;
CString strMsg = s;
delete [] s; //立即删除中间的new对象;
CString strIp = odd->GetValY();
int nPort = odd->GetvSize();
if(*(bt+nSize-1)!=0) //判断最后一个字符是否为空字符!如果是,收到的是回复消息,不添加到列表;
{
CString strCont;
GetDlgItem(IDC_EDIT1ContR)->GetWindowText(strCont);
CWnd* pWnd=GetDlgItem(IDC_EDIT1ContR);
char sp[255];sprintf(sp,"Re from: %s, %d",strIp,nPort);
strCont+= "\r\n";
strCont+= sp;
strCont+= "\r\n";
strCont+= strMsg;
pWnd->SetWindowText(strCont);
((CEdit*)pWnd)->SetSel(0,-1,FALSE);
}
}
//if(flag==0) MessageBox("!","收到消息",MB_ICONINFORMATION);
return 1;
}
void CSocketDlg::OnCHECK1RecvS()
{
// TODO: Add your control notification handler code here
BOOL nbChk = ((CButton*)GetDlgItem(IDC_CHECK1RecvS))->GetCheck();
UINT n_swh = nbChk? SW_HIDE : SW_SHOW;
GetDlgItem(IDC_STATIC1PortS)->ShowWindow(n_swh);
GetDlgItem( IDC_EDIT1PortS)->ShowWindow(n_swh);
}
HBRUSH CSocketDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if( pWnd->GetDlgCtrlID()==IDC_STATIC1PortSA ) // 第1个控件
{
pDC->SetTextColor(RGB( 0,128,255));
}
if( pWnd->GetDlgCtrlID()==IDC_EDIT1PortSA ) // 第2个控件
{
pDC->SetTextColor(RGB( 0,128,255));
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -