📄 sokdlg.cpp
字号:
// sokDlg.cpp : implementation file
//
#include "stdafx.h"
#include "sok.h"
#include "sokDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CSokDlg dialog
CSokDlg::CSokDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSokDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSokDlg)
// 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 CSokDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSokDlg)
DDX_Control(pDX, IDC_LIST2, m_list1);
DDX_Control(pDX, IDC_LIST1, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSokDlg, CDialog)
//{{AFX_MSG_MAP(CSokDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSokDlg message handlers
BOOL CSokDlg::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
// Fss="我发送的数据";
m_ts.SetParent(this);
m_ta.SetParent(this);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSokDlg::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 CSokDlg::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 CSokDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*********************************
1.Create()
该函数的参数有:
1)端口,UINT类型。注意:如果是服务方,则使 用一个众所周知的端口供服务方连接;如果是客户方,
典型做法是接受默认参数,使 套接字可以自主选择一个可用端口;
2)socket 类型,可以是SOCK-STREAM(默认值,字节流)或SOCK-DGRAM(数据报);
3)socket的地址,例如"ftp.gliet.edu.cn"或"202.193.64.33"。
2.
*************************************/
void CSokDlg::OnButton1()
{
// TODO: Add your control notification handler code here
m_iType=1;
GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
BOOL F;
F=m_ts.Create(4000);
// m_ts.Listen();//成员函数开始 监听,一旦收到连接请求,则调用CAsyncSocket∷Accept()成员函数开始接收
if(F==0)
{
MessageBox("建立错误 !");
}
if(!m_ts.Listen(1))
{
int nErrorCode = m_ts.GetLastError();
if(nErrorCode = WSAEWOULDBLOCK)
{
MessageBox("侦听错误 !");
m_ts.Close();
return;
}
}
}
void CSokDlg::OnAccept()
{
m_ts.Accept(m_ta);
}
void CSokDlg::OnButton2()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
m_iType=0;
BOOL SS;
SS=m_ta.Create ();
if(!SS)
{
MessageBox("请求失败 !");
m_ta.Close();
return;
}
SS=m_ta.Connect ("192.168.0.12",4000);
}
void CSokDlg::OnButton3()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
int isent,ilen;
CString Fss;
Fss="我发送的数据";
ilen=Fss.GetLength ();
isent=m_ta.Send(LPCTSTR(Fss),ilen);
if(isent==SOCKET_ERROR)
{
MessageBox("连接失败,请重试!");
}
else
{
m_list1.AddString (Fss);
UpdateData(FALSE);
}
}
void CSokDlg::OnReceive()
{
// if(m_iType==1)
// MessageBox("您可以接收信息了!");
char *pbuf=new char[1025];
int ibufsize=1024;
int ircvd;
CString strrecvd;
ircvd=m_ta.Receive (pbuf,ibufsize);
if(ircvd==SOCKET_ERROR)
{
MessageBox("接收数据错误");
}
else
{
pbuf[ircvd]=NULL;
strrecvd=pbuf;
m_list.AddString(strrecvd);
UpdateData(FALSE);
}
}
void CSokDlg::OnSend()
{
if(m_iType==0)
MessageBox("您可以发送信息了!");
OnButton3();
}
void CSokDlg::OnConnet()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -