⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpsocket.cpp

📁 串口转TCPIP 实用程序,很实用的代码和code,我们可以学习串口和tcp ip的原理
💻 CPP
字号:
// TcpSocket.cpp : implementation file
//

#include "stdafx.h"
#include "SerialTcp.h"
#include "TcpSocket.h"
#include "Item.h"
#include "SerialTcpDlg.h"


// CTcpSocket
CTcpSocket::CTcpSocket()
{
	m_pParentItem = 0;
}

CTcpSocket::~CTcpSocket()
{
}


// CTcpSocket member functions

void CTcpSocket::OnSend(int nErrorCode)
{

	CAsyncSocket::OnSend(nErrorCode);
}

void CTcpSocket::OnAccept(int nErrorCode)
{
	switch(m_iKind)
	{
	case 1:
		break;
	case 2://Data_Listen
		if(!m_pParentItem->m_dataSockInUse)
		{
			sockaddr_in addr;
			int size = sizeof(sockaddr);	
			int bResult = Accept(m_pParentItem->m_sockDateMain, (sockaddr*)&addr, &size);
			m_pParentItem->m_dataSockInUse = 1;
			m_pParentItem->m_strDataLinkTo.Format("%s",inet_ntoa(addr.sin_addr));

			//写入日志
			CTime timer_1;
			timer_1 = CTime::GetCurrentTime();
			CString s=timer_1.Format("%H:%M:%S");

			char bufTemp1[512];
			memset(bufTemp1, 0, 512);
			sprintf_s(bufTemp1, 512, "%s  %s Connect To Port %d\r\n", s, m_pParentItem->m_strDataLinkTo, m_pParentItem->m_iDataPort);

			CString temp = bufTemp1;
 
			if (m_pParentItem->m_logFlag)
			{
				FILE* stream = fopen(m_pParentItem->m_strLogFileName, "a+");
				fwrite(temp, 1, temp.GetLength(), stream);
				fclose(stream);
			}
		}
		else//Data_Main already in use
		{
			sockaddr_in addr;
			int size = sizeof(sockaddr);	
			int bResult = Accept(m_pParentItem->m_sockTemp, (sockaddr*)&addr, &size);
			m_pParentItem->m_sockTemp.Close();			
		}
		break;
	case 3:
		break;
	case 4://Debug_Listen
		if(!m_pParentItem->m_debugSockInUse)
		{
			sockaddr_in addr;
			int size = sizeof(sockaddr);	
			int bResult = Accept(m_pParentItem->m_sockDebugMain, (sockaddr*)&addr, &size);
			m_pParentItem->m_debugSockInUse = 1;
			m_pParentItem->m_strDebugLinkTo.Format("%s",inet_ntoa(addr.sin_addr));

			CTime timer_1;
			timer_1 = CTime::GetCurrentTime();
			CString s=timer_1.Format("%H:%M:%S");

			char bufTemp1[512];
			memset(bufTemp1, 0, 512);
			sprintf_s(bufTemp1, 512, "%s  %s Connect To Port %d\r\n", s, m_pParentItem->m_strDebugLinkTo, m_pParentItem->m_iDebugPort);

			CString temp = bufTemp1;

			if (m_pParentItem->m_logFlag)
			{
				FILE* stream = fopen(m_pParentItem->m_strLogFileName, "a+");
				fwrite(temp, 1, temp.GetLength(), stream);
				fclose(stream);
			}

		}
		else
		{
			sockaddr_in addr;
			int size = sizeof(sockaddr);	
			int bResult = Accept(m_pParentItem->m_sockTemp, (sockaddr*)&addr, &size);
			m_pParentItem->m_sockTemp.Close();
		}
		break;
	default:
		break;
	}

	CAsyncSocket::OnAccept(nErrorCode);
}

void CTcpSocket::OnReceive(int nErrorCode)
{
	if(m_iKind == 1)
	{

		BYTE buf[512];
		memset(buf, 0, 512);
		int nRead = Receive(buf, 512);


		//*************** 写入 COM 口
		memcpy(m_pParentItem->m_serialPort.m_pWriteBuffer, buf, nRead);
		m_pParentItem->m_serialPort.WriteToPort(buf, nRead);
		//m_pParentItem->m_pParent->SetErrTimer(m_pParentItem->m_iSerialPort);

		//*************** 发送到调试口

		char bufTemp[512];
		memset(bufTemp, 0, 512);
		sprintf_s(bufTemp, 512, "In <— %s", buf);
		m_pParentItem->m_sockDebugMain.Send(bufTemp, nRead+7);

		
		//************* 写入日志
		memset(bufTemp, 0, 512);
		sprintf_s(bufTemp, 512, "COM%d <= TCP %2d]", m_pParentItem->m_serialPort.m_nPortNr, nRead);

		CTime timer_1;
		timer_1 = CTime::GetCurrentTime();
		CString s=timer_1.Format("[%H:%M:%S");

		char bufTemp1[512];
		memset(bufTemp1, 0, 512);
		sprintf_s(bufTemp1, 512, "%s %s", s, bufTemp);

		CString temp = bufTemp1;

		if (m_pParentItem->m_logFlag)
		{
			FILE* stream = fopen(m_pParentItem->m_strLogFileName, "a+");
			fwrite(temp, 1, temp.GetLength(), stream);
			fclose(stream);

			char buf1[5];
			memset(buf1,0,5);

			if (nRead>16)
			{
				nRead = 16;
			}

			for (int i=0; i<nRead; i++)
			{
				BYTE hex;
				hex = buf[i];
				sprintf(buf1, " %02X", hex);

				stream = fopen(m_pParentItem->m_strLogFileName, "a+");
				fwrite(buf1, 1, 3, stream);
				fclose(stream);
			}
			
			for(int i=0; i<16-nRead; i++)
			{
				stream = fopen(m_pParentItem->m_strLogFileName, "a+");
				fwrite("   ", 1, 3, stream);
				fclose(stream);
			}

			stream = fopen(m_pParentItem->m_strLogFileName, "a+");
			fwrite(" [", 1, 2, stream);
			for (int i=0; i<nRead; i++)
			{
				char temp12 = buf[i];
				if (temp12>= '0' && temp12 <= '9')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12>= 'A' && temp12 <='Z')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12>= 'a' && temp12 <= 'z')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == ' ')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == '@')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == '#')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == '$')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == '%')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == '^')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == '&')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == '*')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == '(')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == ')')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else if (temp12 == '=')
				{
					fwrite(&temp12, 1, 1, stream);
				}
				else
				{
					fwrite(".", 1, 1, stream);
				}
			}
			fwrite("]", 1, 1, stream);
			fwrite("\r\n", 1, 2, stream);
			fclose(stream);
		}

		//*************

		//**************** 增加系统收到字节
		m_pParentItem->m_iTotalRecvDate += nRead;
	}

	CAsyncSocket::OnReceive(nErrorCode);
}

void CTcpSocket::OnClose(int nErrorCode)
{
	// TODO: Add your specialized code here and/or call the base class
	Close();	//???????
	//1: data_main   3: debug_main    2:date_listen    4:debug_listen    5: temp
	CTime timer_1;
	char bufTemp1[512];
	CString temp;
	CString s;

	switch(m_iKind)
	{
	case 1:

		//************************************
		timer_1 = CTime::GetCurrentTime();
		s=timer_1.Format("%H:%M:%S");

		memset(bufTemp1, 0, 512);
		sprintf_s(bufTemp1, 512, "%s  %s Disconnect From Port %d\r\n", s, m_pParentItem->m_strDataLinkTo, m_pParentItem->m_iDataPort);

		temp = bufTemp1;

		if (m_pParentItem->m_logFlag)
		{
			FILE* stream = fopen(m_pParentItem->m_strLogFileName, "a+");
			fwrite(temp, 1, temp.GetLength(), stream);
			fclose(stream);
		}

		m_pParentItem->m_dataSockInUse = 0;
		m_pParentItem->m_strDataLinkTo = "";

		break;
	case 2:
		break;
	case 3:

		timer_1 = CTime::GetCurrentTime();
		s=timer_1.Format("%H:%M:%S");

		memset(bufTemp1, 0, 512);
		sprintf_s(bufTemp1, 512, "%s  %s Disconnect From Port %d\r\n", s, m_pParentItem->m_strDebugLinkTo, m_pParentItem->m_iDebugPort);

		temp = bufTemp1;

		if (m_pParentItem->m_logFlag)
		{
			FILE* stream = fopen(m_pParentItem->m_strLogFileName, "a+");
			fwrite(temp, 1, temp.GetLength(), stream);
			fclose(stream);
		} 

		m_pParentItem->m_debugSockInUse = 0;
		m_pParentItem->m_strDebugLinkTo = "";
		break;
	case 4:
		break;
	default:
		break;
	}
	CAsyncSocket::OnClose(nErrorCode);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -