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.

66 lines
2.2KB

  1. #ifndef STK_TCPSERVER_H
  2. #define STK_TCPSERVER_H
  3. #include "Socket.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class TcpServer
  7. \brief STK TCP socket server class.
  8. This class provides a uniform cross-platform TCP socket server
  9. interface. Methods are provided for reading or writing data
  10. buffers to/from connections.
  11. TCP sockets are reliable and connection-oriented. A TCP socket
  12. server must accept a connection from a TCP client before data can
  13. be sent or received. Data delivery is guaranteed in order,
  14. without loss, error, or duplication. That said, TCP transmissions
  15. tend to be slower than those using the UDP protocol and data sent
  16. with multiple \e write() calls can be arbitrarily combined by the
  17. underlying system.
  18. The user is responsible for checking the values
  19. returned by the read/write methods. Values
  20. less than or equal to zero indicate a closed
  21. or lost connection or the occurence of an error.
  22. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  23. */
  24. /***************************************************/
  25. class TcpServer : public Socket
  26. {
  27. public:
  28. //! Default constructor creates a local socket server on port 2006 (or the specified port number).
  29. /*!
  30. An StkError will be thrown if a socket error occurs during instantiation.
  31. */
  32. TcpServer( int port = 2006 );
  33. //! The class destructor closes the socket instance, breaking any existing connections.
  34. ~TcpServer();
  35. //! Extract the first pending connection request from the queue and create a new connection, returning the descriptor for the accepted socket.
  36. /*!
  37. If no connection requests are pending and the socket has not
  38. been set non-blocking, this function will block until a connection
  39. is present. If an error occurs, -1 is returned.
  40. */
  41. int accept( void );
  42. //! Write a buffer over the socket connection. Returns the number of bytes written or -1 if an error occurs.
  43. int writeBuffer(const void *buffer, long bufferSize, int flags = 0);
  44. //! Read a buffer from the socket connection, up to length \e bufferSize. Returns the number of bytes read or -1 if an error occurs.
  45. int readBuffer(void *buffer, long bufferSize, int flags = 0);
  46. protected:
  47. };
  48. } // stk namespace
  49. #endif