| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- /**************************************************************************************************
- 版本所有(C), 2020-2022, 北京普达迪泰
- ***************************************************************************************************
- 文 件 名: XSocket.cpp
- 功能描述: 封闭网络套接字通信
- 版 本 号: 1.00初版
- 作 者: HYF
- 生成日期; 2021-12-20
- 最近修改:
- 函数列表:
- 修改历史:
- 1.日 期 :2021-12-20
- 作 者 : HYF
- 修改内容 : 创建文件
- **************************************************************************************************/
- #include "pch.h"
- #include <Ws2tcpip.h>
- #include "XSocket.h"
- CXSocket::CXSocket(void)
- {
- m_hSocket = INVALID_SOCKET;
- }
- CXSocket::CXSocket(SOCKET hSocket)
- :m_hSocket(hSocket)
- {
- }
- CXSocket::~CXSocket(void)
- {
- Close();
- }
- bool CXSocket::EventSelect()
- {
- /////////////////////////////////////////////////////////////////////////////
- //event select
- if (WSAEventSelect(m_hSocket, m_eventNet.GetEvent(), FD_READ | FD_WRITE | FD_CLOSE) != 0)
- {
- m_nLastError = WSAGetLastError();
- return false;
- }
- return true;
- }
- int CXSocket::GetLastError()
- {
- return m_nLastError;
- }
- bool CXSocket::Init()
- {
- WORD wVersionRequested;
- WSADATA wsaData;
- wVersionRequested = MAKEWORD(2, 2);
- int nErr = 0;
- m_nLastError = 0;
- if ((nErr = WSAStartup(wVersionRequested, &wsaData)) != 0)
- {
- m_nLastError = WSAGetLastError();
- return false;
- }
- if (LOBYTE(wsaData.wVersion) != 2 ||
- HIBYTE(wsaData.wVersion) != 2)
- {
- WSACleanup();
- return false;
- }
- return true;
- }
- void CXSocket::Uninit()
- {
- WSACleanup();
- }
- void CXSocket::Close()
- {
- if (m_hSocket != INVALID_SOCKET)
- {
- closesocket(m_hSocket);
- m_hSocket = INVALID_SOCKET;
- }
- }
- void CXSocket::Abort()
- {
- m_eventStop.Set();
- }
- bool CXSocket::Connect(const char *pAddr, unsigned short uPort)
- {
- m_nLastError = 0;
- //Create
- if ((m_hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
- {
- m_nLastError = WSAGetLastError();
- return false;
- }
- /////////////////////////////////////////////////////////////////////////////
- sockaddr_in sa;
- addrinfo aiHints;
- addrinfo *aiList = NULL;
- memset(&aiHints, 0, sizeof(aiHints));
- aiHints.ai_family = AF_INET;
- aiHints.ai_socktype = SOCK_STREAM;
- aiHints.ai_protocol = IPPROTO_TCP;
- if (getaddrinfo(pAddr, NULL, &aiHints, &aiList) != 0)
- {
- m_nLastError = WSAGetLastError();
- closesocket(m_hSocket);
- m_hSocket = INVALID_SOCKET;
- return false;
- }
- sa = *((sockaddr_in*)aiList->ai_addr);
- sa.sin_port = htons(uPort);
- /////////////////////////////////////////////////////////////////////////////
- //connect
- if (connect(m_hSocket, (SOCKADDR*)&sa, sizeof(sa)) == SOCKET_ERROR)
- {
- m_nLastError = WSAGetLastError();
- closesocket(m_hSocket);
- m_hSocket = INVALID_SOCKET;
- return false;
- }
- /////////////////////////////////////////////////////////////////////////////
- //event select
- if (WSAEventSelect(m_hSocket, m_eventNet.GetEvent(), FD_READ | FD_WRITE | FD_CLOSE) != 0)
- {
- m_nLastError = WSAGetLastError();
- return false;
- }
- return true;
- }
- bool CXSocket::QConnect(DWORD dwip, unsigned short uPort)
- {
- m_nLastError = 0;
- if ((m_hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
- {
- m_nLastError = WSAGetLastError();
- return false;
- }
- /////////////////////////////////////////////////////////////////////////////
- sockaddr_in sa;
- sa.sin_family = AF_INET;
- sa.sin_addr.S_un.S_addr = htonl(dwip);
- sa.sin_port = htons(uPort);
- /////////////////////////////////////////////////////////////////////////////
- //connect
- if (connect(m_hSocket, (SOCKADDR*)&sa, sizeof(sa)) == SOCKET_ERROR)
- {
- m_nLastError = WSAGetLastError();
- closesocket(m_hSocket);
- m_hSocket = INVALID_SOCKET;
- return false;
- }
- /////////////////////////////////////////////////////////////////////////////
- //event select
- if (WSAEventSelect(m_hSocket, m_eventNet.GetEvent(), FD_READ | FD_WRITE | FD_CLOSE) != 0)
- {
- m_nLastError = WSAGetLastError();
- return false;
- }
- /////////////////////////////////////////////////////////////////////////////
- return true;
- }
- int CXSocket::Send(const char* pBuff, int nLen, int& nLenSent, DWORD dwTimeOut)
- {
- _BEGIN:
- int nRet = 0;
- m_nLastError = 0;
- if ((nRet = send(m_hSocket, pBuff, nLen, 0)) > 0 || (m_nLastError = WSAGetLastError()) != WSAEWOULDBLOCK)
- {
- nLenSent = nRet;
- return E_XSOCKET_SUCCESS;
- }
- ///////////////////////////////////////////////////////////////////////////////////
- HANDLE arrHandles[2];
- arrHandles[0] = m_eventNet.GetEvent();
- arrHandles[1] = m_eventStop.GetEvent();
- DWORD dwWaitRes = WaitForMultipleObjects(2, arrHandles, FALSE, dwTimeOut);
- if (dwWaitRes == WAIT_OBJECT_0 + 1)
- return E_XSOCKET_ABORTED;
- else if (dwWaitRes != WAIT_OBJECT_0)
- return E_XSOCKET_TIMEDOUT;
- //////////////////////////////////////////////////////////////////////////////////
- WSANETWORKEVENTS myNetEvents;
- if (WSAEnumNetworkEvents(m_hSocket, m_eventNet.GetEvent(), &myNetEvents) != 0)
- {
- m_nLastError = WSAGetLastError();
- return E_XSOCKET_SOCKERR;
- }
- if ((myNetEvents.lNetworkEvents & FD_WRITE) != FD_WRITE)
- {
- goto _BEGIN;
- }
- if (myNetEvents.iErrorCode[FD_WRITE_BIT] != 0)
- return E_XSOCKET_SOCKERR;
- /////////////////////////////////////////////////////////////////////////////////////
- nLenSent = send(m_hSocket, pBuff, nLen, 0);
- return E_XSOCKET_SUCCESS;
- }
- int CXSocket::Recv(char* pBuff, int nLen, int& nLenReceived, DWORD dwTimeOut)
- {
- _BEGIN:
- ///////////////////////////////////////////////////////////////////////////////////
- HANDLE arrHandles[2];
- arrHandles[0] = m_eventNet.GetEvent();
- arrHandles[1] = m_eventStop.GetEvent();
- DWORD dwWaitRes = WaitForMultipleObjects(2, arrHandles, FALSE, dwTimeOut);
- if (dwWaitRes == WAIT_OBJECT_0 + 1)
- return E_XSOCKET_ABORTED;
- else if (dwWaitRes != WAIT_OBJECT_0)
- return E_XSOCKET_TIMEDOUT;
- ////////////////////////////////////////////////////////////////////////////////////
- WSANETWORKEVENTS myNetEvents;
- if (WSAEnumNetworkEvents(m_hSocket, m_eventNet.GetEvent(), &myNetEvents) != 0)
- {
- m_nLastError = WSAGetLastError();
- return E_XSOCKET_SOCKERR;
- }
- if ((myNetEvents.lNetworkEvents & FD_READ) != FD_READ)
- goto _BEGIN;
- if (myNetEvents.iErrorCode[FD_READ_BIT] != 0)
- return E_XSOCKET_SOCKERR;
- /////////////////////////////////////////////////////////////////////////////////////
- if ((nLenReceived = recv(m_hSocket, pBuff, nLen, 0)) == WSAEWOULDBLOCK)
- {
- nLenReceived = 0;
- return E_XSOCKET_NOMOREDATA;
- }
- return E_XSOCKET_SUCCESS;
- }
- long CXSocket::GetLenDataAvail()
- {
- u_long lLen = 0;
- if (ioctlsocket(m_hSocket, FIONREAD, &lLen) != 0)
- {
- m_nLastError = WSAGetLastError();
- return E_XSOCKET_SOCKERR;
- }
- return lLen;
- }
|