-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_client.hpp
42 lines (30 loc) · 1.15 KB
/
tcp_client.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//=============================================================================
// Description: A simple tcp client class
// Author: Marcelo Arroyo (2014)
//=============================================================================
#ifndef TCP_CLIENT_H
#define TCP_CLIENT_H
#include <string> // for string
#include <vector> // for vector
#include <arpa/inet.h> // for struct inet_addr
class tcp_client {
public:
tcp_client();
// Destructor: it disconnect if connection is alive
~tcp_client();
// connect to a node (ip or name) and service (name or port)
bool connect(std::string node, std::string service);
// close connection
void disconnect();
// send data, return bytes sent
int send(std::vector<unsigned char> data);
int send(std::string data);
// receive data
std::vector<unsigned char> receive(int max_length=4096);
std::string receive_string(int max_length=4096);
inline bool is_connected() const { return connected; }
protected:
int s; // the socket
bool connected; // Is the connection alive?
};
#endif