📄 ucthread.cpp
字号:
/****************************************************************************/
/* FILE */
/* */
/* UCThread */
/* */
/* DESCRIPTION */
/* */
/* Contains functions to handle Winsock UDP Client. */
/* */
/****************************************************************************/
// UCThread.cpp : implementation file
//
#include "stdafx.h"
#include "nunetdem.h"
#include "NuNetDemDlg.h"
#include "UCThread.h"
#include "winsock.h"
#include "SockUtil.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUdpClientThread
//IMPLEMENT_DYNCREATE(CUdpClientThread, CWinThread)
IMPLEMENT_DYNAMIC(CUdpClientThread, CWinThread)
/****************************************************************************/
/* FUNCTION */
/* */
/* CUdpClientThread::CUdpClientThread */
/* */
/* DESCRIPTION */
/* */
/* Starts the UDP Client Thread. */
/* */
/****************************************************************************/
CUdpClientThread::CUdpClientThread(CNuNetDemDlg * pWnd)
{
pDlgWnd = pWnd;
// kill event starts out in the signaled state
m_hEventKill = CreateEvent(NULL, TRUE, FALSE, NULL);
m_hEventDead = CreateEvent(NULL, TRUE, FALSE, NULL);
}
/****************************************************************************/
/* FUNCTION */
/* */
/* CUdpClientThread::~CUdpClientThread */
/* */
/* DESCRIPTION */
/* */
/* Ends the UDP Client Thread. */
/* */
/****************************************************************************/
CUdpClientThread::~CUdpClientThread()
{
CloseHandle(m_hEventKill);
CloseHandle(m_hEventDead);
}
/****************************************************************************/
/* FUNCTION */
/* */
/* CUdpClientThread::InitInstance */
/* */
/* DESCRIPTION */
/* */
/* Initializes the UDP Client Thread. */
/* */
/****************************************************************************/
BOOL CUdpClientThread::InitInstance()
{
// TODO: perform and per-thread initialization here
//AfxMessageBox("UDP Client Thread Running");
m_sAddr = pDlgWnd->m_IPAddress;
CNuNetDemDlg::c_lBytesUDPClient = 0;
UpdateByteCount();
return TRUE;
}
/****************************************************************************/
/* FUNCTION */
/* */
/* CUdpClientThread::ExitInstance */
/* */
/* DESCRIPTION */
/* */
/* Performs Thread Clean-up. */
/* */
/****************************************************************************/
int CUdpClientThread::ExitInstance()
{
// TODO: perform any per-thread cleanup here
//AfxMessageBox("UDP Client Thread Stopped");
return CWinThread::ExitInstance();
}
/****************************************************************************/
/* FUNCTION */
/* */
/* CUdpClientThread::Delete */
/* */
/* DESCRIPTION */
/* */
/* Deletes teh UDP Client Thread. */
/* */
/****************************************************************************/
void CUdpClientThread::Delete()
{
// acknowledge receipt of kill notification
VERIFY(SetEvent(m_hEventDead));
// calling the base Delete
CWinThread::Delete();
}
/****************************************************************************/
/* FUNCTION */
/* */
/* CUdpClientThread::KillThread */
/* */
/* DESCRIPTION */
/* */
/* Kills the Thread. */
/* */
/****************************************************************************/
void CUdpClientThread::KillThread()
{
// Note: this function is called in the context of other threads,
// not the thread itself.
// reset the m_hEventKill which signals the thread to shutdown
VERIFY(SetEvent(m_hEventKill));
// allow thread to run at higher priority during kill process
SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL);
WaitForSingleObject(m_hEventDead, INFINITE);
// now delete CWinThread object since no longer necessary
//delete this;
}
BEGIN_MESSAGE_MAP(CUdpClientThread, CWinThread)
//{{AFX_MSG_MAP(CUdpClientThread)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUdpClientThread message handlers
/****************************************************************************/
/* FUNCTION */
/* */
/* CUdpClientThread::UpdateByteCount */
/* */
/* DESCRIPTION */
/* */
/* Task performs a Winsock UDP Client and updates status from an embedded */
/* client. */
/* */
/****************************************************************************/
int CUdpClientThread::UpdateByteCount()
{
WSADATA stWSAData; /* return data for Winsock startup */
SOCKET s; /* socket handle */
struct sockaddr_in stRmtAddr; /* structure of remote socket */
char OutBuf [buflength]; /* array for output */
char InBuf [buflength]; /* array for input */
int cRet; /* connection return error value */
int sRet; /* startup return error value */
int snRet; /* send return error value */
int rRet; /* read return error value */
int clRet; /* close return error value */
long loop= 0; /* counter for loop */
unsigned long ulloop= 0;
int port = pDlgWnd->m_Port; /* number of port to use */
int portspecifid=0;
int hostspecifid=0;
int loopnumspec=0; /* flags for what is on command line */
int txtfilsupp=0;
unsigned long sizestr=0;
unsigned long ulTotalBytes = 0;
CString sNumBytes;
CString sStatus;
CSockUtil sockUtil; /* WinSock Error Messages */
int i,count;
// Initialize Winsock - WSAData not looked at
sRet = WSAStartup(0x0101, &stWSAData);
if (sRet != 0)
{
AfxMessageBox("Winsock startup error\n"); /*can't call GetLastError */
return 1;
}
// Ask for a udp socket
s = socket(PF_INET, SOCK_DGRAM, 0);
if (s == INVALID_SOCKET)
{
sStatus.Format("Unable to get socket - %s\n",sockUtil.SockerrToString(WSAGetLastError() ) );
AfxMessageBox(sStatus);
}
// Setup the Remote Socket Address Structure
stRmtAddr.sin_family = PF_INET; /*internet family */
stRmtAddr.sin_port = htons(port);
stRmtAddr.sin_addr.s_addr = (inet_addr(m_sAddr));
// Connect
cRet = connect(s,
(struct sockaddr FAR *)&stRmtAddr,
sizeof(struct sockaddr));
if (cRet == SOCKET_ERROR)
{
sStatus.Format("Unable to connect socket - %s\n",sockUtil.SockerrToString(WSAGetLastError() ) );
AfxMessageBox(sStatus);
}
// Initialize Loop Counter
loop = 0;
Sleep(1000);
// Wait for thread event
while ( (WaitForSingleObject(m_hEventKill, 0) != WAIT_OBJECT_0) &&
(loop != pDlgWnd->m_num_trans1))
{
// Create String to Send Out
memset(OutBuf, 0, pDlgWnd->m_size_bytes1);
count= 0x30;
for (i = 0; i < pDlgWnd->m_size_bytes1; i++)
{
OutBuf[i]= count;
count++;
if (count == 0x3a)
count = 0x30;
}
// Send it
snRet = send(s,
(LPSTR)&OutBuf, /*send the string */
pDlgWnd->m_size_bytes1,
0); /* flags, 0 = no flags */
if (snRet == SOCKET_ERROR)
{
sStatus.Format("Unable to send Error - %s\n",sockUtil.SockerrToString(WSAGetLastError() ) );
AfxMessageBox(sStatus);
}
// Prepare/Receive a Buffer
memset(InBuf, 0, buflength);
rRet = recv(s, /* time to get the string back */
(LPSTR)&InBuf,
pDlgWnd->m_size_bytes1,
0);
if (rRet == SOCKET_ERROR)
{
sStatus.Format("Unable to receive Error - %s\n",sockUtil.SockerrToString(WSAGetLastError() ) );
AfxMessageBox(sStatus);
}
CNuNetDemDlg::c_lBytesUDPClient += snRet;
ulTotalBytes += rRet;
sNumBytes.Format("%u",ulTotalBytes);
pDlgWnd->m_edBytesUDPClient.SetWindowText(sNumBytes);
ulloop = loop;
sNumBytes.Format("%u",ulloop+1);
pDlgWnd->m_edpcktUDPCLIENT.SetWindowText(sNumBytes);
pDlgWnd->UpdateList(OutBuf, loop +1, rRet);
loop++;
} // End of Loop
/* Send the Quit 'q' datagram. */
snRet = send(s, (char*)"q", 1, 0);
// Check for Send Errors
if (snRet == SOCKET_ERROR)
{
sStatus.Format("Unable to send Quit Cmd - %s\n",sockUtil.SockerrToString(WSAGetLastError() ) );
AfxMessageBox(sStatus);
}
// Close the Socket - only use closesocket, no shutdown()
clRet = closesocket(s);
// Check for Closing Socket Errors
if (clRet != 0)
{
sStatus.Format("Problem closing socket! - %s\n",sockUtil.SockerrToString(WSAGetLastError() ) );
AfxMessageBox(sStatus);
}
// Finished with Winsock - De-register WinSock
WSACleanup();
pDlgWnd->m_bUDPClientStatus = FALSE;
pDlgWnd->ToggleButtons(TRUE);
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -