📄 tcp ip demodlg.cpp
字号:
// TCP IP DemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TCP IP Demo.h"
#include "TCP IP DemoDlg.h"
#include "winsock.h"
#include "Server.h"
#include "string.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DEFAULT_BUFFER 1024*16
char hostname[256];
struct hostent * pHostent;
struct sockaddr_in sa;
int nAdapter;
UINT m_uPort = 5150;
DWORD bufsize;
unsigned char DataBuff[DEFAULT_BUFFER];
unsigned int TransLen;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTCPIPDemoDlg dialog
CTCPIPDemoDlg::CTCPIPDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTCPIPDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTCPIPDemoDlg)
m_SendData = _T("");
m_ReceiveData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTCPIPDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTCPIPDemoDlg)
DDX_Control(pDX, IDC_REDIT, m_ReceiveText);
DDX_Control(pDX, IDC_SEDIT, m_SendText);
DDX_Text(pDX, IDC_SEDIT, m_SendData);
DDX_Text(pDX, IDC_REDIT, m_ReceiveData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTCPIPDemoDlg, CDialog)
//{{AFX_MSG_MAP(CTCPIPDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_MESSAGE(SER_MESSAGE,OnServerMessage)
ON_BN_CLICKED(IDC_CLEARSEND, OnClearsend)
ON_BN_CLICKED(IDC_CLEARRECEIVE, OnClearreceive)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTCPIPDemoDlg message handlers
BOOL CTCPIPDemoDlg::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_hNetEnableIcon = AfxGetApp()->LoadIcon(IDI_NETOPEN);
m_hNetDisableIcon = AfxGetApp()->LoadIcon(IDI_NETCLOSE);
((CButton *)GetDlgItem(IDC_NETSTATE))->EnableWindow(TRUE);
((CButton *)GetDlgItem(IDC_SEND))->EnableWindow(FALSE);
((CButton *)GetDlgItem(IDC_NETSTATE))->SetIcon(m_hNetDisableIcon);
InitSocket();
OnSetServer();
return TRUE; // return TRUE unless you set the focus to a control
}
void CTCPIPDemoDlg::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 CTCPIPDemoDlg::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 CTCPIPDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTCPIPDemoDlg::OnSend()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
// Get input char
TransLen = m_SendData.GetLength();
if (TransLen > DEFAULT_BUFFER)
TransLen = DEFAULT_BUFFER;
CopyString((char *)DataBuff, m_SendData, TransLen);
// Send char to dsp
if(SOCKET_ERROR == send(socket, (char *)DataBuff, TransLen, 0))
{
AfxMessageBox("Send data error");
}
}
//////////////////////////////////////////////////////////////////////////
// 初始化Socket
void CTCPIPDemoDlg::InitSocket()
{
m_Fopened = FALSE;
WSADATA wsaData;
WORD version = MAKEWORD(2, 0);
int ret = WSAStartup(version, &wsaData);
if(ret != 0)
{
AfxMessageBox("Failed in initial socket");
}
m_connectionList.RemoveAll();
ASSERT(m_connectionList.GetCount()==0);
//获取主机的Ip地址
int res = gethostname(hostname,sizeof(hostname));
if(res != 0)
{
AfxMessageBox("gethostname is error!");
return ;
}
// get the res of the hostname
pHostent = gethostbyname(hostname);
if(pHostent ==NULL)
{
AfxMessageBox("the Ip address is error!");
return ;
}
for (nAdapter=0; pHostent->h_addr_list[nAdapter]; nAdapter++)
{
memcpy ( &sa.sin_addr.s_addr, pHostent->h_addr_list[nAdapter],pHostent->h_length);
}
}
//////////////////////////////////////////////////////////////////////////
// 设置网络服务器
void CTCPIPDemoDlg::OnSetServer()
{
// TODO: Add your control notification handler code here
m_server.m_addr = sa;
if(m_server.InitAndListen(this->m_hWnd,m_uPort)==FALSE)
{
AfxMessageBox("Set server error!");
return;
}
}
//////////////////////////////////////////////////////////////////////////
//Server消息相应函数
LRESULT CTCPIPDemoDlg::OnServerMessage(WPARAM wParam, LPARAM lParam)
{
int length,len,i;
CEdit * output=NULL;
CString Stemp;
int TotalLen;
switch(lParam)
{
case FD_ACCEPT:
socket= accept(m_server.m_hSocket,NULL,NULL);
m_connectionList.AddHead(socket);
((CButton *)GetDlgItem(IDC_NETSTATE))->SetIcon(m_hNetEnableIcon);
((CButton *)GetDlgItem(IDC_SEND))->EnableWindow(TRUE);
return 0;
case FD_READ:
length=m_connectionList.GetCount();
for(i=0;i<length;i++)
{
socket=m_connectionList.GetAt(m_connectionList.FindIndex(i));
if(socket==wParam)
{
TotalLen = 0;
Loop1:
len = recv(socket,(char *)&DataBuff[TotalLen],TransLen,0);
// If network is error
if (len == SOCKET_ERROR)
{
AfxMessageBox("Receive data error!");
return 0;
}
TotalLen += len;
if(len == TransLen)
{
Stemp.Empty();
Stemp = GetString(DataBuff, TotalLen);
m_ReceiveData += Stemp;
UpdateData(FALSE);
return 0;
}
else
{
TransLen -= len;
goto Loop1;
}
}
}
return 0;
case FD_WRITE:
return 0;
case FD_CLOSE:
((CButton *)GetDlgItem(IDC_NETSTATE))->SetIcon(m_hNetDisableIcon);
((CButton *)GetDlgItem(IDC_SEND))->EnableWindow(FALSE);
return 0;
default:
return 0;
}
}
void CTCPIPDemoDlg::CopyString(char *szTo, CString sFrom, int nLimit)
{
memset(szTo, 0, nLimit);
int nLen = sFrom.GetLength();
if(nLen > nLimit)
nLen = nLimit;
if(nLen > 0)
memcpy(szTo, LPCTSTR(sFrom), nLen);
}
CString CTCPIPDemoDlg::GetString(BYTE *pBuf, int nMaxLen)
{
CString sTemp;
sTemp.Empty();
if(nMaxLen < 1)
return sTemp;
BYTE* pszTemp = new BYTE[nMaxLen];
ZeroMemory(pszTemp, nMaxLen);
memcpy(pszTemp, pBuf, nMaxLen);
sTemp.Format("%s", pszTemp);
return sTemp;
}
void CTCPIPDemoDlg::OnClearsend()
{
// TODO: Add your control notification handler code here
m_SendData.Empty();
UpdateData(FALSE);
}
void CTCPIPDemoDlg::OnClearreceive()
{
// TODO: Add your control notification handler code here
m_ReceiveData.Empty();
UpdateData(FALSE);
}
//////////////////////////////////////////////////////////////////////////
// No more
//////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -