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

📄 netagentthread.cpp

📁 网络转发器 用于运行在可以跨两个网段的机器上用来作为一个TCP应用层的一个桥
💻 CPP
字号:
#include "stdafx.h"
#include "envInit.h"
#include "spIPCComm.h"
using namespace commIPC;
//
SOCKET InitNetworkListen(LPCSTR pszAddr,int iPort)
{
	//准备监听
	int iLocalPort =iPort;
	SOCKET sockListen = SPCreateListenSocket(pszAddr,iLocalPort);
	if(sockListen == SOCKET_ERROR)
	{
		GetConsoleWnd(1)->printf("[error] init network listen socketed");
		return SOCKET_ERROR;
	}
	return sockListen;
}

//网络监听线程
DWORD NetworkListenThread(CAgentIni *pIni)
{
	SOCKET sockListen = InitNetworkListen(NULL,GetAgentIni()->m_iLocalPort);
	if(sockListen == SOCKET_ERROR)
		return 1;
	GetConsoleWnd(0)->printf("[info] 开始等待连接");
	while(1)
	{//开始接受连接
		struct sockaddr adr;
		int iAdrSize= sizeof(adr);
		SOCKET sockClient = accept(sockListen,&adr,&iAdrSize);
		if(sockClient>0)
		{//连接成功后产生处理线程序
			LPCSTR pszRmtAddr = inet_ntoa(((sockaddr_in*)&adr)->sin_addr);
			char szTemp[200];
			sprintf(szTemp,"[info] client connected from %s:%d@%d",pszRmtAddr,ntohs(((sockaddr_in*)&adr)->sin_port),sockClient);
			GetConsoleWnd(0)->printf(szTemp);

			DWORD dwSeqHandle = GetConnectionIDSeq()->GetNext();
			DWORD dwID;
			char szProlog[40];
			sprintf(szProlog,"%s_netsockcontain",GetAgentName());
			CNetSocketContainer *pnsCon= new CNetSocketContainer(sockClient,NULL,(UINT)dwSeqHandle,szProlog);
			HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ClientAndServerDataExchange,(LPVOID)pnsCon,0,&dwID);
			//
			CloseHandle(hThread);
		}
	}
	return 0;
}
void _append_to_file(LPCSTR pszFName,void* pD,int iL)
{//输出到文件
	if(GetAgentIni()->m_iDebugOut != 1)
		return;
	if(iL <=0)
		return;
	FILE* pFile=fopen(pszFName,"ab");
	if(pFile)
	{
		int iWrote = fwrite(pD,iL,1,pFile);
		fclose(pFile);
		GetConsoleWnd(0)->printf("[file] append to %s file write item=%d bytes=%d",pszFName,iWrote,iWrote*iL);
	}
	else
	{
		GetConsoleWnd(1)->printf("[error] append to %s file error",pszFName);
	}
}

DWORD ClientAndServerDataExchange(CNetSocketContainer* pnsContainer)
{//建立到真正服务器间连接,然后采用轮询的方式在C、S间转发数据,每次轮询延时1S
	pnsContainer->AddRefer();
	SOCKET sockClient = pnsContainer->m_sockClient;
	SOCKET sockServer =SPCreateConnectSocket(GetAgentIni()->m_szServerIP,
												GetAgentIni()->m_iServerPort,
												(GetAgentIni()->m_szLocalIP.GetLength()!=0)?(LPCSTR)GetAgentIni()->m_szLocalIP:NULL);
	if(sockServer != SOCKET_ERROR)
	{
		GetConsoleWnd(0)->printf("[info] Connect to Server OK %s:%d",GetAgentIni()->m_szServerIP,GetAgentIni()->m_iServerPort);
		pnsContainer->m_sockServer = sockServer;
		char szTitleCS[32],szTitleSC[32],szFileCS[256],szFileSC[256];
		sprintf(szTitleCS,"{%s} C->S",pnsContainer->GetContainerName());
		sprintf(szTitleSC,"{%s} S->C",pnsContainer->GetContainerName());
		sprintf(szFileCS,"Data\\%d_%s_client_2_server.txt",GetAgentIni()->m_iCurrentSeq,pnsContainer->GetContainerName());
		sprintf(szFileSC,"Data\\%d_%s_server_2_client.txt",GetAgentIni()->m_iCurrentSeq,pnsContainer->GetContainerName());

		CTCPSocket sClient(pnsContainer->m_sockClient,FALSE);
		CTCPSocket sServer(pnsContainer->m_sockServer,FALSE);

		while(1)
		{
			if(SOCKET_ERROR == RecvAndSendBetween2Hosts(pnsContainer,sClient,sServer,szTitleCS,szFileCS))
			{//从客户端接收并发送到服务器
				break;
			}
			if(SOCKET_ERROR == RecvAndSendBetween2Hosts(pnsContainer,sServer,sClient,szTitleSC,szFileSC))
			{//从服务器接收并发送到客户端
				break;
			}
		}
	}
	else
		GetConsoleWnd(1)->printf("[error] Connect to Server error %s:%d",GetAgentIni()->m_szServerIP,GetAgentIni()->m_iServerPort);
	GetConsoleWnd(0)->printf("[info] {%s} Exit",pnsContainer->GetContainerName());
	pnsContainer->CloseAll();
	pnsContainer->ReleaseRefer();
	delete pnsContainer;
	return 0;
}

int RecvAndSendBetween2Hosts(CNetSocketContainer* pnsContainer,CIPCComm &sFrom,CIPCComm &sTo,LPCSTR pszTitle,LPCSTR pszFile)
{//从第一个Socket收数据,发送到第二个Socket
	int iRead=0,iWrote=0;
	//接受数据
	int iRet = sFrom.TestAndRecv(1,pnsContainer->m_iBufferLen,pnsContainer->m_pbBuffer,iRead);
	if(iRet ==SP_ERR_NETWORK || iRet==SP_ERR_GENERAL)
	{
		GetConsoleWnd(1)->printf("[error] %s Receive Error",pszTitle);
		return SOCKET_ERROR;
	}
	if(iRet ==SP_ERR_SUCCESS || iRet==SP_ERR_NOT_FINISH)
	{
		GetConsoleWnd(0)->printf("[info] %s Receive OK length=%d",pszTitle,iRead);
		//记录数据
		_append_to_file(pszFile,pnsContainer->m_pbBuffer,iRead);
		//转发数据
		iRet = sTo.Send(iRead,pnsContainer->m_pbBuffer,iWrote);
		if(iRet ==SP_ERR_NETWORK || iRet==SP_ERR_GENERAL)
		{
			GetConsoleWnd(1)->printf("[error] %s Send Error",pszTitle);
			return SOCKET_ERROR;
		}
		GetConsoleWnd(0)->printf("[info] %s Send OK length=%d",pszTitle,iWrote);
		return iWrote;
	}
//	GetConsoleWnd(0)->printf("%s Send OK length=%d",pszTitle,iWrote);
	return 0;
}

⌨️ 快捷键说明

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