/** * CRXSocket.h */ #ifndef __CRXSOCKET_H__ #define __CRXSOCKET_H__ #ifdef WIN32 # include #else # include # include # include # include #endif #include using namespace std; typedef struct sockaddr stSockAddr; typedef struct sockaddr_in stSockAddrIn; typedef unsigned short ushort; typedef unsigned int uint; #define ERROR_TCPSOCKET -1000 #define ERROR_TCPSOCKET_FAILED_TO_INITIALIZE ERROR_TCPSOCKET - 1 #define ERROR_TCPSOCKET_ALREADY_IN_USE ERROR_TCPSOCKET - 2 #define ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET ERROR_TCPSOCKET - 3 #define ERROR_TCPSOCKET_FAILED_TO_GET_HOSTNAME ERROR_TCPSOCKET - 4 #define ERROR_TCPSOCKET_FAILED_TO_CONNECT ERROR_TCPSOCKET - 5 #define ERROR_TCPSOCKET_FAILED_TO_BIND ERROR_TCPSOCKET - 6 #define ERROR_TCPSOCKET_FAILED_TO_LISTEN ERROR_TCPSOCKET - 7 #define ERROR_TCPSOCKET_FAILED_TO_ACCEPT ERROR_TCPSOCKET - 8 #define ERROR_TCPSOCKET_FAILED_TO_SEND ERROR_TCPSOCKET - 9 #define ERROR_TCPSOCKET_FAILED_TO_RECEIVE ERROR_TCPSOCKET - 10 #define ERROR_TCPSOCKET_NOT_READY ERROR_TCPSOCKET - 11 class CRXSocket { private: static bool mInitialized; stSockAddrIn mAddress; int mSocket; private: /** * create socket. * @return success : socket id, failed : -1. */ int CreateSocket (void); /** * check socket status. * @return available : true, else : false. */ inline bool IsCreated (void) const; inline bool IsReady (void) const; /** * static functions */ static int Initialize (void); static bool IsInitialized (void); static void Finalize (void); public: /** * common functions */ CRXSocket (int aSocket = 0); ~CRXSocket (void); void Attach (int aSocket); int Detach (void); void Close (void); operator int (void) const; CRXSocket & operator = (int aSocket); /** * for server */ int CreateServer (ushort aPort, int aBacklog = 5, stSockAddrIn * aAddress = NULL); int Accept (stSockAddrIn * aRemoteAddress = NULL, int * aAddressLength = NULL); /** * for client */ int Connect (std::string aUrl, ushort aPort); /** * for communication */ int Send (char * aBuffer, int aSize); int Receive (char * aBuffer, int aSize); }; #endif // #ifndef __CRXSOCKET_H__