ActiveSocket.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*---------------------------------------------------------------------------*/
  2. /* */
  3. /* ActiveSocket.h - Active Socket Decleration */
  4. /* */
  5. /* Author : Mark Carrier (mark@carrierlabs.com) */
  6. /* */
  7. /*---------------------------------------------------------------------------*/
  8. /* Copyright (c) 2007-2009 CarrierLabs, LLC. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The name of the author may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * 4. The name "CarrierLabs" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * mark@carrierlabs.com.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY
  31. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK CARRIER OR
  34. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *----------------------------------------------------------------------------*/
  43. #ifndef __ACTIVESOCKET_H__
  44. #define __ACTIVESOCKET_H__
  45. #include "SimpleSocket.h"
  46. class CPassiveSocket;
  47. /// Provides a platform independent class to create an active socket.
  48. /// An active socket is used to create a socket which connects to a server.
  49. /// This type of object would be used when an application needs to send/receive
  50. /// data from a server.
  51. class EXPORT CActiveSocket : public CSimpleSocket {
  52. public:
  53. friend class CPassiveSocket;
  54. CActiveSocket(CSocketType type = SocketTypeTcp);
  55. virtual ~CActiveSocket() {
  56. Close();
  57. };
  58. /// Established a connection to the address specified by pAddr.
  59. /// Connection-based protocol sockets (CSocket::SocketTypeTcp) may
  60. /// successfully call Open() only once, however; connectionless protocol
  61. /// sockets (CSocket::SocketTypeUdp) may use Open() multiple times to
  62. /// change their association.
  63. /// @param pAddr specifies the destination address to connect.
  64. /// @param nPort specifies the destination port.
  65. /// @return true if successful connection made, otherwise false.
  66. virtual bool Open(const char *pAddr, uint16 nPort);
  67. private:
  68. /// Utility function used to create a TCP connection, called from Open().
  69. /// @return true if successful connection made, otherwise false.
  70. bool ConnectTCP(const char *pAddr, uint16 nPort);
  71. /// Utility function used to create a UDP connection, called from Open().
  72. /// @return true if successful connection made, otherwise false.
  73. bool ConnectUDP(const char *pAddr, uint16 nPort);
  74. /// Utility function used to create a RAW connection, called from Open().
  75. /// @return true if successful connection made, otherwise false.
  76. bool ConnectRAW(const char *pAddr, uint16 nPort);
  77. private:
  78. struct hostent *m_pHE;
  79. };
  80. #endif /* __ACTIVESOCKET_H__ */