1. 程式人生 > >簡單的TCP客戶端發包工具

簡單的TCP客戶端發包工具

一、TCP介紹

先放這裡有時間在寫,最近在寫DuiLib相關的使用內容,這部分大家湊活著看

二、程式截圖

下載連結

連結: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);
	//關閉套接字  
}