CasyNsocket seems to meet the requirements, can't be used across threads should be its fatal injuries, using Socket is the best way, which is also recommended by many big cows. In order to avoid duplication of labor, I have encapsulated a CMYSocket class, I hope to use like CasyNsocket (it is not possible now, even beginners, only slowly improve, maybe a day can be close to casynsocket), since it is a class of packages, Thread safety has been calculated, huh, huh.
The following functions are currently implemented:
Create Socket, Data Receiving (TCP, and UDP), send and receive buffer size settings.
The IO model uses a WSAEventSelect mechanism.
Use it to realize the chat program of the LAN, very easy to use. If there is an inappropriate place, please leave a message, I will gradually improve it!
Instructions:
Inherit the CMYSocket class, overload onconcept, onReceive and other functions (same as the CasyncSocket method). Don't call the AFXSocketinit function, because the function is working in the code I packaged.
Below is the code
***************************** Head file mysocket.h ************** ******** #ifndef __MYSOCKET_H __ # define __MYSOCKET_H__ #include
#ENDIF *************************** Code implements mysocket.cpp *************** ************* #include "stdafx.h" #include "mysocket.h" #include
addr.sin_port = htons (port); if (bind (m_socket, (LPSOCKADDR) & addr, sizeof (addr)) == SOCKET_ERROR) {int err = WSAGetLastError (); return err;}} WSAEVENT wsaEvent = WSACreateEvent (); if (WSAEventSelect (m_socket, wsaEvent, lEvent)) {int err = WSAGetLastError (); return err;} EnterCriticalSection (& m_cs); socketArray [m_totalSocket] = m_socket; eventArrray [m_totalSocket] = wsaEvent; m_totalSocket ; LeaveCriticalSection (& m_cs); m_bEndTread = False; if (_BeginThread, 0, this) == -1) {RETURN-1;} int loopnum = 0; while (! M_bthreadstarted) {loopNum ; if (loopnum> 200) {return -1;} SLEEP 10);} return 0;} int CMySocket :: Close () {RemoveSocket (m_socket); if (closesocket (m_socket) == SOCKET_ERROR) {int err = WSAGetLastError (); return err;} m_socket = INVALID_SOCKET; m_bEndTread = TRUE Return 0;} int CMYSocket :: Listen (INT Backlog) {if (Listen (m_socket, backlog) == Socket_ERROR) {int err = wsagetlasterror (); return err;} return 0;} int CMYSOCKET :: Acce pt (CMySocket * pSocket, struct sockaddr * addr) {SOCKET sock; int len = sizeof (struct sockaddr); if ((sock = accept (m_socket, addr, & len)) == INVALID_SOCKET) {int err = WSAGetLastError (); return err;} pSocket-> m_socket = sock; pSocket-> m_nSocketType = SOCK_STREAM; WSAEVENT wsaEvent = WSACreateEvent (); if (WSAEventSelect (sock, wsaEvent, FD_READ | FD_CLOSE)) {int err = WSAGetLastError (); return err;} ENTERCRITILAY [M_TOTALSOCKET] = SOCK; EventArrray [m_totalsocket] = WSAEvent; M_Totalsocket ; int index = m_nclientnum;
clientList [index] .addr = * addr; clientList [index] .sock = sock; m_nClientNum ; LeaveCriticalSection (& m_cs); return 0;} int CMySocket :: Connect (const char * IP, int port) {sockaddr_in addr; addr. sin_family = AF_INET; addr.sin_addr.S_un.S_addr = inet_addr (IP); addr.sin_port = htons (port); if (connect (m_socket, (LPSOCKADDR) & addr, sizeof (addr)) == SOCKET_ERROR) {int err = Wsagetlasterror (); return err;} return 0;} void cmysocket :: removeSocket (socket s) {INT i, j; entercriticalsection (& m_cs); int totalsocket = m_totalsocket; for (i = 0; i
dwIndex - = WSA_WAIT_EVENT_0; CMySocket :: m_dwIndex = dwIndex; memset (& tEvent, 0, sizeof (WSANETWORKEVENTS)); EnterCriticalSection (& CMySocket :: m_cs); WSAEnumNetworkEvents (CMySocket :: socketArray [dwIndex], CMySocket :: eventArrray [dwIndex], & tEvent); WSAResetEvent (CMySocket :: eventArrray [dwIndex]); LeaveCriticalSection (& CMySocket :: m_cs); if (tEvent.lNetworkEvents & FD_CONNECT) {errorcode = tEvent.iErrorCode [FD_CONNECT_BIT]; pMySocket-> OnConnect (errorcode);} else if (tEvent.lNetworkEvents & FD_ACCEPT) {errorcode = tEvent.iErrorCode [FD_ACCEPT_BIT]; pMySocket-> OnAccept (errorcode);} else if (tEvent.lNetworkEvents & FD_READ) {DWORD nBytes = 0; errorcode = tEvent.iErrorCode [FD_READ_BIT] ; ioctlsocket (CMySocket :: socketArray [dwIndex], FIONREAD, & nBytes); if (nBytes> 0) pMySocket-> OnReceive (errorcode);} else if (tEvent.lNetworkEvents & FD_WRITE) {errorcode = tEvent.iErrorCode [FD_WRITE_BIT]; PMYSOCKET-> Onsend (ErrorCode);} else if (TEVENT.LNETWORKEVENTS & FD _CLOSE) {errorcode = tEvent.iErrorCode [FD_CLOSE_BIT]; pMySocket-> OnClose (errorcode); CMySocket :: RemoveSocket (CMySocket :: socketArray [dwIndex]);} else {}}} int CMySocket :: Receive (char * buf, int len) {sockaddr addr; int size = sizeof (addr); switch (m_nSocketType) {case SOCK_DGRAM: if (recvfrom (m_socket, buf, len, 0, & addr, & size) == SOCKET_ERROR) {int err = WSAGetLastError () Return Err;} Break; Case Sock_Stream: IF (RECV (M_Socket, BUF, LEN, 0) == SOCKET_ERROR) {int err = wsagetlasterror (); return err;} Break; default: Break;} return;
} Int CMySocket :: Receive (char * buf, int len, char * IP, int * port) {sockaddr addr; int size = sizeof (addr); sockaddr_in * pAddr_in; switch (m_nSocketType) {case SOCK_DGRAM: if (recvfrom ( M_Socket, BUF, LEN, 0, & Addr, & size) == Socket_ERROR) {int err = wsagetlasterror (); return err;} Paddr_in = (sockaddr_in *) & addr; structdr; ip, inet_adoa (paddr_in-> sin_addr); * Port = NTOHS (PADDR_IN-> SIN_PORT); Break; Case Sock_Stream: IF (RECV (M_Socket, BUF, LEN, 0) == SOCKET_ERROR) {int err = wsagetlasterror (); return err;} Break; default: Break; Return 0;} int CMYSocket :: Send (Char * BUF, INT LEN) {IF (send (m_socket, buf, len, 0) == SOCKET_ERROR) {int err = wsagetlasterror (); return err;} returnography;} INT CMYSOCKET :: Send (Char * BUF, INT LEN, CONST Char * IP, INT port) {sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_un.s_addr = inet_addr (ip); addr.sin_port = htons Port); switch (m_nsockettype) {copy sock_dgram: IF (sendto (M_Socket, Buf, Len, 0, (LPSOCKADDR) & addr, sizeof (addr) == Socket_er RETURN ERR;} Break; Case Sock_Stream: IF (send (m_socket, buf, len, 0) == Socket_ERROR) {int err = wsagetlasterror (); returnal;} Break; default : break;} return 0;} int CMySocket :: GetRcvBufSize (int * bufsize, int * len) {if (getsockopt (m_socket, SOL_SOCKET, SO_RCVBUF, (char *) bufsize, len)) {int err = WSAGetLastError (); return err;} return 0;} int CMySocket :: GetSndBufSize (int * bufsize, int * len) {if (getsockopt (m_socket, SOL_SOCKET, SO_SNDBUF, (char *) bufsize, len)) {int err = WSAGetLastError (); Return Err;} Return 0;