📄 socketcc.h
字号:
/* destination IP Address and Port Number. *//* ReceiveDatagram() : Receive a datagram sent to this socket. The source IP *//* Address and Port Number are stored in the provided *//* variables. *//* 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. *//******************************************************************************/class UDPSocket : protected SocketBase{ public: UDPSocket(bool bUseIPv6 = false); UDPSocket(int iNewSockDesc) : SocketBase(iNewSockDesc) {} virtual ~UDPSocket() {} int SendDatagram(const void *pBuffer, int iBufLength, IPAddress &cDestAddr, int iDestPort, unsigned int uiFlags = 0); int ReceiveDatagram(void *pBuffer, int iBufLength, IPAddress &cSourceAddr, int &iSourcePort, unsigned int uiFlags = 0); 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);};/******************************************************************************//* class UDPServerSocket. *//* *//* The UDPServerSocket class provides server based UDP functionality within *//* a class. This class inherits from the UDPSocket base class and provides *//* the base functionality of a Server side UDP Socket. The Send and Receive *//* data methods are inherited from UDPSocket, the constructor requires the *//* provision of local port number and IP Address to bind to. 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 Server Socket on the *//* specified port. Two constructors are available, the *//* first binds the socket to the specified local IP *//* Address and port number, the second to all local IP *//* Addresses and the specified port number. The default *//* port number of zero means that the system chooses the *//* the port number of the socket. *//* Destructor : Destroy the created Socket. *//* SendDatagram() : Inherited from UDPSocket with the same parameters. *//* ReceiveDatagram() : Inherited from UDPSocket with the same parameters. *//* LocalIPAddress() : If connected, returns the local IP address of the *//* connected socket. *//* LocalPortNumber() : If connected, returns the local port number of the *//* connected socket. *//******************************************************************************/class UDPServerSocket : public UDPSocket{ public: UDPServerSocket(IPAddress &cLocalAddr, int iLocalPortNo = 0); UDPServerSocket(int iLocalPortNo = 0, bool bUseIPv6 = false); virtual ~UDPServerSocket() {} IPAddress &LocalIPAddress() { return cLocalAddress; } int LocalPortNumber() { return iLocalPort; } private: IPAddress cLocalAddress; int iLocalPort;};/******************************************************************************//* class UDPConnectedSocket. *//* *//* The UDPConnectedSocket class provides client based UDP functionality *//* within a class. This class inherits from the SocketBase base class and *//* provides the base functionality of a connected UDP Socket. Data can only *//* be sent or received once the socket has been connected and can only be *//* sent to or received from the server the socket is connected to. There is *//* provision to connect and disconnect the socket as well as obtaining *//* information about the local and remote IP addresses and port numbers. 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 Connected Socket, two *//* constructors are available, the first creates the *//* socket in a disconnected state, the second connects *//* to the specified server. *//* Destructor : Destroy the created Socket. *//* Connect() : Connect the socket to the specified server and port. *//* DisConnect() : Disconnect the socket, can no longer send or receive *//* data. This is not necessary when destroying the *//* socket, the destructor will handle any necessary *//* clean-up. *//* SendDatagram() : Send a Datagram from this socket to the connected *//* server. Will fail if not connected. *//* ReceiveDatagram() : Receive a datagram sent to this socket from the *//* connected server. Will fail if not connected. *//* LocalIPAddress() : If connected, returns the local IP address of the *//* connected socket. *//* RemoteIPAddress() : If connected, returns the server IP address of the *//* connected socket. *//* LocalPortNumber() : If connected, returns the local port number of the *//* connected socket. *//* RemotePortNumber() : If connected, returns the server port number of the *//* connected socket. *//* 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. *//******************************************************************************/class UDPConnectedSocket : protected SocketBase{ public: UDPConnectedSocket(bool bUseIPv6 = false); UDPConnectedSocket(IPAddress &cServAddr, int iServPortNo, bool bUseIPv6 = false); virtual ~UDPConnectedSocket() {} void Connect(IPAddress &cServAddr, int iServPortNo); void DisConnect(); int SendDatagram(const void *pBuffer, int iBufLength, unsigned int uiFlags = 0); int ReceiveDatagram(void *pBuffer, int iBufLength, unsigned int uiFlags = 0); IPAddress &LocalIPAddress() { return cLocalAddress; } IPAddress &RemoteIPAddress() { return cServerAddress; } int LocalPortNumber() { return iLocalPort; } int RemotePortNumber() { return iServerPort; } 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); private: bool bIsConnected; IPAddress cLocalAddress, cServerAddress; int iLocalPort, iServerPort;};/******************************************************************************//* class SocketSet. *//* *//* The SocketSet class provides support for programming using the select() *//* socket function calls. This class provides support for the building and *//* and querying of fd_set type constructs used in by select(). Pointers to *//* SocketBase and derivered classes are supplied so that the socket descriptor*//* is fully hidden. The basic implementation is fully contained within the *//* the header file. Public methods are: *//* *//* Constructor : Initialise the Socket Set to the empty set. *//* Destructor : Free any allocated resources. *//* Clear() : Clear the Set to contain no socket descriptors. *//* operator += : Add the provided Socket to the represented Set. *//* operator -= : Remove the provided Socket from the represented Set. *//* IsMember() : Return true if the provided Socket class is a member of *//* the represented Set. *//* cast (int) : Return the largest socket descriptor contained by Set. *//* cast (fd_set *) : Return a pointer to the contained fd_set object. *//******************************************************************************/class SocketSet{ public: SocketSet() { Clear(); } virtual ~SocketSet() {} void Clear() { FD_ZERO(&fsSet); iMaxSockDesc = 0; } void operator+=(SocketBase *pcSocket) { FD_SET(pcSocket->iSockDesc, &fsSet); if (pcSocket->iSockDesc > iMaxSockDesc) iMaxSockDesc = pcSocket->iSockDesc; } void operator-=(SocketBase *pcSocket) { FD_CLR(pcSocket->iSockDesc, &fsSet); } bool IsMember(SocketBase *pcSocket) { return FD_ISSET(pcSocket->iSockDesc, &fsSet); } operator int() { return iMaxSockDesc; } operator fd_set *() { return &fsSet; } private: int iMaxSockDesc; fd_set fsSet;};#endif/******************************************************************************//* End of File: socketcc.h *//******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -