You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.7KB

  1. #ifndef STK_UDPSOCKET_H
  2. #define STK_UDPSOCKET_H
  3. #include "Socket.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class UdpSocket
  7. \brief STK UDP socket server/client class.
  8. This class provides a uniform cross-platform UDP socket
  9. server/client interface. Methods are provided for reading or
  10. writing data buffers. The constructor creates a UDP socket and
  11. binds it to the specified port. Note that only one socket can be
  12. bound to a given port on the same machine.
  13. UDP sockets provide unreliable, connection-less service. Messages
  14. can be lost, duplicated, or received out of order. That said,
  15. data transmission tends to be faster than with TCP connections and
  16. datagrams are not potentially combined by the underlying system.
  17. The user is responsible for checking the values returned by the
  18. read/write methods. Values less than or equal to zero indicate
  19. the occurence of an error.
  20. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  21. */
  22. /***************************************************/
  23. class UdpSocket : public Socket
  24. {
  25. public:
  26. //! Default constructor creates a local UDP socket on port 2006 (or the specified port number).
  27. /*!
  28. An StkError will be thrown if a socket error occurs during instantiation.
  29. */
  30. UdpSocket( int port = 2006 );
  31. //! The class destructor closes the socket instance.
  32. ~UdpSocket();
  33. //! Set the address for subsequent outgoing data sent via the \e writeBuffer() function.
  34. /*!
  35. An StkError will be thrown if the host is unknown.
  36. */
  37. void setDestination( int port = 2006, std::string hostname = "localhost" );
  38. //! Send a buffer to the address specified with the \e setDestination() function. Returns the number of bytes written or -1 if an error occurs.
  39. /*!
  40. This function will fail if the default address (set with \e setDestination()) is invalid or has not been specified.
  41. */
  42. int writeBuffer(const void *buffer, long bufferSize, int flags = 0);
  43. //! Read an input buffer, up to length \e bufferSize. Returns the number of bytes read or -1 if an error occurs.
  44. int readBuffer(void *buffer, long bufferSize, int flags = 0);
  45. //! Write a buffer to the specified socket. Returns the number of bytes written or -1 if an error occurs.
  46. int writeBufferTo(const void *buffer, long bufferSize, int port, std::string hostname = "localhost", int flags = 0 );
  47. protected:
  48. //! A protected function for use in writing a socket address structure.
  49. /*!
  50. An StkError will be thrown if the host is unknown.
  51. */
  52. void setAddress( struct sockaddr_in *address, int port = 2006, std::string hostname = "localhost" );
  53. struct sockaddr_in address_;
  54. bool validAddress_;
  55. };
  56. } // stk namespace
  57. #endif