📄 hwsocket.cpp
字号:
#include "StdAfx.h"
#include "HwSocket.h"
CString GetSocketAddressAndPort(CHwSocket &sock, BOOL bPeer/*=TRUE*/, CString *pcsAddress/*=NULL*/, USHORT *pnPort/*=NULL*/)
{
if ( !HANDLE_IS_VALID(sock.m_hSocket) ) return _T("");
CString csAddress;
UINT nPort;
if ( bPeer )
{
if ( !sock.GetPeerName ( csAddress, nPort ) )
return _T("");
}
else
{
if ( !sock.GetSockName ( csAddress, nPort ) )
return _T("");
}
if ( pcsAddress ) *pcsAddress = csAddress;
if ( pnPort ) *pnPort = nPort;
CString csAddressAndPort;
csAddressAndPort.Format ( _T("%s:%d"), csAddress, nPort );
return csAddressAndPort;
}
/////////////////////////////////////////////////////////////////////////////
// CHwSocket
CHwSocket::CHwSocket()
: m_hSocket ( INVALID_SOCKET )
{
}
/*
Create a SOCKET
PARAMETER :
nSocketPort - A well-known port to be used with the socket, or 0 if you want Windows Sockets to select a port.
nSocketType - SOCK_STREAM or SOCK_DGRAM.
lpszSockAddress - A pointer to a string containing the network address of the connected socket, a dotted number such as "128.56.22.8".
*/
BOOL CHwSocket::Create(UINT nSocketPort, int nSocketType, LPCTSTR lpszSocketAddress )
{
ASSERT ( m_hSocket == INVALID_SOCKET );
if (Socket(nSocketType))
{
if (Bind(nSocketPort,lpszSocketAddress))
return TRUE;
int nResult = GetLastError();
Close();
WSASetLastError(nResult);
}
return FALSE;
}
/*
Generate a SOCKET
*/
BOOL CHwSocket::Socket(int nSocketType, int nProtocolType, int nAddressFormat)
{
ASSERT(m_hSocket == INVALID_SOCKET);
m_hSocket = socket(nAddressFormat,nSocketType,nProtocolType);
if (m_hSocket != INVALID_SOCKET) return TRUE;
return FALSE;
}
/*
Bind a SOCKET with port and address
PARAMETER :
nSocketPort - The port identifying the socket application.
lpszSocketAddress - The network address, a dotted number such as “128.56.22.8”.
RETURN :
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by
calling GetLastError. The following errors apply to this member function:
*/
BOOL CHwSocket::Bind(UINT nSocketPort, LPCTSTR lpszSocketAddress)
{
USES_CONVERSION;
SOCKADDR_IN sockAddr;
memset(&sockAddr,0,sizeof(sockAddr));
LPSTR lpszAscii = T2A((LPTSTR)lpszSocketAddress);
sockAddr.sin_family = AF_INET;
if (lpszAscii == NULL)
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
else
{
DWORD lResult = inet_addr(lpszAscii);
if (lResult == INADDR_NONE)
{
WSASetLastError(WSAEINVAL);
return FALSE;
}
sockAddr.sin_addr.s_addr = lResult;
}
sockAddr.sin_port = htons((u_short)nSocketPort);
return Bind((SOCKADDR*)&sockAddr, sizeof(sockAddr));
}
/*
Bind a SOCKET with port and address
PARAMETER :
lpSockAddr - A pointer to a SOCKADDR structure that contains the address to assign to this socket.
nSockAddrLen - The length of the address in lpSockAddr in bytes.
RETURN :
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by
calling GetLastError. The following errors apply to this member function:
*/
BOOL CHwSocket::Bind(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return FALSE;
}
return (SOCKET_ERROR != bind(m_hSocket, lpSockAddr, nSockAddrLen));
}
/*
Attaches a socket handle to a CHwSocket object.
PARAMETER :
hSocket - Contains a handle to a socket.
RETURN :
Nonzero if the function is successful.
*/
void CHwSocket::Attach(SOCKET hSocket)
{
ASSERT ( hSocket != INVALID_SOCKET );
ASSERT ( m_hSocket == INVALID_SOCKET );
m_hSocket = hSocket;
}
/*
Call this member function to detach the SOCKET handle in the m_hSocket data member from the CHwSocket object and set m_hSocket to NULL.
PARAMETER :
hSocket - Contains a handle to a socket.
RETURN :
Nonzero if the function is successful.
*/
SOCKET CHwSocket::Detach()
{
SOCKET hSocket = m_hSocket;
m_hSocket = INVALID_SOCKET;
return hSocket;
}
/*
Gets the address of the peer socket to which the socket is connected.
PARAMETER :
rPeerAddress - Reference to a CString object that receives a dotted number IP address.
rPeerPort - Reference to a UINT that stores a port.
RETURN :
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError.
*/
BOOL CHwSocket::GetPeerName(CString& rPeerAddress, UINT& rPeerPort)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return FALSE;
}
SOCKADDR_IN sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
int nSockAddrLen = sizeof(sockAddr);
BOOL bResult = GetPeerName((SOCKADDR*)&sockAddr, &nSockAddrLen);
if (bResult)
{
rPeerPort = ntohs(sockAddr.sin_port);
rPeerAddress = inet_ntoa(sockAddr.sin_addr);
}
return bResult;
}
/*
Gets the address of the peer socket to which the socket is connected.
PARAMETER :
lpSockAddr - A pointer to the SOCKADDR structure that receives the name of the peer socket.
lpSockAddrLen - A pointer to the length of the address in lpSockAddr in bytes. On return, the lpSockAddrLen argument contains the actual size of lpSockAddr returned in bytes.
RETURN :
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError.
*/
BOOL CHwSocket::GetPeerName(SOCKADDR* lpSockAddr, int* lpSockAddrLen)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return FALSE;
}
return (SOCKET_ERROR != getpeername(m_hSocket, lpSockAddr, lpSockAddrLen));
}
/*
Gets the local name for a socket.
PARAMETER :
rSocketAddress - Reference to a CString object that receives a dotted number IP address
rSocketPort - Reference to a UINT that stores a port.
RETURN :
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError.
*/
BOOL CHwSocket::GetSockName(CString& rSocketAddress, UINT& rSocketPort)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return FALSE;
}
SOCKADDR_IN sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
int nSockAddrLen = sizeof(sockAddr);
BOOL bResult = GetSockName((SOCKADDR*)&sockAddr, &nSockAddrLen);
if (bResult)
{
rSocketPort = ntohs(sockAddr.sin_port);
rSocketAddress = inet_ntoa(sockAddr.sin_addr);
}
return bResult;
}
/*
Gets the local name for a socket.
PARAMETER :
lpSockAddr - A pointer to a SOCKADDR structure that receives the address of the socket.
lpSockAddrLen - A pointer to the length of the address in lpSockAddr in bytes.
RETURN :
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError.
*/
BOOL CHwSocket::GetSockName(SOCKADDR* lpSockAddr, int* lpSockAddrLen)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return FALSE;
}
return (SOCKET_ERROR != getsockname(m_hSocket, lpSockAddr, lpSockAddrLen));
}
/////////////////////////////////////////////////////////////////////////////
// CAscynSocket Operations
/*
Accepts a connection on the socket.
PARAMETER :
rConnectedSocket - A reference identifying a new socket that is available for connection.
lpSockAddr - A pointer to a SOCKADDR structure that receives the address of the connecting socket, as known on the network.
The exact format of the lpSockAddr argument is determined by the address family established when the socket was created.
If lpSockAddr and/or lpSockAddrLen are equal to NULL, then no information about the remote address of the accepted
socket is returned.
lpSockAddrLen - A pointer to the length of the address in lpSockAddr in bytes. The lpSockAddrLen is a value-result parameter:
it should initially contain the amount of space pointed to by lpSockAddr; on return it will contain the actual length
(in bytes) of the address returned.
RETURN :
NNonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError.
*/
BOOL CHwSocket::Accept(CHwSocket& rConnectedSocket,SOCKADDR* lpSockAddr, int* lpSockAddrLen)
{
ASSERT(rConnectedSocket.m_hSocket == INVALID_SOCKET);
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return FALSE;
}
SOCKET hTemp = accept(m_hSocket, lpSockAddr, lpSockAddrLen);
if (hTemp == INVALID_SOCKET)
{
DWORD dwProblem = GetLastError();
SetLastError(dwProblem);
return FALSE;
}
rConnectedSocket.Attach ( hTemp );
return TRUE;
}
/*
Closes the socket.
*/
void CHwSocket::Close()
{
if (m_hSocket != INVALID_SOCKET)
{
VERIFY(SOCKET_ERROR != closesocket(m_hSocket));
m_hSocket = INVALID_SOCKET;
}
}
/*
Establishes a connection to a peer socket.
PARAMETER :
lpszHostAddress - The network address of the socket to which this object is connected: a machine name such as “ftp.microsoft.com”,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -