Blocking socket package class - header file

xiaoxiao2021-03-06  48

// mysocket.h: socket package, network data transmission, receive // ​​package name: socket class // Dependent component: WS2_32.Lib, Winsock2.h //// --------- --------------------------- // * Class Name: CSOCKADDR / / * Package Name: Network Socket // * Function: Package Skinname Address // * Servers: Fusx // * Dependence: Winsock.h // * Development Date: 2003/06/11 // * Modify Date: 2004/06/06 // * Project Name: / / * Discovery: // ------------------------------------ // Destination: Packaging Set Words can be used to send to the network, receive data. Blocking the call function. // Description: This class is extracted and modified from the "VC 6.0 technology insider"

#if! defined (my_socket_h) #define my_socket_h

#include #pragma Comment (LIB, "WS2_32.LIB")

class CSockAddr: public sockaddr_in {public: // Constructor CSockAddr () {sin_family = AF_INET; sin_port = 0; sin_addr.s_addr = 0;} // Default CSockAddr (const SOCKADDR & sa) {memcpy (this, & sa, sizeof (SOCKADDR ));} Csockaddr (const sockaddr_in & sin) {memcpy (this, & sin));} // uLaddr is INADDR_ANY, default is that the unit is an IP CSOCKADDR (Const Ulong ULADDR, Const USHORT Ushport = 0) / / parms are host byte ordered {sin_family = AF_INET; sin_port = htons (ushPort); sin_addr.s_addr = htonl (ulAddr);} CSockAddr (const char * pchIP, const USHORT ushPort = 0) // dotted IP addr string {sin_family = AF_INET; sin_port = htons (ushPort); sin_addr.s_addr = inet_addr (pchIP);} // already network byte ordered // return address format LPCTSTR GetIPDottedDecimal point (LPTSTR strIP = NULL, int nLen = 16) {if (strIP ! = Null) Memcpy (Strip, inet_ntoa (sin_addr); return inet_ntoa (sin_addr);} // Get port and address (host order) ushort port () const {return ntoHs (sin_port);} // Get IP Address (host order) Ulong ipaddr () const {retur n ntohl (sin_addr.s_addr);} // convert and assignment const CSockAddr & operator = (const SOCKADDR & sa) {memcpy (this, & sa, sizeof (SOCKADDR)); return * this;} const CSockAddr & operator = (const SOCKADDR_IN & sin) {memcpy (this, & sin, sizeof (SOCKADDR_IN)); return * this;} operator SOCKADDR () {return * ((LPSOCKADDR) this);} operator LPSOCKADDR () {return (LPSOCKADDR) this;} operator LPSOCKADDR_IN () { Return (lpsockaddr_in) this;}};

//// ------------------------------------ // * Class name: cmysocket // * Package Name: Network Socket // * Features: Package Network Socket // * Server: Fusx // * Dependent Components: WSOCK32.LIB // * Development Date: 2003/06/11 // * Modify Date : 2004/06/06 // * Discount: sishine // ----------------------------------- - // Objective: // Packaged socket can be used to send to the network, receive data. Blocking the call function. // Description: This class is extracted from the "VC 6.0 technology insider" and modified /// typedef const struct sockaddr * lpcsockaddr;

Class AFX_EXT_CLASS CMYSOCKET {public: cmysocket (); virtual ~ cmysocket ();

// Parameter Description: // Return Value: Successfully Returns true, failback Returns false // char * PCH for the first address of the data to be sent or received, nsize is the data size (byte) // int & Nerrcode to return false errors Code. Nerrcode invalid // lpcsockaddr PSA is timeout setting time when returning True is a timeout setting time, the NMillSecs unit is microsecond, and when the two are 0, it is not considered, ie blocking calls

// If you turn close the socket, return false, NerrCode = WSAENOTSOCK // Returns false, NerrCode = wsaetimedout; // if you turn off, return TRUE if the other party closes the connection, and nBytesReceived = 0Public: Void Operator = (socket SCK);

// Create a socket, successfully return true, otherwise, the default creation type is SOCK_STREAM BOOL CREATE (int NTYPE = SOCK_STREAM / * SOCK_STREAM OR SOCK_DGRAM * /); // Bind Address BOOL BIND (LPCSOCKADDR PSA); Bool Bind LPCTSTR STRIP, INT NPORT

// Listen (server-side) BOOL LISTEN ();

/ / Connect the server address BOOL Connect (LPCSOCKADDR PSA); Bool Connect (LPCTSTSTSTSTSTSTST); / / The default is blocked, accept the client connection (server-side), the new socket is S, the client address is In the PSA // Saire, return false, but nerrcode = wsaeintr, timeout returns false, Nerrcode = 0; Bool Accept (CMYSocket & S, LPSockAddr PSA, INT NSECS = 0); // Send data, nbytessent is actual Sending the number of bytes of the Bool Send (const char * pch, const nSize, int "); // Send all the data until all data is sent or an error, the timeout returns BOOL WRITE (Const Char * PCH, const Int nsize, int nsecs = 0); // If the other side socket is turned off, return TRUE, and NBytesReceived is 0 characters, and the other error sees Bool Receive (Char * PCH, Const Int Nsize, INT & NBYTESRECEIVED, INT NSECS = 0);

// Send a datagram, actually sending the data size is NBytessent Bool Senddatagram (LPCSOCKADDR PSA, Const Char * PCH, Const Int Nsize, Int & nSecs = 0);

/ / Receive a datagram, the actually received data size is NBytesReceived Bool ReceiveDataGram (LPSOCKADDR PSA, CHAR * PCH, Const Int Nsize, Int & nbytesReceived, Int nsecs = 0);

/ / Get the other party's socket address Bool getPeeradDr (LPSOCKADDR PSA);

/ / Get local socket address BOOL GetSockAddr (LPSOCKADDR PSA);

/ / Get the host address according to the name, the failed return address is 0 static csockaddr gethostByname (const car * pchname, const usort usport = 0);

// obtain the host name according to the address, the caller needs to delete char * memory; failure returns NULL. Static const char * gethostbyaddr (LPCSOCKADDR PSA);

// Get the local computer's IP and hostname static bool GetLocalHostInfo (LPTSTR strHostName, LPTSTR strHostIP);

// Turn off the socket and may return the error BOOL Close ();

// This cover can be used with Socket General operator socket () {return m_hsocket;}

private: SOCKET m_hSocket; int m_nErrCode; public: // Get error string according to the error code bool GetErrorMessage (const int nErrCode, char * pErrMsg, int nMaxLen); inline int GetErrorCode () {return m_nErrCode;}; // determine the validity (Non-empty) Inline Bool IsValid (Void) {Return M_HSocket! = Invalid_socket;

}

#endif //

转载请注明原文地址:https://www.9cbs.com/read-79298.html

New Post(0)