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

📄 socket.cpp

📁 PDA通讯网关服务器源码程序
💻 CPP
字号:
// Socket.cpp: implementation of the CSafeSocket class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GateAgent.h"
#include "Socket.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSafeSocket::CSafeSocket()
{

}

CSafeSocket::~CSafeSocket()
{

}

int CSafeSocket::OnStartConnect(int iPort,const char*pchIp)
{
	try
	{
		struct sockaddr_in ConnectOtherSafeProxyAddr;
		m_localClientSocket = socket(AF_INET,SOCK_STREAM,0);
		if(m_localClientSocket == INVALID_SOCKET)
		{
			return -1;
		}
		
		hostent *pHost=gethostbyname(pchIp);
		//connect address
		memset((void *)&ConnectOtherSafeProxyAddr, 0, sizeof(ConnectOtherSafeProxyAddr));
		ConnectOtherSafeProxyAddr.sin_family = AF_INET;
		ConnectOtherSafeProxyAddr.sin_port = htons(iPort); //port which  to connect the other proxy
	//	ConnectOtherSafeProxyAddr.sin_addr.s_addr =inet_addr(pchIp);//("10.62.179.114");//IP of the other proxy
		ConnectOtherSafeProxyAddr.sin_addr.s_addr=*((unsigned long*)pHost->h_addr_list[0]);
		if (connect(m_localClientSocket, (struct sockaddr *)&ConnectOtherSafeProxyAddr, sizeof(ConnectOtherSafeProxyAddr)) == SOCKET_ERROR)
		{
			closesocket(m_localClientSocket);
			return -1;
		}
	}
	catch(...)
	{
			closesocket(m_localClientSocket);
			return -1;
	}
	return 0;
}

//接收数据然后比较是否为安全用户
int CSafeSocket::OnRecvSafeLogin(char *pchValid)
{
	char chRecvSafe[500]={0};
	int  iRecvLen=-1;
	if ((iRecvLen = recv(m_localClientSocket, chRecvSafe, sizeof(chRecvSafe), 0)) <= 0)   //超时未收到数据。
	{//
		closesocket(m_localClientSocket);
		return -1;
	}	

	char *pDest=strstr(chRecvSafe,pchValid);
	if (pDest==NULL)
	{
		closesocket(m_localClientSocket);
		return -1;
	}
	closesocket(m_localClientSocket);
	return 0;
}

⌨️ 快捷键说明

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