📄 tcpserverdlg.cpp
字号:
// TCPServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TCPServer.h"
#include "TCPServerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/****************************************************************************/
// globals
// defines
/* max clients connected at a time */
#define SERVER_MAX_CONNECTIONS 5
// Message to send for update
#define WM_UPDATE (WM_USER+100)
// receive socket
int snew;
// receive count and length
int nRxCount = 0,nRxLength = 0;
// data buffer
BYTE byRcvData[64];
// window handler
HWND myhWnd;
// receive thread
UINT RcvThread(LPVOID lParam);
// receive enable flag
BOOL rcvFlag = FALSE;
/***************************************************************************/
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTCPServerDlg dialog
CTCPServerDlg::CTCPServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTCPServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTCPServerDlg)
m_strLocalIP = _T("127.0.0.1");
m_nServerPort = 18018;
m_nClientPort = 0;
m_nRxCount = 0;
m_nRxLength = 0;
m_nClientIP = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
////////////////////////////////////////////////////////////////////////////
//
void CTCPServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTCPServerDlg)
DDX_Control(pDX, IDC_BUTTON_LISTEN, m_btnListen);
DDX_Control(pDX, IDC_LIST, m_list);
DDX_Text(pDX, IDC_EDIT_LOCAL_IP, m_strLocalIP);
DDX_Text(pDX, IDC_EDIT_SERVER_PORT, m_nServerPort);
DDX_Text(pDX, IDC_EDIT_CLIENT_PORT, m_nClientPort);
DDX_Text(pDX, IDC_EDIT_RX_COUNT, m_nRxCount);
DDX_Text(pDX, IDC_EDIT_RX_LENGTH, m_nRxLength);
DDX_Text(pDX, IDC_EDIT_CLIENT_IP, m_nClientIP);
//}}AFX_DATA_MAP
}
//////////////////////////////////////////////////////////////////////////////
//
BEGIN_MESSAGE_MAP(CTCPServerDlg, CDialog)
//{{AFX_MSG_MAP(CTCPServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_LISTEN, OnButtonListen)
ON_BN_CLICKED(IDC_BUTTON_RCV, OnButtonRcv)
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
ON_MESSAGE( WM_UPDATE, RcvUpdate )
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTCPServerDlg message handlers
BOOL CTCPServerDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
////////////////////////////////////////////////////////////////////////////////
//
void CTCPServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
/////////////////////////////////////////////////////////////////////////////////
//
void CTCPServerDlg::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 CTCPServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/////////////////////////////////////////////////////////////////////////////
// Listen,must run before client runs
void CTCPServerDlg::OnButtonListen()
{
UpdateData(TRUE);
myhWnd = this->m_hWnd;
m_btnListen.EnableWindow(FALSE);
/* socket fd's */
int sock;
/* server's address */
struct sockaddr_in serverAddr;
/* client's address */
struct sockaddr_in clientAddr;
/* length of clientAddr */
int client_len;
int rcvCount = 0;
memset(&serverAddr,0,sizeof (serverAddr));
memset(&clientAddr,0,sizeof (clientAddr));
// Create a socket
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
AfxMessageBox("Socket creates failed!\n");
return;
}
/* Set up our internet address, and bind it so the client can connect. */
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(m_nServerPort);
// Bind to local address
if(bind (sock, (struct sockaddr *)&serverAddr, sizeof (serverAddr))== -1)
{
closesocket(sock);
return;
}
/* Listen, for the client to connect to us. */
if (listen (sock, 1) == -1)
{
closesocket(sock);
return;
}
client_len = sizeof (clientAddr);
// Accept new connection
snew = accept (sock, (struct sockaddr *)&clientAddr, &client_len);
if (snew == -1)
{
closesocket(sock);
AfxMessageBox("Accept connection timeout!\n");
return;
}
// display connected client info
m_nClientIP = (UINT)ntohl(clientAddr.sin_addr.s_addr);
m_nClientPort = ntohs(clientAddr.sin_port);
m_list.AddString("Connection has been establish\n");
m_list.AddString("click RECEIVE button to receive data...\n");
UpdateData(FALSE);
}
///////////////////////////////////////////////////////////////////////////////////
// Update
void CTCPServerDlg::RcvUpdate(WPARAM wParam, LPARAM lParam)
{
int i,count;
CString str;
count = m_list.GetCount();
m_nRxCount = nRxCount;
m_nRxLength = nRxLength;
UpdateData(FALSE);
// Clear
for(i=0;i<=count;i++)
m_list.DeleteString(0);
for(i=0;i<8;i++)
{
str.Format("%02x\t%02x\t%02x\t%02x\t%02x\t%02x\t%02x\t%02x\n",
byRcvData[0+8*i],
byRcvData[1+8*i],
byRcvData[2+8*i],
byRcvData[3+8*i],
byRcvData[4+8*i],
byRcvData[5+8*i],
byRcvData[6+8*i],
byRcvData[7+8*i]);
m_list.AddString(str);
}
}
///////////////////////////////////////////////////////////////////////////////////
// Receive thread
UINT RcvThread(LPVOID lParam)
{
int nCurLength;
// Receive data
while(rcvFlag)
{
nCurLength = recv(snew,(char*)byRcvData,sizeof(byRcvData),0);
nRxLength += nCurLength;
if(nCurLength == 0)
{
Sleep(1);
}
else
{
nRxCount++;
::PostMessage(myhWnd,WM_UPDATE,0,0);
}
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
// Start the receive thread...
void CTCPServerDlg::OnButtonRcv()
{
// enable receive
rcvFlag = TRUE;
m_pThread = AfxBeginThread(RcvThread,0);
if(m_pThread == NULL)
{
AfxMessageBox("Thread begin failed!\n");
}
// Clear
for(int i=0;i<=m_list.GetCount();i++)
m_list.DeleteString(0);
}
//////////////////////////////////////////////////////////////////////////
// Close the connection
void CTCPServerDlg::OnButtonClose()
{
rcvFlag = FALSE;
Sleep(100);
if(m_pThread != NULL)
TerminateThread(m_pThread,0);
if(snew != 0)
closesocket(snew);
AfxMessageBox("Connection has been close!");
}
////////////////////////////////////////////////////////////////////////
// quit
void CTCPServerDlg::OnOK()
{
rcvFlag = FALSE;
Sleep(100);
if(m_pThread != NULL)
TerminateThread(m_pThread,0);
if(snew != 0)
closesocket(snew);
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -