📄 hwsocket.cpp
字号:
or a dotted number such as “128.56.22.8”.
nHostPort - The port identifying the socket application.
RETURN :
NNonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError.
*/
BOOL CHwSocket::Connect(LPCTSTR lpszHostAddress, UINT nHostPort)
{
USES_CONVERSION;
ASSERT(lpszHostAddress != NULL);
SOCKADDR_IN sockAddr;
memset(&sockAddr,0,sizeof(sockAddr));
LPSTR lpszAscii = T2A((LPTSTR)lpszHostAddress);
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr(lpszAscii);
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;
}
}
sockAddr.sin_port = htons((u_short)nHostPort);
return Connect((SOCKADDR*)&sockAddr, sizeof(sockAddr));
}
/*
Establishes a connection to a peer socket.
PARAMETER :
lpSockAddr - A pointer to a SOCKADDR structure that contains the address of the connected socket.
nSockAddrLen - The length of the address in lpSockAddr in bytes.
RETURN :
NNonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError.
*/
BOOL CHwSocket::Connect(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
return ConnectHelper(lpSockAddr, nSockAddrLen);
}
BOOL CHwSocket::ConnectHelper(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return FALSE;
}
return connect(m_hSocket, lpSockAddr, nSockAddrLen) != SOCKET_ERROR;
}
/*
Receives data from the socket.
PARAMETER :
lpBuf - A buffer for the incoming data.
nBufLen - The length of lpBuf in bytes.
nFlags - Specifies the way in which the call is made. The semantics of this function are determined by the socket options and
the nFlags parameter. The latter is constructed by combining any of the following values with the C++ OR operator:
MSG_PEEK Peek at the incoming data. The data is copied into the buffer but is not removed from the input queue.
MSG_OOB Process out-of-band data (seeWindows Sockets Programming Considerations in the Win32 SDK documentation for a discussion of this topic).
RETURN :
If no error occurs, Receive returns the number of bytes received. If the connection has been closed, it returns 0. Otherwise,
a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling GetLastError.
*/
int CHwSocket::Receive(void* lpBuf, int nBufLen, int nFlags)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return SOCKET_ERROR;
}
return recv(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags);
}
/*
Receives a datagram and stores the source address.
PARAMETER :
lpBuf - A buffer for the incoming data.
nBufLen - The length of lpBuf in bytes.
rSocketAddress - Reference to a CString object that receives a dotted number IP address.
rSocketPort - Reference to a UINT that stores a port.
lpSockAddr - A pointer to a SOCKADDR structure that holds the source address upon return.
lpSockAddrLen - A pointer to the length of the source address in lpSockAddr in bytes.
nFlags - Specifies the way in which the call is made. The semantics of this function are determined by the socket options and
the nFlags parameter. The latter is constructed by combining any of the following values with the C++ OR operator:
MSG_PEEK Peek at the incoming data. The data is copied into the buffer but is not removed from the input queue.
MSG_OOB Process out-of-band data (seeWindows Sockets Programming Considerations in the Win32 SDK documentation for a discussion of this topic).
RETURN :
If no error occurs, ReceiveFrom returns the number of bytes received. If the connection has been closed, it returns 0. Otherwise, a value of SOCKET_ERROR
is returned, and a specific error code can be retrieved by calling GetLastError.
*/
int CHwSocket::ReceiveFrom(void* lpBuf, int nBufLen, CString& rSocketAddress, UINT& rSocketPort, int nFlags)
{
SOCKADDR_IN sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
int nSockAddrLen = sizeof(sockAddr);
int nResult = ReceiveFrom(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, &nSockAddrLen, nFlags);
if(nResult != SOCKET_ERROR)
{
rSocketPort = ntohs(sockAddr.sin_port);
rSocketAddress = inet_ntoa(sockAddr.sin_addr);
}
return nResult;
}
int CHwSocket::ReceiveFrom(void* lpBuf, int nBufLen, SOCKADDR* lpSockAddr, int* lpSockAddrLen, int nFlags)
{
return ReceiveFromHelper(lpBuf, nBufLen, lpSockAddr, lpSockAddrLen, nFlags);
}
int CHwSocket::ReceiveFromHelper(void* lpBuf, int nBufLen, SOCKADDR* lpSockAddr, int* lpSockAddrLen, int nFlags)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return SOCKET_ERROR;
}
return recvfrom(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags, lpSockAddr, lpSockAddrLen);
}
/*
Sends data to a connected socket
PARAMETER :
lpBuf - A buffer containing the data to be transmitted.
nBufLen - The length of the data in lpBuf in bytes.
nFlags - Specifies the way in which the call is made. The semantics of this function are determined by the socket options and
the nFlags parameter. The latter is constructed by combining any of the following values with the C++ OR operator:
MSG_PEEK Peek at the incoming data. The data is copied into the buffer but is not removed from the input queue.
MSG_OOB Process out-of-band data (seeWindows Sockets Programming Considerations in the Win32 SDK documentation for a discussion of this topic).
RETURN :
If no error occurs, Send returns the total number of characters sent. (Note that this can be less than the number indicated by nBufLen.) Otherwise, a
value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling GetLastError.
*/
int CHwSocket::Send(const void* lpBuf, int nBufLen, int nFlags)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return SOCKET_ERROR;
}
return send(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags);
}
/*
Sends data to a specific destination.
PARAMETER :
lpBuf - A buffer containing the data to be transmitted.
nBufLen - The length of the data in lpBuf in bytes.
nHostPort - The port identifying the socket application.
lpszHostAddress - The network address of the socket to which this object is connected: a machine name such as “ftp.microsoft.com,” or a dotted number such as “128.56.22.8”.
nFlags - Specifies the way in which the call is made. The semantics of this function are determined by the socket options and
the nFlags parameter. The latter is constructed by combining any of the following values with the C++ OR operator:
MSG_PEEK Peek at the incoming data. The data is copied into the buffer but is not removed from the input queue.
MSG_OOB Process out-of-band data (seeWindows Sockets Programming Considerations in the Win32 SDK documentation for a discussion of this topic).
lpSockAddr - A pointer to a SOCKADDR structure that contains the address of the target socket.
nSockAddrLen - The length of the address in lpSockAddr in bytes.
RETURN :
If no error occurs, SendTo returns the total number of characters sent. (Note that this can be less than the number indicated by nBufLen.) Otherwise,
a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling GetLastError
*/
int CHwSocket::SendTo(const void* lpBuf, int nBufLen, UINT nHostPort, LPCTSTR lpszHostAddress, int nFlags)
{
USES_CONVERSION;
SOCKADDR_IN sockAddr;
memset(&sockAddr,0,sizeof(sockAddr));
LPSTR lpszAscii = T2A((LPTSTR)lpszHostAddress);
sockAddr.sin_family = AF_INET;
if (lpszAscii == NULL)
sockAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
else
{
sockAddr.sin_addr.s_addr = inet_addr(lpszAscii);
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 SOCKET_ERROR;
}
}
}
sockAddr.sin_port = htons((u_short)nHostPort);
return SendTo(lpBuf, nBufLen, (SOCKADDR*)&sockAddr, sizeof(sockAddr), nFlags);
}
int CHwSocket::SendTo(const void* lpBuf, int nBufLen, const SOCKADDR* lpSockAddr, int nSockAddrLen, int nFlags)
{
return SendToHelper(lpBuf, nBufLen, lpSockAddr, nSockAddrLen, nFlags);
}
int CHwSocket::SendToHelper(const void* lpBuf, int nBufLen, const SOCKADDR* lpSockAddr, int nSockAddrLen, int nFlags)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return SOCKET_ERROR;
}
return sendto(m_hSocket, (LPSTR)lpBuf, nBufLen, nFlags, lpSockAddr, nSockAddrLen);
}
/*
Disables Send and/or Receive calls on the socket.
PARAMETER :
nHow - A flag that describes what types of operation will no longer be allowed, using the following enumerated values:
receives = 0
sends = 1
both = 2
RETURN :
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError
*/
BOOL CHwSocket::ShutDown(int nHow)
{
return (SOCKET_ERROR != shutdown(m_hSocket,nHow));
}
/*
Establishes a socket to listen for incoming connection requests.
PARAMETER :
nConnectionBacklog - The maximum length to which the queue of pending connections can grow. Valid range is from 1 to 5.
RETURN :
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError.
*/
BOOL CHwSocket::Listen(int nConnectionBacklog)
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return FALSE;
}
return (SOCKET_ERROR != listen(m_hSocket, nConnectionBacklog));
}
/*
Destructor
*/
CHwSocket::~CHwSocket()
{
Close();
}
/*
将套接字设置为广播类型的,m_nSocketType 必须等于 SOCK_DGRAM,即UDP类型
*/
BOOL CHwSocket::SetAsBroadcastSocket ()
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return FALSE;
}
BOOL optval;
if ( setsockopt ( m_hSocket, SOL_SOCKET, SO_BROADCAST,
(char FAR *)&optval, sizeof(optval) ) == SOCKET_ERROR )
{
Close ();
return FALSE;
}
return TRUE;
}
/*
设置非阻塞模式
*/
BOOL CHwSocket::SetNonblocking ()
{
if ( m_hSocket == INVALID_SOCKET || m_hSocket == 0 )
{
return FALSE;
}
long cmd = FIONBIO;
int err = ioctlsocket ( m_hSocket, cmd, (u_long*)&cmd );
if ( err != 0 )
{
return FALSE;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -