?? socket.h
字號:
*/ Error connect(const IPV4Broadcast &subnet, tpport_t port); /** * Associate this socket with a multicast group. * * @return 0 on success, -1 on error. * @param mgroup address of the multicast group to send to. * @param port port number */ Error connect(const IPV4Multicast &mgroup, tpport_t port);#ifdef CCXX_IPV6 Error connect(const IPV6Multicast &mgroup, tpport_t port);#endif /** * Transmit "send" to use "connected" send rather than sendto. * * @return number of bytes sent. * @param buf address of buffer to send. * @param len of bytes to send. */ inline ssize_t send(const void *buf, size_t len) {return _IORET64 ::send(so, (const char *)buf, _IOLEN64 len, 0);} /** * Stop transmitter. */ inline void endTransmitter(void) {Socket::endSocket();} /* * Get transmitter socket. * * @return transmitter. */ inline SOCKET getTransmitter(void) {return so;}; inline Error setMulticast(bool enable) {return Socket::setMulticastByFamily(enable, family);} inline Error setTimeToLive(unsigned char ttl) {return Socket::setTimeToLiveByFamily(ttl, family);};public: /** * Transmit "send" to use "connected" send rather than sendto. * * @note Windows does not support MSG_DONTWAIT, so it is defined * as 0 on that platform. * @return number of bytes sent. * @param buffer address of buffer to send. * @param len of bytes to send. */ inline ssize_t transmit(const char *buffer, size_t len) {return _IORET64 ::send(so, buffer, _IOLEN64 len, MSG_DONTWAIT);} /** * See if output queue is empty for sending more packets. * * @return true if output available. * @param timeout in milliseconds to wait. */ inline bool isOutputReady(unsigned long timeout = 0l) {return Socket::isPending(Socket::pendingOutput, timeout);}; inline Error setRouting(bool enable) {return Socket::setRouting(enable);}; inline Error setTypeOfService(Tos tos) {return Socket::setTypeOfService(tos);}; inline Error setBroadcast(bool enable) {return Socket::setBroadcast(enable);};};/** * Representing half of a two-way UDP connection, the UDP receiver * can receive data from another peer host or subnet. This class is * used exclusivily to derive the UDPDuplex. * * @author David Sugar <dyfet@ostel.com> * @short Unreliable Datagram Peer Associations. */class __EXPORT UDPReceive : protected UDPSocket{protected: /** * Create a UDP receiver, bind it to a specific interface * and port address so that other UDP sockets on remote * machines (or the same host) may find and send UDP messages * to it, and associate it with a given port on a peer host. * On failure to bind, an exception is thrown. * * @param bind address to bind this socket to. * @param port number to bind this socket to. */ UDPReceive(const IPV4Address &bind, tpport_t port);#ifdef CCXX_IPV6 UDPReceive(const IPV6Address &bind, tpport_t port);#endif /** * Associate this socket with a specified peer host. The port * number from the constructor will be used. All UDP packets * will be sent received from the specified host. * * @return 0 on success, -1 on error. * @param host host network address to connect socket to. * @param port host transport port to connect socket to. */ Error connect(const IPV4Host &host, tpport_t port);#ifdef CCXX_IPV6 Error connect(const IPV6Host &host, tpport_t port);#endif /** * Check for pending data. * * @return true if data is waiting. * @param timeout in milliseconds. */ bool isPendingReceive(timeout_t timeout) {return Socket::isPending(Socket::pendingInput, timeout);}; /** * End receiver. */ inline void endReceiver(void) {Socket::endSocket();} inline SOCKET getReceiver(void) const {return so;}; inline Error setRouting(bool enable) {return Socket::setRouting(enable);} inline Error setMulticast(bool enable) {return Socket::setMulticastByFamily(enable, family);} inline Error join(const IPV4Multicast &ia) {return Socket::join(ia);}#ifdef CCXX_IPV6 inline Error join(const IPV6Multicast &ia) {return Socket::join(ia);}#endif inline Error drop(const IPV4Multicast &ia) {return Socket::drop(ia);}#ifdef CCXX_IPV6 inline Error drop(const IPV6Multicast &ia) {return Socket::drop(ia);}#endifpublic: /** * Receive a data packet from the connected peer host. * * @return num of bytes actually received. * @param buf address of data receive buffer. * @param len size of data receive buffer. */ inline ssize_t receive(void *buf, size_t len) {return _IORET64 ::recv(so, (char *)buf, _IOLEN64 len, 0);}; /** * See if input queue has data packets available. * * @return true if data packets available. * @param timeout in milliseconds. */ inline bool isInputReady(timeout_t timeout = TIMEOUT_INF) {return Socket::isPending(Socket::pendingInput, timeout);};};/** * UDP duplex connections impliment a bi-directional point-to-point UDP * session between two peer hosts. Two UDP sockets are typically used * on alternating port addresses to assure that sender and receiver * data does not collide or echo back. A UDP Duplex is commonly used * for full duplex real-time streaming of UDP data between hosts. * * @author David Sugar <dyfet@ostel.com> * @short Unreliable Datagram Peer Associations. */class __EXPORT UDPDuplex : public UDPTransmit, public UDPReceive{public: /** * Create a UDP duplex as a pair of UDP simplex objects * bound to alternating and interconnected port addresses. * * @param bind address to bind this socket to. * @param port number to bind sender. */ UDPDuplex(const IPV4Address &bind, tpport_t port);#ifdef CCXX_IPV6 UDPDuplex(const IPV6Address &bind, tpport_t port);#endif /** * Associate the duplex with a specified peer host. Both * the sender and receiver will be interconnected with * the remote host. * * @return 0 on success, error code on error. * @param host address to connect socket to. * @param port number to connect socket to. */ Error connect(const IPV4Host &host, tpport_t port);#ifdef CCXX_IPV6 Error connect(const IPV6Host &host, tpport_t port);#endif /** * Disassociate this duplex from any host connection. No data * should be read or written until a connection is established. * * @return 0 on success, error code on error. */ Error disconnect(void);};/** * TCP sockets are used for stream based connected sessions between two * sockets. Both error recovery and flow control operate transparently * for a TCP socket connection. The TCP socket base class is primary used * to bind a TCP "server" for accepting TCP streams. * * An implicit and unique TCPSocket object exists in Common C++ to represent * a bound TCP socket acting as a "server" for receiving connection requests. * This class is not part of TCPStream because such objects normally perform * no physical I/O (read or write operations) other than to specify a listen * backlog queue and perform "accept" operations for pending connections. * The Common C++ TCPSocket offers a Peek method to examine where the next * pending connection is coming from, and a Reject method to flush the next * request from the queue without having to create a session. * * The TCPSocket also supports a "OnAccept" method which can be called when a * TCPStream related object is created from a TCPSocket. By creating a * TCPStream from a TCPSocket, an accept operation automatically occurs, and * the TCPSocket can then still reject the client connection through the * return status of it's OnAccept method. * * @author David Sugar <dyfet@tycho.com> * @short bound server for TCP streams and sessions. */class __EXPORT TCPSocket : protected Socket{protected: int segsize; void setSegmentSize(unsigned mss);public: /** * A method to call in a derived TCPSocket class that is acting * as a server when a connection request is being accepted. The * server can implement protocol specific rules to exclude the * remote socket from being accepted by returning false. The * Peek method can also be used for this purpose. * * @return true if client should be accepted. * @param ia internet host address of the client. * @param port number of the client. */ virtual bool onAccept(const IPV4Host &ia, tpport_t port); /** * Fetch out the socket. */ inline SOCKET getSocket(void) {return so;}; /** * Get the buffer size for servers. */ inline int getSegmentSize(void) {return segsize;}; /** * A TCP "server" is created as a TCP socket that is bound * to a hardware address and port number on the local machine * and that has a backlog queue to listen for remote connection * requests. If the server cannot be created, an exception is * thrown. * * @param bind local ip address or interface to use. * @param port number to bind socket under. * @param backlog size of connection request queue. * @param mss maximum segment size for accepted streams. */ TCPSocket(const IPV4Address &bind, tpport_t port, unsigned backlog = 5, unsigned mss = 536); /** * Create a named tcp socket by service and/or interface id. * For IPV4 we use [host:]svc or [host/]svc for the string. * If we have getaddrinfo, we use that to obtain the addr to * bind for. * * @param name of host interface and service port to bind. * @param backlog size of connection request queue. * @param mss maximum segment size for streaming buffers. */ TCPSocket(const char *name, unsigned backlog = 5, unsigned mss = 536); /** * Return address and port of next connection request. This * can be used instead of OnAccept() to pre-evaluate connection * requests. * * @return host requesting a connection. * @param port number of requestor. */ inline IPV4Host getRequest(tpport_t *port = NULL) const {return Socket::getIPV4Sender(port);} /** * Used to reject the next incoming connection request. */ void reject(void); /** * Used to get local bound address. */ inline IPV4Host getLocal(tpport_t *port = NULL) const {return Socket::getIPV4Local(port);} /** * Used to wait for pending connection requests. * @return true if data packets available. * @param timeout in milliseconds. TIMEOUT_INF if not specified. */ inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) /* not const -- jfc */ {return Socket::isPending(Socket::pendingInput, timeout);} /** * Use base socket handler for ending this socket. */ virtual ~TCPSocket();};#ifdef CCXX_IPV6/** * TCPV6 sockets are used for stream based connected sessions between two * ipv6 sockets. Both error recovery and flow control operate transparently * for a TCP socket connection. The TCP socket base class is primary used * to bind a TCP "server" for accepting TCP streams. * * An implicit and unique TCPV6Socket object exists in Common C++ to represent * a bound ipv6 TCP socket acting as a "server" for receiving connection requests. * This class is not part of TCPStream because such objects normally perform * no physical I/O (read or write operations) other than to specify a listen * backlog queue and perform "accept" operations for pending connections. * The Common C++ TCPV6Socket offers a Peek method to examine where the next * pending connection is coming from, and a Reject method to flush the next * request from the queue without having to create a session. * * The TCPV6Socket also supports a "OnAccept" method which can be called when a * TCPStream related object is created from a TCPSocket. By creating a * TCPStream from a TCPV6Socket, an accept operation automatically occurs, and * the TCPV6Socket can then still reject the client connection through the * return status of it's OnAccept method. * * @author David Sugar <dyfet@tycho.com> * @short bound server for TCP streams and sessions. */class __EXPORT TCPV6Socket : protected Socket{private: int segsize; void setSegmentSize(unsigned mss);public: /** * A method to call in a derived TCPSocket class that is acting * as a server when a connection request is being accepted. The * server can implement protocol specific rules to exclude the * remote socket from being accepted by returning false. The * Peek method can also be used for this purpose. * * @return true if client should be accepted. * @param ia internet host address of the client. * @param port number of the client. */ virtual bool onAccept(const IPV6Host &ia, tpport_t port); /** * Fetch out the socket. */ inline SOCKET getSocket(void) {return so;}; inline int getSegmentSize(void) {return segsize;}; /** * A TCP "server" is created as a TCP socket that is bound * to a hardware address and port number on the local machine * and that has a backlog queue to listen for remote connection * requests. If the server cannot be created, an exception is * thrown. * * @param bind local ip address or interface to use. * @param port number to bind socket under. * @param backlog size of connection request queue. * @param mss maximum segment size of streaming buffer. */ TCPV6Socket(const IPV6Address &bind, tpport_t port, unsigned backlog = 5, unsigned mss = 536); /** * Create a TCP server for a named host interface and service * port. We use [host/]port for specifying the optional host * name and service port since ':' is a valid char for ipv6 * addresses. * * @param name of host interface and service to use. * @param backlog size of connection request queue. * @param mss maximum segment size of streaming buffers. */ TCPV6Socket(const char *name, unsigned backlog = 5, unsigned mss = 536); /** * Return address and port of next connection request. This * can be used instead of OnAccept() to pre-evaluate connection * requests. * * @return host requesting a connection. * @param port number of requestor. */ inline IPV6Host getRequest(tpport_t *port = NULL) const {return Socket::getIPV6Sender(port);} /** * Used to reject the next incoming connection request. */ void reject(void); /** * Used to get local bound address. */ inline IPV6Host getLocal(tpport_t *port = NULL) const {return Socket::getIPV6Local(port);} /** * Used to wait for pending connection requests. * @return true if data packets available. * @param timeout in milliseconds. TIMEOUT_INF if not specified. */ inline bool isPendingConnection(timeout_t timeout = TIMEOUT_INF) /* not const -- jfc */ {return Socket::isPending(Socket::pendingInput, timeout);} /** * Use base socket handler for ending this socket. */ virtual ~TCPV6Socket();};#endif/*:\projects\libraries\cplusplus\commonc++\win32\socket.h(357) : warning C4275: non dll-interface class 'streambuf' used as base for dll-interface class 'TCPStream' c:\program files\microsoft visual studio\vc98\include\streamb.h(69) : see declaration of 'streambuf'c:\projects\libraries\cplusplus\commonc++\win32\socket.h(358) : warning C4275: non dll-interface class 'iostream' used as base for dll-interface class 'TCPStream' c:\program files\microsoft visual studio\vc98\include\iostream.h(66) : see declaration of 'iostream'*/#ifdef _MSC_VER#pragma warning(disable:4275) // disable C4275 warning#endif/** * TCP streams are used to represent TCP client connections to a server * by TCP protocol servers for accepting client connections. The TCP * stream is a C++ "stream" class, and can accept streaming of data to * and from other C++ objects using the << and >> operators. * * TCPStream itself can be formed either by connecting to a bound network * address of a TCP server, or can be created when "accepting" a * network connection from a TCP server. * * @author David Sugar <dyfet@ostel.com>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -