DAVID4 SDK  1.8.7
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
TcpClient.h
1 //=============================================================================
2 // See License in Related Pages
3 //=============================================================================
4 
5 #pragma once
6 
7 #ifndef DAVID_SDK_TCP_CLIENT_H
8 #define DAVID_SDK_TCP_CLIENT_H
9 
10 #include "davidSDK/Common.h"
11 
12 #include <stdint.h>
13 #ifdef _WIN32
14  #include <winsock2.h>
15  #include <ws2tcpip.h>
16  #ifndef QT_VERSION
17  #define close closesocket
18  #endif
19 #else // not _WIN32
20  #include <sys/types.h>
21  #include <sys/socket.h>
22  #include <sys/time.h>
23  #include <unistd.h>
24  #include <netinet/in.h>
25  #include <netdb.h>
26  #define SOCKET int
27  #define closesocket close
28 #endif
29 
30 
31 namespace david {
32 
33 //=============================================================================
34 // TcpClient
35 //=============================================================================
36 
37 /// Implementation of a TCP client.
38 class TcpClient
39 {
40 public:
41  //-------------------------------------------------------------------------
42  // (De-)Initialization
43  //-------------------------------------------------------------------------
44 
45  /// Constructor.
46  /// @param[in] serverAddress Remote IP address. Use 127.0.0.1 for localhost.
47  /// @param[in] serverPort Remote port.
48  TcpClient(const std::string& serverAddress, uint16_t serverPort);
49 
50  /// Destructor.
51  virtual ~TcpClient();
52 
53  /// Connect to the remote machine.
54  /// True if successful, false otherwise.
55  bool Connect();
56 
57  /// Close socket.
58  void Close();
59 
60  //-------------------------------------------------------------------------
61  // Info
62  //-------------------------------------------------------------------------
63 
64  /// Get remote IP address.
65  /// @return Server IP address.
66  std::string GetServerAddress() const;
67 
68  /// Get remote port.
69  /// @return Server port.
70  uint16_t GetServerPort() const;
71 
72  //-------------------------------------------------------------------------
73  // Data transfer
74  //-------------------------------------------------------------------------
75 
76  /// Receive data from the network. Function blocks until data is received.
77  /// @param[out] data Gets the received data.
78  /// @return Number of bytes received or -1 in case of an error.
79  int64_t ReceiveString(std::string& data);
80 
81  /// Receive binary data from the network.
82  /// @note This method will block until 'numBytes' were received.
83  /// @param[out] data Destination buffer with size of at least 'numBytes'.
84  /// @param[in] numBytes Number of bytes to be received.
85  /// @return True, if 'numBytes' were received.
86  bool ReceiveBinary(void* data, size_t numBytes);
87 
88  /// Send data.
89  /// @param[in] data Data to be sent.
90  /// @return Number of bytes sent or -1 in case of an error.
91  int64_t SendString(const std::string& data);
92 
93  /// Send binary data to network.
94  /// @note This method will block until 'numBytes' bytes are send.
95  /// @param[in] data Source buffer with size of at least 'numBytes'.
96  /// @param[in] numBytes Number of bytes to be send.
97  /// @return True, if 'numBytes' were send.
98  bool SendBinary(const void* data, size_t numBytes);
99 
100 private:
101  //-------------------------------------------------------------------------
102  // Member variables
103  //-------------------------------------------------------------------------
104 
105  std::string m_serverAddress; ///< Server address
106  uint16_t m_serverPort; ///< Server port
107  SOCKET m_socket; ///< Socket descriptor.
108 // socklen_t m_sockaddrlen; ///< Remote socket address length.
109 // sockaddr_storage m_sockaddr; ///< Remote socket address.
110 };
111 
112 
113 } // namespace
114 
115 #endif // DAVID_SDK_TCP_CLIENT_H