📄 socketcc.h
字号:
/******************************************************************************//* File: socketcc.h *//******************************************************************************//* This source code file is a header file for a series of C++ classes that *//* encapsulate the socket libraries to provide network functionality. The *//* C++ classes wrap the C function calls to provide the same functionality to *//* object oriented programmers. *//******************************************************************************//* Written By: Jason But *//* Copyright: CTIE - Monash University 2001-2003 *//* *//* Contributions: *//* Andreas Almroth - Support for Solaris OS. *//* Desmond Schmidt - Support for MacOS X 1. *//* Daniel Grimm - Testing for MacOS X Jaguar. *//* Clayborne Taylor - Support for FreeBSD. *//* Andrea Rui - Inspiration for cleaner implementation to accept a *//* connection on a listening socket. *//* *//* Notes: *//* Version 1.00 - Original Version of module. *//* *//* Version 1.10 - pthreadcc Class Library now used for thread-safe *//* implementation. *//* - New exception type added to SocketException to signify *//* a failure in converting an IPv6 address to IPv4. *//* - Lazy evaluation of pcHostName via reverse DNS lookup and *//* pcStrAddress. Evaluation performed only if the string *//* values are requested - major performance increase. *//* - Added new member method to convert an IPAddress instance *//* into either an IPv4 or IPv6 address. *//* - TCPClientSocket must now be created in the connected *//* state, no longer need to specify whether to use IPv6 or *//* IPv4 as this is retrieved from the IP Address provided. *//* No unconnected client sockets available, makes sense *//* since you cannot disconnect the socket anyway. *//* - TCPServerSocket constructor no longer requires the IPv6 *//* flag when binding to a specific IP address, but is still *//* required when binding to a wildcard address. *//* *//* Version 1.20 - SocketException constructor now takes optional value of *//* an integer error number. If the error code is of type *//* errUnknown, this error code is included in the string *//* representation of the exception. *//* - New exception type - errHostUnreachable - added. *//* - UDPServerSocket constructor no longer requires the IPv6 *//* flag when binding to a specific IP address, but is still *//* required when binding to a wildcard address. *//* - Added new methods to TCPSocket to send and receive *//* different types of data (uint16_t, uint32_t, C-style *//* strings). *//* - pcHostName replaced with strHostName of type std::string *//* This should fix a range of minor memory leakage problems *//* as well as simplify the code somewhat. *//* *//* Version 1.30 - Support for MacOS X, many thanks to Desmond Schmidt and *//* Daniel Grimm. *//* - Header file <sys/socket.h> now included in socketcc.h *//* as SOCK_STREAM is defined here on the Mac Platform. *//* - The <string> header file must be included as extern "C++"*//* otherwise the MacOS X g++ compiler complains when using *//* SocketCC in your own applications. *//* - Enhanced functionality of TCPSocket::SendASCII() and *//* TCPSocket::RecvASCII() to allow specification of the *//* string terminating character. For backwards *//* compatibility, this has a default value of '\0'. *//* - Added new method to IPAddress to return an unmapped *//* (not IPv6 Mapped IPv4) string representation of the IP *//* Address. *//* - Enhanced functionality of send and receive methods in *//* all TCP and UDP socket classes to allow specification of *//* socket flags. A default value of zero allows backwards *//* compatibility. *//* *//* Version 1.38 - Send methods for all socket types now accept const void *//* pointers as a parameter, this is more correct when *//* defining what the method does. Many thanks to Andrea *//* for pointing this out. *//* - Created a new constructor for the SocketBase Class, this *//* constructor allows creation of a SocketBase instance by *//* providing a socket descriptor. This method is primarily *//* for convenience as well as a better implementation of *//* accepting connections on TCP Sockets. Please note that *//* the provided socket descriptor is not checked to ensure *//* it is a valid descriptor. *//* - Created a new constructor for the TCPSocket Class, this *//* constructor allows creation of a TCPSocket instance by *//* providing a socket descriptor. This method is primarily *//* for convenience only as the provided socket descriptor *//* is not checked to ensure it is a valid descriptor, nor *//* checked to ensure it refers to an actual TCP socket. A *//* similar constructor is not provided for TCPServerSocket *//* as creating a server socket also means specifying an *//* address and port number to bind to, this makes no sense *//* when providing an existing socket descriptor. A similar *//* argument is provided when constructing an instance of *//* TCPClientSocket and therefore no similar constructor has *//* been created for this class either. *//* - Created a new constructor for the UDPSocket Class, this *//* constructor allows creation of a UDPSocket instance by *//* providing a socket descriptor. This method is primarily *//* for convenience only as the provided socket descriptor *//* is not checked to ensure it is a valid descriptor, nor *//* checked to ensure it refers to an actual UDP socket. A *//* similar constructor is not provided for UDPServerSocket *//* as creating a server socket also means specifying an *//* address and port number to bind to, this makes no sense *//* when providing an existing socket descriptor. A similar *//* argument is provided when constructing an instance of *//* UDPConnectedSocket and therefore no similar constructor *//* has been created for this class either. *//* - The interface for the SocketBase::Accept() method has *//* been changed. Hopefully this will have minimal impact *//* on existing code as most people will be using the *//* inherited TCP classes rather than SocketBase. I am sorry*//* if you are affected by this but the new implementation *//* has better usage of resources. *//******************************************************************************//******************************************************************************//* Check to see if already included. *//******************************************************************************/#ifndef SOCKETCC_H#define SOCKETCC_H/******************************************************************************//* Include definitions required to define classes. *//******************************************************************************/#include <unistd.h>#include <netinet/in.h>#include <sys/socket.h>#include <pthreadcc.h>extern "C++"{#include <string>}/******************************************************************************//* class SocketException. *//* *//* This class is used to store the exception details when an error occurs. *//* When any class method call in the socketcc library fails, it throws an *//* instance of SocketException, this can be caught and queried for the actual *//* error code. The exception can be cast to either the error code or a char *//* pointer for a textual description of the error. *//******************************************************************************/class SocketException{ public: enum ErrorCodes { errBadHostName, errNoIPAddress, errDNSError, errNoProtocolSupport, errKernelMemory, errCannotConvertToIPv4, errNoDescriptors, errPermissionDenied, errMemory, errInvalidProtocol, errBadDescriptor, errIllegalPointer, errAlreadyConnected, errConnectRefused, errConnectTimeOut, errNetUnreachable, errHostUnreachable, errAddrInUse, errInProgress, errAlreadyConnecting, errIncorrectAddrFamily, errBrdCastNotEnabled, errAlreadyBound, errAddressProtected, errCantListen, errNotStreamSock, errNoPendingConnections, errFirewall, errNotConnected, errWouldBlock, errInterrupted, errInvalidArgument, errMessageSizeTooBig, errNotBound, errOptionNotSupported, errUnknown }; SocketException(ErrorCodes ecType = errUnknown, int iErrNo = 0); SocketException(const SocketException &cOriginal); ~SocketException(); operator const ErrorCodes() { return ecException; } operator const char*() { return pcExceptionString; } private: ErrorCodes ecException; char *pcExceptionString; void SetString(const char *pcErrString);};/******************************************************************************//* class IPAddress. *//* *//* This class is used to provide transparent IPv4 and IPv6 address support *//* for the Socket classes within the library. The constructor creates a *//* default IPv4 address to localhost. The address can be assigned and parts *//* of the address can be extracted for usage. This class is intended to be *//* used in conjunction with the Socket classes. If the class instance is *//* assigned a binary IP address value, the hostname and address strings are *//* avaluated lazily, the reverse DNS lookup only occurs if and when the user *//* application requests these values. This should speed up execution of code *//* where these values are not required. The member methods are: *//* *//* operator= : Assignment of an IPAddress class. Can be *//* either from an existing IPAddress class, a */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -