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

📄 basesocket.cpp

📁 tcp for test
💻 CPP
字号:
/*
 *  General Information:
 *  TradeMark:	    TestDLL Dynamic Link Library
 *  ProductName:    
 *  
 *  File Name:      BaseSocket.cpp
 *  Author:         cuizhongfa
 *  Project:        test1
 *
 *  Version:        1, 0, 0, 1
 *  Comments:       
 *
 *  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 *  Copyright Notice:
 *
 *  Copyright (c) 2002 Shanghai BaoSight Co., Ltd.
 *
 *  Warning: This computer program is protected by copyright law and 
 *  international treaties.  Unauthorized reproduction or distribution
 *  of this program, or any portion of it, may result in severe civil and
 *  criminal penalties, and will be prosecuted to the maximum extent 
 *  possible under the law. *
 *  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 *  Revision History:
 *
 *  10/31/2002   cuizhongfa   Initial Version
 *  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 */

#include "stdafx.h"
#include "BaseSocket.h"

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

CBaseSocket::CBaseSocket()
{
	m_hSocket = INVALID_SOCKET; //default to an invalid scoket descriptor
}

CBaseSocket::~CBaseSocket()
{
	Close();
}

BOOL CBaseSocket::Create()
{
	m_hSocket = socket(AF_INET, SOCK_STREAM, 0);
	return (m_hSocket != INVALID_SOCKET);
}

BOOL CBaseSocket::Connect(LPCTSTR pszHostAddress, int nPort)
{
	//For correct operation of the T2A macro, see MFC Tech Note 59
	USES_CONVERSION;
	
	//must have been created first
	ASSERT(m_hSocket != INVALID_SOCKET);
	
	LPSTR lpszAscii = T2A((LPTSTR)pszHostAddress);
	
	//Determine if the address is in dotted notation
	SOCKADDR_IN sockAddr;
	ZeroMemory(&sockAddr, sizeof(sockAddr));
	sockAddr.sin_family = AF_INET;
	sockAddr.sin_port = htons((u_short)nPort);
	sockAddr.sin_addr.s_addr = inet_addr(lpszAscii);
	
	//If the address is not dotted notation, then do a DNS 
	//lookup of it.
	if (sockAddr.sin_addr.s_addr == INADDR_NONE)
	{
		LPHOSTENT lphost;
		lphost = gethostbyname(lpszAscii);
		if (lphost != NULL)
			sockAddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
		else
		{
			WSASetLastError(WSAEINVAL); 
			return FALSE;
		}
	}
	
	//Call the protected version which takes an address 
	//in the form of a standard C style struct.
	return Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr));
}

BOOL CBaseSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
	return (connect(m_hSocket, lpSockAddr, nSockAddrLen) != SOCKET_ERROR);
}

BOOL CBaseSocket::Send(LPCTSTR pszBuf, int nBuf)
{
	//must have been created first
	ASSERT(m_hSocket != INVALID_SOCKET);
	
	return (send(m_hSocket, pszBuf, nBuf, 0) != SOCKET_ERROR);
}

int CBaseSocket::Receive(LPSTR pszBuf, int nBuf)
{
	//must have been created first
	ASSERT(m_hSocket != INVALID_SOCKET);
	
	return recv(m_hSocket, pszBuf, nBuf, 0); 
}

void CBaseSocket::Close()
{
	if (m_hSocket != INVALID_SOCKET)
	{
		VERIFY(SOCKET_ERROR != closesocket(m_hSocket));
		m_hSocket = INVALID_SOCKET;
	}
}

BOOL CBaseSocket::IsReadible(BOOL& bReadible)
{
	timeval timeout = {0, 0};
	fd_set fds;
	FD_ZERO(&fds);
	FD_SET(m_hSocket, &fds);
	int nStatus = select(0, &fds, NULL, NULL, &timeout);
	if (nStatus == SOCKET_ERROR)
	{
		return FALSE;
	}
	else
	{
		bReadible = !(nStatus == 0);
		return TRUE;
	}
}

⌨️ 快捷键说明

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