📄 tcthread.cpp
字号:
// TCThread.cpp : implementation file
//
/****************************************************************************/
/* FILE */
/* */
/* TCThread.cpp */
/* */
/* DESCRIPTION */
/* */
/* Holds the thread code for the Winsock TCP Client Thread. */
/* */
/****************************************************************************/
#include "stdafx.h"
#include "nunetdem.h"
#include "NuNetDemDlg.h"
#include "TCThread.h"
#include "winsock.h"
#include "SockUtil.h"
#include "tcpcli.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TESTTIME 30000
#define NU_SUCCESS 0
#define UNSIGNED unsigned long
/////////////////////////////////////////////////////////////////////////////
// CTcpClientThread
//IMPLEMENT_DYNCREATE(CTcpClientThread, CWinThread)
IMPLEMENT_DYNAMIC(CTcpClientThread, CWinThread)
BEGIN_MESSAGE_MAP(CTcpClientThread, CWinThread)
//{{AFX_MSG_MAP(CTcpClientThread)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/****************************************************************************/
/* FUNCTION */
/* */
/* CTcpClientThread */
/* */
/* DESCRIPTION */
/* */
/* Starts the TCP Client Thread. */
/* */
/****************************************************************************/
CTcpClientThread::CTcpClientThread(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 */
/* */
/* ~CTcpClientThread */
/* */
/* DESCRIPTION */
/* */
/* Ends the TCP Client Thread. */
/* */
/****************************************************************************/
CTcpClientThread::~CTcpClientThread()
{
CloseHandle(m_hEventKill);
CloseHandle(m_hEventDead);
}
/****************************************************************************/
/* FUNCTION */
/* */
/* InitInstance */
/* */
/* DESCRIPTION */
/* */
/* Initial Instance of the Thread. */
/* */
/****************************************************************************/
BOOL CTcpClientThread::InitInstance()
{
// loop but check for kill notification
CNuNetDemDlg::c_lBytesTCPClient = 0;
UpdateByteCount();
// avoid entering standard message loop by returning FALSE
return FALSE;
}
/****************************************************************************/
/* FUNCTION */
/* */
/* ExitInstamce */
/* */
/* DESCRIPTION */
/* */
/* Handles the Thread exit instance. */
/* */
/****************************************************************************/
int CTcpClientThread::ExitInstance()
{
// TODO: perform any per-thread cleanup here
return CWinThread::ExitInstance();
}
/****************************************************************************/
/* FUNCTION */
/* */
/* Delete */
/* */
/* DESCRIPTION */
/* */
/* Deletes the Thread. */
/* */
/****************************************************************************/
void CTcpClientThread::Delete()
{
// acknowledge receipt of kill notification
VERIFY(SetEvent(m_hEventDead));
// calling the base Delete
CWinThread::Delete();
}
/****************************************************************************/
/* FUNCTION */
/* */
/* CTcpClientThread::KillThread */
/* */
/* DESCRIPTION */
/* */
/* Note: this function is called in the context of other threads, */
/* not the thread itself. */
/* */
/****************************************************************************/
void CTcpClientThread::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;
}
/****************************************************************************/
/* FUNCTION */
/* */
/* CTcpClientThread::UpdateByteCount */
/* */
/* DESCRIPTION */
/* */
/* The TCP CLient Thread is performed and executed. */
/* */
/****************************************************************************/
int CTcpClientThread::UpdateByteCount()
{
int socketd; /* the socket descriptor */
struct sockaddr_in *servaddr; /* holds the server address structure */
unsigned int *return_ptr;
int status;
int bytes_received;
int bytes_sent;
char *buffer;
char *nbuffer;
long total_pkts = 0;
char *tbuf;
WSADATA WsaData;
CString sNumBytes;
unsigned long ulTotalBytes = 0;
int port= pDlgWnd->m_Port; /* number of port to use */
CString sStatus;
CSockUtil sockUtil; /* WinSock Error Messages */
int count;
unsigned long ultotal_pkts = 0;
int i;
// Initialize Windows Sockets and request version 1.1
status = WSAStartup (0x0101, &WsaData);
if (status != 0)
{
AfxMessageBox("WSAStartup() failed");
return 1;
}
// Allocate space for the buffer.
buffer = (char *)malloc (10000);
nbuffer = (char *)malloc (10000);
tbuf = (char *)malloc (100);
if (buffer == NULL)
{
/* Can't allocate memory, get out. */
AfxMessageBox("Cannot allocate memory for TCP buffer\n\r");
return 1;
}
Sleep(1000);
/* open a connection via the socket interface */
if ((socketd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != INVALID_SOCKET)
{
//AfxMessageBox(" socket successes\n");
// Allocate Memory for the local or remote endpoint address to which to connect a socket
return_ptr = (unsigned int *) malloc ( sizeof(struct sockaddr_in));
if (return_ptr != NULL)
{
servaddr = (struct sockaddr_in *)return_ptr;
// Fill in a structure with the server address
servaddr->sin_family = AF_INET;
servaddr->sin_port = htons(port);
servaddr->sin_addr.s_addr = (inet_addr(pDlgWnd->m_IPAddress));
// Connect
if (( status = connect (socketd, (PSOCKADDR)servaddr, sizeof(*servaddr))) != 0 )
{
sStatus.Format("Connection Error - %s\n",sockUtil.SockerrToString(WSAGetLastError() ) );
AfxMessageBox(sStatus);
return (1);
}
else
{
// Wait for thread event
while ( (WaitForSingleObject(m_hEventKill, 0) != WAIT_OBJECT_0) &&
(total_pkts != pDlgWnd->m_num_trans))
{
// Create/Send the Datagram
// init count to 0
count= 0x30;
for (i = 0; i < pDlgWnd->m_size_bytes; i++)
{
buffer[i]= count;
count++;
if (count == 0x3a)
count = 0x30;
}
bytes_sent = send(socketd, buffer, pDlgWnd->m_size_bytes, 0);
/* If the data was not sent, we have a problem. */
if (bytes_sent < 0)
{
sStatus.Format("Error sending to the TCP server - %s\n",sockUtil.SockerrToString(WSAGetLastError() ) );
AfxMessageBox(sStatus);
break;
}
// Show on UI Total Bytes sent
CNuNetDemDlg::c_lBytesTCPClient += bytes_sent;
ulTotalBytes += bytes_sent;
sNumBytes.Format("%u",ulTotalBytes);
pDlgWnd->m_edBytesTCPClient.SetWindowText(sNumBytes);
// Only wait for a string if we sent something.
if (bytes_sent > 0)
{
// Receive: Go get the server's response.
bytes_received = recv(socketd, nbuffer, 10000, 0);
// If we got an error, its bad.
if (memcmp(buffer,nbuffer,bytes_received) != 0)
{
sStatus.Format("Error strings to the TCP server - %d\n",bytes_received);
}
if (bytes_received < 0)
{
sStatus.Format("Error in receiving from TCP server - %s\n",sockUtil.SockerrToString(WSAGetLastError() ) );
AfxMessageBox(sStatus);
break;
}
// NULL terminate the string.
buffer[bytes_received] = '\0';
ultotal_pkts= total_pkts;
sNumBytes.Format("%u",ultotal_pkts + 1);
pDlgWnd->m_edpcktTCPCLIENT.SetWindowText(sNumBytes);
// Show the user that we got something back.
pDlgWnd->UpdateList(buffer, total_pkts+1, bytes_received);
} // If bytes_sent > 0
total_pkts++;
} // While not quitting
} // Connected
// Close the connection
if ((closesocket(socketd)) != NU_SUCCESS)
{
sStatus.Format("Error from closesocket - %s\n",sockUtil.SockerrToString(WSAGetLastError() ) );
AfxMessageBox(sStatus);
//exit(0);
}
} // status = NU_SUCCESS
} // end successful NU_Socket
// Clean UP
free(buffer);
free(return_ptr);
// De-register WinSock
WSACleanup();
pDlgWnd->m_bTCPClientStatus = FALSE;
pDlgWnd->ToggleButtons(TRUE);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CTcpClientThread message handlers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -