简单的TCP客户端发包工具

简单的TCP客户端发包工具一、TCP介绍先放这里有时间在写,最近在写DuiLib相关的使用内容,这部分大家凑活着看二、程序截图下载链接链接:https://pan.baidu.com/s/1MzNUzwd7WwBat6vNMcu6Ow密码:ibuv 主要源码://.h//TCPClient.cpp:定义应用程序的类行为。//#include"stdafx.h"#in…

一、TCP介绍

先放这里有时间在写,最近在写DuiLib相关的使用内容,这部分大家凑活着看

二、程序截图

简单的TCP客户端发包工具

下载链接

链接:https://pan.baidu.com/s/1MzNUzwd7WwBat6vNMcu6Ow 密码:ibuv

 

主要源码:

//.h


// TCPClient.cpp : 定义应用程序的类行为。
//

#include "stdafx.h"
#include "TCPClient.h"
#include "TCPClientDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTCPClientApp

BEGIN_MESSAGE_MAP(CTCPClientApp, CWinApp)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CTCPClientApp 构造

CTCPClientApp::CTCPClientApp()
{
	// 支持重新启动管理器
	m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

	// TODO: 在此处添加构造代码,
	// 将所有重要的初始化放置在 InitInstance 中
}


// 唯一的一个 CTCPClientApp 对象

CTCPClientApp theApp;


// CTCPClientApp 初始化

BOOL CTCPClientApp::InitInstance()
{
	// 如果一个运行在 Windows XP 上的应用程序清单指定要
	// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
	//则需要 InitCommonControlsEx()。否则,将无法创建窗口。
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// 将它设置为包括所有要在应用程序中使用的
	// 公共控件类。
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinApp::InitInstance();


	AfxEnableControlContainer();

	// 创建 shell 管理器,以防对话框包含
	// 任何 shell 树视图控件或 shell 列表视图控件。
	CShellManager *pShellManager = new CShellManager;

	// 标准初始化
	// 如果未使用这些功能并希望减小
	// 最终可执行文件的大小,则应移除下列
	// 不需要的特定初始化例程
	// 更改用于存储设置的注册表项
	// TODO: 应适当修改该字符串,
	// 例如修改为公司或组织名
	SetRegistryKey(_T("应用程序向导生成的本地应用程序"));

	CTCPClientDlg dlg;
	m_pMainWnd = &dlg;
	INT_PTR nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: 在此放置处理何时用
		//  “确定”来关闭对话框的代码
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: 在此放置处理何时用
		//  “取消”来关闭对话框的代码
	}

	// 删除上面创建的 shell 管理器。
	if (pShellManager != NULL)
	{
		delete pShellManager;
	}

	// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
	//  而不是启动应用程序的消息泵。
	return FALSE;
}

//.cpp


// TCPClientDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "TCPClient.h"
#include "TCPClientDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define TIMEID 1
CTCPClientDlg* CTCPClientDlg::m_TCPClient;

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CTCPClientDlg 对话框




CTCPClientDlg::CTCPClientDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CTCPClientDlg::IDD, pParent)
	, m_DestIP(0)
	, m_DestPort(0)
	, m_strContent(_T(""))
	, m_Status(_T(""))
	, m_Detail(_T(""))
{
	m_TCPClient = this;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTCPClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_IPAddress(pDX, IDC_IPADDRESS1, m_DestIP);
	DDX_Text(pDX, IDC_EDIT_PORT, m_DestPort);
	DDX_Text(pDX, IDC_EDIT_CONTENT, m_strContent);
	DDX_Control(pDX, IDC_STATIC_BCONNECT, m_Tip);
	DDX_Text(pDX, IDC_STATIC_BCONNECT, m_Status);
	DDX_Text(pDX, IDC_EDIT_RESULT, m_Detail);
	DDX_Control(pDX, IDC_EDIT_RESULT, m_EditResult);
	DDX_Control(pDX, IDC_EDIT_FAIL, m_EditFail);
	DDX_Control(pDX, IDC_COMBO1, m_ComboBox);
}

BEGIN_MESSAGE_MAP(CTCPClientDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CON_SEND, &CTCPClientDlg::OnBnClickedButtonConSend)
	ON_BN_CLICKED(IDC_BUTTON_SEND, &CTCPClientDlg::OnBnClickedButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CTCPClientDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_RECONNECT, &CTCPClientDlg::OnBnClickedButtonReconnect)
END_MESSAGE_MAP()


// CTCPClientDlg 消息处理程序

BOOL CTCPClientDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	m_ComboBox.SetCurSel(0);
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CTCPClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CTCPClientDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CTCPClientDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CTCPClientDlg::OnBnClickedButtonConSend()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	CString strCBText;
	m_EditResult.SetWindowTextA("");
	m_EditFail.SetWindowTextA("");

	UpdateData(TRUE);
	unsigned char *pIP;
	CString strIP;//IP
	CString strPort;//端口号
	//CString strTimes;//发送次数
	CString strContent;//发送内容
	CString strStatus;
	CString strErr;
	//显示时间的变量
	int iHour;
	int iMinute;
	int iSecond;
	pIP = (unsigned   char*)&m_DestIP;
	strIP.Format("%u.%u.%u.%u", *(pIP + 3), *(pIP + 2), *(pIP + 1), *pIP);
	strPort.Format("%u", m_DestPort);
	//strTimes.Format("%u", m_SendTimes);
	strContent.Format("%s", m_strContent);
	strStatus.Format("%s", "连接状态:未连接");
	WSADATA wsaData;
	char buff[1024];
	memset(buff, 0, sizeof(buff));

	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
	{
		MessageBox("状态:加载Winsock失败", "提示", 0);
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		//获得系统时间显示发送内容
		CString strContinueTIP;
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strContinueTIP.Format("%d:%d:%d %s\r\n\r\n", iHour, iMinute, iSecond, strErr);
		m_EditFail.SetSel(-1);
		m_EditFail.ReplaceSel(strContinueTIP);
		strContinueTIP.Empty();
		strErr.Empty();
		return;
	}
	SOCKADDR_IN addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_port = htons(atoi(strPort));
	addrSrv.sin_addr.S_un.S_addr = inet_addr(strIP.GetBuffer());
	//创建套接字  
	m_SocketClient = socket(AF_INET, SOCK_STREAM, 0);
	if (SOCKET_ERROR == m_SocketClient)
	{

		strErr.Format("状态:Socket创建失败:%d", WSAGetLastError());
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		//获得系统时间显示发送内容
		CString strContinueTIP;
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strContinueTIP.Format("%d:%d:%d %s\r\n\r\n", iHour, iMinute, iSecond, strErr);
		m_EditFail.SetSel(-1);
		m_EditFail.ReplaceSel(strContinueTIP);
		strContinueTIP.Empty();
		strErr.Empty();
		//MessageBox(strErr, "提示", 0);
		return;
	}
	//向服务器发出连接请求  
	if (connect(m_SocketClient, (struct  sockaddr*)&addrSrv, sizeof(addrSrv)) == INVALID_SOCKET) {
		//printf(”Connect failed : %d”, WSAGetLastError());

		strErr.Format("状态:连接失败:%d", WSAGetLastError());
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		//获得系统时间显示发送内容
		CString strContinueTIP;
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strContinueTIP.Format("%d:%d:%d %s\r\n\r\n", iHour, iMinute, iSecond, strErr);
		m_EditFail.SetSel(-1);
		m_EditFail.ReplaceSel(strContinueTIP);
		strContinueTIP.Empty();
		strErr.Empty();
		//MessageBox(strErr, "提示", 0);
		return;
	}
	else
	{
		strErr.Format("状态:已连接");
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
	}



	//获得ComboBox内容
	int nIndex = m_ComboBox.GetCurSel();
	m_ComboBox.GetLBText(nIndex, strCBText);
	//设置定时器
	int iTimes = atoi(strCBText.GetString());
	UINT uElapse = 1000 / iTimes;
	//开始定时发送数据  
	SetTimer(TIMEID, uElapse, TimerProc);
	CWnd* cwndBtStop =  GetDlgItem(IDC_BUTTON_STOP);
	cwndBtStop->EnableWindow(TRUE);
	CWnd* cwndBtContinue = GetDlgItem(IDC_BUTTON_CON_SEND);
	cwndBtContinue->EnableWindow(FALSE);
	return;
}


void CTCPClientDlg::OnBnClickedButtonSend()
{
	// TODO: 在此添加控件通知处理程序代码
	m_EditResult.SetWindowTextA("");
	m_EditFail.SetWindowTextA("");
	SendTcpMessage();
}


void CTCPClientDlg::OnBnClickedButtonStop()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	if (FALSE == KillTimer(TIMEID))
	{
		MessageBox("停止失败", "提示", 0);
	}
	CWnd* cwndBtStop = GetDlgItem(IDC_BUTTON_STOP);
	cwndBtStop->EnableWindow(FALSE);
	CWnd* cwndBtContinue = GetDlgItem(IDC_BUTTON_CON_SEND);
	cwndBtContinue->EnableWindow(TRUE);
	closesocket(m_SocketClient);
	WSACleanup();
}


void CTCPClientDlg::OnBnClickedButtonReconnect()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	CString strCBText;

	UpdateData(TRUE);
	unsigned char *pIP;
	CString strIP;//IP
	CString strPort;//端口号
	//CString strTimes;//发送次数
	CString strContent;//发送内容
	CString strStatus;
	CString strErr;
	//显示时间的变量
	int iHour;
	int iMinute;
	int iSecond;
	pIP = (unsigned   char*)&m_DestIP;
	strIP.Format("%u.%u.%u.%u", *(pIP + 3), *(pIP + 2), *(pIP + 1), *pIP);
	strPort.Format("%u", m_DestPort);
	//strTimes.Format("%u", m_SendTimes);
	strContent.Format("%s", m_strContent);
	strStatus.Format("%s", "连接状态:未连接");
	WSADATA wsaData;
	char buff[1024];
	memset(buff, 0, sizeof(buff));

	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
	{
		MessageBox("状态:加载Winsock失败", "提示", 0);
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		//获得系统时间显示发送内容
		CString strContinueTIP;
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strContinueTIP.Format("%d:%d:%d %s\r\n\r\n", iHour, iMinute, iSecond, strErr);
		m_EditFail.SetSel(-1);
		m_EditFail.ReplaceSel(strContinueTIP);
		strContinueTIP.Empty();
		strErr.Empty();
		return;
	}
	SOCKADDR_IN addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_port = htons(atoi(strPort));
	addrSrv.sin_addr.S_un.S_addr = inet_addr(strIP.GetBuffer());
	//创建套接字  
	m_SocketClient = socket(AF_INET, SOCK_STREAM, 0);
	if (SOCKET_ERROR == m_SocketClient)
	{

		strErr.Format("状态:Socket创建失败:%d", WSAGetLastError());
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		//获得系统时间显示发送内容
		CString strContinueTIP;
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strContinueTIP.Format("%d:%d:%d %s\r\n\r\n", iHour, iMinute, iSecond, strErr);
		m_EditFail.SetSel(-1);
		m_EditFail.ReplaceSel(strContinueTIP);
		strContinueTIP.Empty();
		strErr.Empty();
		//MessageBox(strErr, "提示", 0);
		return;
	}
	//向服务器发出连接请求  
	if (connect(m_SocketClient, (struct  sockaddr*)&addrSrv, sizeof(addrSrv)) == INVALID_SOCKET) {
		//printf(”Connect failed : %d”, WSAGetLastError());

		strErr.Format("状态:连接失败:%d", WSAGetLastError());
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		//获得系统时间显示发送内容
		CString strContinueTIP;
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strContinueTIP.Format("%d:%d:%d %s\r\n\r\n", iHour, iMinute, iSecond, strErr);
		m_EditFail.SetSel(-1);
		m_EditFail.ReplaceSel(strContinueTIP);
		strContinueTIP.Empty();
		strErr.Empty();
		//MessageBox(strErr, "提示", 0);
		return;
	}
	else
	{
		strErr.Format("状态:已连接");
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
	}



	//获得ComboBox内容
	int nIndex = m_ComboBox.GetCurSel();
	m_ComboBox.GetLBText(nIndex, strCBText);
	//设置定时器
	int iTimes = atoi(strCBText.GetString());
	UINT uElapse = 1000 / iTimes;
	//开始定时发送数据  
	SetTimer(TIMEID, uElapse, TimerProc);
	CWnd* cwndBtStop = GetDlgItem(IDC_BUTTON_STOP);
	cwndBtStop->EnableWindow(TRUE);
	CWnd* cwndBtContinue = GetDlgItem(IDC_BUTTON_CON_SEND);
	cwndBtContinue->EnableWindow(FALSE);
	CWnd* cwndBtReContinue = m_TCPClient->GetDlgItem(IDC_BUTTON_RECONNECT);
	cwndBtReContinue->EnableWindow(FALSE);
	return;
}
void CTCPClientDlg::SendTcpMessage()
{
	UpdateData(TRUE);
	unsigned char *pIP;
	CString strIP;//IP
	CString strPort;//端口号
	//CString strTimes;//发送次数
	CString strContent;//发送内容
	CString strStatus;
	CString strErr;
	//显示时间的变量
	int iHour;
	int iMinute;
	int iSecond;
	pIP = (unsigned   char*)&m_DestIP;
	strIP.Format("%u.%u.%u.%u", *(pIP + 3), *(pIP + 2), *(pIP + 1), *pIP);
	strPort.Format("%u", m_DestPort);
	//strTimes.Format("%u", m_SendTimes);
	strContent.Format("%s", m_strContent);
	strStatus.Format("%s", "连接状态:未连接");
	WSADATA wsaData;
	char buff[1024];
	memset(buff, 0, sizeof(buff));

	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
	{
		MessageBox("状态:加载Winsock失败", "提示", 0);
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		//获得系统时间显示发送内容
		CString strContinueTIP;
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strContinueTIP.Format("%d:%d:%d %s\r\n\r\n", iHour, iMinute, iSecond, strErr);
		m_EditFail.SetSel(-1);
		m_EditFail.ReplaceSel(strContinueTIP);
		strContinueTIP.Empty();
		strErr.Empty();
		return;
	}
	SOCKADDR_IN addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_port = htons(atoi(strPort));
	addrSrv.sin_addr.S_un.S_addr = inet_addr(strIP.GetBuffer());
	//创建套接字  
	SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
	if (SOCKET_ERROR == sockClient)
	{

		strErr.Format("状态:Socket创建失败:%d", WSAGetLastError());
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		//获得系统时间显示发送内容
		CString strContinueTIP;
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strContinueTIP.Format("%d:%d:%d %s\r\n\r\n", iHour, iMinute, iSecond, strErr);
		m_EditFail.SetSel(-1);
		m_EditFail.ReplaceSel(strContinueTIP);
		strContinueTIP.Empty();
		strErr.Empty();
		//MessageBox(strErr, "提示", 0);
		return;
	}
	//向服务器发出连接请求  
	if (connect(sockClient, (struct  sockaddr*)&addrSrv, sizeof(addrSrv)) == INVALID_SOCKET) {
		//printf(”Connect failed : %d”, WSAGetLastError());

		strErr.Format("状态:连接失败:%d", WSAGetLastError());
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		//获得系统时间显示发送内容
		CString strContinueTIP;
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strContinueTIP.Format("%d:%d:%d %s\r\n\r\n", iHour, iMinute, iSecond, strErr);
		m_EditFail.SetSel(-1);
		m_EditFail.ReplaceSel(strContinueTIP);
		strContinueTIP.Empty();
		strErr.Empty();
		//MessageBox(strErr, "提示", 0);
		return;
	}
	else
	{
		strErr.Format("状态:已连接");
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
	}

	//发送数据  
	CString strAdd; //将时间+内容追加到控件中  
	if (m_strContent.IsEmpty())
	{
		closesocket(sockClient);
		WSACleanup();
		return ;
	}
	if (SOCKET_ERROR == send(sockClient, m_strContent, m_strContent.GetLength(), 0))
	{
		strErr.Format("状态:发送失败:%d", WSAGetLastError());
		GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		closesocket(sockClient);
		WSACleanup();
		//获得系统时间显示发送内容
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strAdd.Format("%d:%d:%d  %s (发送失败)\r\n\r\n", iHour, iMinute, iSecond, m_strContent);
		m_EditResult.SetSel(-1);
		m_EditResult.ReplaceSel(strAdd);
		m_EditFail.SetSel(-1);
		m_EditFail.ReplaceSel(strAdd);
		return;
	}
	//获得系统时间显示发送内容
	CTime tm; tm = CTime::GetCurrentTime();
	iHour = tm.GetHour();
	iMinute = tm.GetMinute();
	iSecond = tm.GetSecond();
	strAdd.Format("%d:/%d:/%d  %s (发送成功) \r\n\r\n", iHour, iMinute, iSecond, m_strContent);
	m_EditResult.SetSel(-1);
	m_EditResult.ReplaceSel(strAdd);
	//关闭套接字  
	closesocket(sockClient);
	WSACleanup();
	return;
}


void   CALLBACK  CTCPClientDlg::TimerProc(HWND   hWnd, UINT   nMsg, UINT_PTR   nTimerid, DWORD   dwTime)
{
	m_TCPClient->UpdateData(TRUE);
	CString strAdd; //将时间+内容追加到控件中  
	CString strErr;

	//显示时间的变量
	int iHour;
	int iMinute;
	int iSecond;
	if (m_TCPClient->m_strContent.IsEmpty())
	{
		return;
	}
	//发送时采用长连接
	if (SOCKET_ERROR == send(m_TCPClient->m_SocketClient, m_TCPClient->m_strContent, m_TCPClient->m_strContent.GetLength(), 0))
	{
		strErr.Format("状态:发送失败:%d", WSAGetLastError());
		m_TCPClient->GetDlgItem(IDC_STATIC_BCONNECT)->SetWindowText(strErr);
		closesocket(m_TCPClient->m_SocketClient);
		WSACleanup();
		//获得系统时间显示发送内容
		CTime tm; tm = CTime::GetCurrentTime();
		iHour = tm.GetHour();
		iMinute = tm.GetMinute();
		iSecond = tm.GetSecond();
		strAdd.Format("%d:%d:%d  %s (定时发送失败)\r\n\r\n", iHour, iMinute, iSecond, m_TCPClient->m_strContent);
		m_TCPClient->m_EditResult.SetSel(-1);
		m_TCPClient->m_EditResult.ReplaceSel(strAdd);
		m_TCPClient->m_EditFail.SetSel(-1);
		m_TCPClient->m_EditFail.ReplaceSel(strAdd);
		m_TCPClient->KillTimer(TIMEID);
		CWnd* cwndBtStop = m_TCPClient->GetDlgItem(IDC_BUTTON_STOP);
		cwndBtStop->EnableWindow(FALSE);
		CWnd* cwndBtContinue = m_TCPClient->GetDlgItem(IDC_BUTTON_CON_SEND);
		cwndBtContinue->EnableWindow(TRUE);
		CWnd* cwndBtReContinue = m_TCPClient->GetDlgItem(IDC_BUTTON_RECONNECT);
		cwndBtReContinue->EnableWindow(TRUE);
		return;
	}
	//获得系统时间显示发送内容
	CTime tm; tm = CTime::GetCurrentTime();
	iHour = tm.GetHour();
	iMinute = tm.GetMinute();
	iSecond = tm.GetSecond();
	strAdd.Format("%d:%d:%d  %s (定时发送成功) \r\n\r\n", iHour, iMinute, iSecond, m_TCPClient->m_strContent);
	m_TCPClient->m_EditResult.SetSel(-1);
	m_TCPClient->m_EditResult.ReplaceSel(strAdd);
	//关闭套接字  
}

 

今天的文章简单的TCP客户端发包工具分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26511.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注