📄 socketcc.h
字号:
/******************************************************************************//* TCP Socket Classes. *//******************************************************************************//* class TCPSocket. *//* *//* The TCPSocket class provides common TCP functionality of client and server *//* side TCP sockets within a class and provides a class from which both *//* TCPClientSocker and TCPServerSocket can inherit - this class inherits from *//* the SocketBase base class. Provision is made to both send and receive *//* data, as well as obtain addressing information (IP Address and port *//* number), both of the local and remote side of the socket. All of these *//* features/methods will fail if the socket is not connected and a Socket *//* Exception will be thrown. The provided methods are as follows: *//* *//* Constructor : Create either an IPv6 or IPv4 TCP Socket, forwards *//* the call to the base class constructor. There also *//* exists a protected constructor that allows creation *//* of a socket using an existing socket descriptor. *//* A second constructor allows creation of a TCP Socket *//* class given a socket descriptor. *//* Destructor : Destroy the created Socket. *//* SendData() : Send data from this socket to the remote connection. *//* Will fail if not connected. *//* SendASCII() : Send a NULL terminated character string from this *//* socket to the remote connection. *//* SendBinary16Bits() : Send a 16-bit value from this socket to the remote *//* connection. Binary value will be converted from host *//* to network byte order. *//* SendBinary32Bits() : Send a 32-bit value from this socket to the remote *//* connection. Binary value will be converted from host *//* to network byte order. *//* RecvData() : Receive data sent to this socket from the remote *//* connection. Will fail if not connected. *//* RecvBinary16Bits() : Receive a 16-bit value sent to this socket from the *//* remote connection. Binary value will be converted *//* from network to host byte order. *//* RecvBinary32Bits() : Receive a 32-bit value sent to this socket from the *//* remote connection. Binary value will be converted *//* from network to host byte order. *//* RecvASCII() : Receive a NULL terminated ASCII string sent to this *//* socket from the remote connection. A new (char *) *//* is allocated and must be free by the calling code. *//* LocalIPAddress() : If set, returns the local IP address of the socket. *//* RemoteIPAddress() : If connected, returns the IP address of the remote *//* socket connection. *//* LocalPortNumber() : If set, returns the local port number of the socket. *//* RemotePortNumber() : If connected, returns the port number of the remote *//* socket connection. *//* GetSockOpt() : Provision of call through to base class method. *//* SetSockOpt() : Provision of call through to base class method. *//* FCntl() : Provision of call through to base class method. *//* *//* NOTE: TCPServerSocket is made a friend of TCPSocket so that it can call *//* the protected member methods of TCPSocket in the Accept() method. *//******************************************************************************/class TCPSocket : protected SocketBase{ friend class TCPServerSocket; public: TCPSocket(bool bUseIPv6 = false); TCPSocket(int iNewSockDesc); virtual ~TCPSocket() {} int SendData(const void *pData, int iDataLen, unsigned int uiFlags = 0); void SendASCII(const char *pcData, char cTerminator = '\0', unsigned int uiFlags = 0); void SendBinary16Bits(const int iData, unsigned int uiFlags = 0); void SendBinary32Bits(const long lData, unsigned int uiFlags = 0); int RecvData(void *pBuffer, int iBufferLen, unsigned int uiFlags = 0); char * RecvASCII(char cTerminator = '\0', unsigned int uiFlags = 0); int RecvBinary16Bits(unsigned int uiFlags = 0); long RecvBinary32Bits(unsigned int uiFlags = 0); IPAddress &LocalIPAddress(); IPAddress &RemoteIPAddress(); int LocalPortNumber(); int RemotePortNumber(); void GetSockOpt(int iCodeLevel, int iOptionName, void *pOptionData, int &iDataLength); void SetSockOpt(int iCodeLevel, int iOptionName, const void *pOptionData, int iDataLength); int FCntl(int iCommand, long lArgument); protected: bool bLocalSet, bIsConnected; IPAddress cLocalAddress, cRemoteAddress; int iLocalPort, iRemotePort; void SetLocal(); void SetConnected();};/******************************************************************************//* class TCPClientSocket. *//* *//* The TCPClientSocket class provides client based TCP functionality within a *//* class. This class inherits from the TCPSocket base class and provides the *//* base functionality of a TCP client socket. All the basic TCP methods from *//* TCPSocket are inherited, the constructor also performs connection to the *//* server, the destructor disconnects the socket. If an error occurs at any *//* stage, a Socket Exception is thrown. The provided methods are as follows: *//* *//* Constructor : Create either an IPv6 or IPv4 TCP Client Socket, the socket *//* is connected to the server as specified by the provided IP *//* Address and port number. The type (IPv4 or IPv6) of the *//* socket is determined by the IP address of the server. *//* Destructor : Destroy the created Socket. *//* *//* The following methods are inherited from TCPSocket: *//* *//* SendData() LocalIPAddress() RemoteIPAddress() GetSockOpt() *//* RecvData() LocalPortNumber() RemotePortNumber() SetSockOpt() *//******************************************************************************/class TCPClientSocket : public TCPSocket{ public: TCPClientSocket(IPAddress &cServAddr, int iServPort); virtual ~TCPClientSocket() {}};/******************************************************************************//* class TCPServerSocket. *//* *//* The TCPServerSocket class provides server based TCP functionality within a *//* class. This class inherits from the TCPSocket base class and provides the *//* base functionality of a TCP server socket. All the basic TCP methods from *//* TCPSocket are inherited, newly provided methods are Accept() and new *//* constructors which enable creation of a listening server socket where the *//* bound IP Address is either specified or a wildcard and the bound port *//* number is either specified or a wildcard. If an error occurs at any *//* stage, a Socket Exception is thrown. The provided methods are as follows: *//* are as follows: *//* *//* Constructor : Create either an IPv6 or IPv4 TCP Server Socket, there *//* are two constructors. One constructor offers the option *//* of specifying an IP Address to bind the listening socket *//* to, the other will bind to the wildcard IP Address. The *//* other options are common to both constructors. iLocalPort *//* specifies the port number bound to the listening socket, *//* a value of zero means the operating system will choose *//* this value, it can then be obtained by calling the base *//* class LocalPortNumber() method. iBackLog indicates the *//* maximum number of pending connections. If the IP Address *//* is provided, it will be queried to determine whether to *//* create an IPv4 or IPv6 socket. If we are binding to the *//* wildcard address, the bUseIPv6 flag determines what type *//* of socket to create (IPv4 or IPv6). *//* Destructor : Destroy the created Socket. *//* AcceptClient() : Accepts a pending connection on the listening server *//* socket. Creates a TCPSocket instance to refer to the *//* newly created socket for future communications to that *//* client. A pointer to the TCPSocket instance is returned. *//* This pointer must be deleted to close the socket with the *//* client. *//* *//* The following methods are inherited from TCPSocket: *//* *//* SendData() LocalIPAddress() RemoteIPAddress() GetSockOpt() *//* RecvData() LocalPortNumber() RemotePortNumber() SetSockOpt() *//******************************************************************************/class TCPServerSocket : public TCPSocket{ public: TCPServerSocket(IPAddress &cLocalAddr, int iPortNo = 0, int iBackLog = 1); TCPServerSocket(int iPortNo = 0, int iBackLog = 1, bool bUseIPv6 = false); virtual ~TCPServerSocket() {} TCPSocket *AcceptClient();};/******************************************************************************//* UDP Socket Classes. *//******************************************************************************//* class UDPSocket. *//* *//* The UDPSocket class provides client based UDP functionality within a *//* class. This class inherits from the SocketBase base class and provides *//* only the base functionality of a UDP client socket. If any error occurs, *//* a Socket Exception is thrown. The provided methods are as follows: *//* *//* Constructor : Create either an IPv6 or IPv4 Socket. *//* A second constructor allows creation of a UDPSocket *//* class given a socket descriptor. This can be used *//* when migrating existing programs as it allows keeping *//* existing code that creates the actual socket *//* descriptor and then creating a socket class from that. *//* The overall effect is that these programs can be *//* converted partially, one bit at a time. *//* Destructor : Destroy the created Socket. *//* SendDatagram() : Send a Datagram from this socket to the provided */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -