libkovan  1
The kovan standard library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
socket.hpp
Go to the documentation of this file.
1 #ifndef _SOCKET_HPP_
2 #define _SOCKET_HPP_
3 
4 #include <netinet/in.h>
5 
6 typedef int socket_fd_t;
7 
8 struct sockaddr;
9 
10 class Address
11 {
12 public:
13  Address(const char *const host, const unsigned short port);
14  Address(const sockaddr_in &addr);
15  Address();
16 
17  bool isValid() const;
18 
19  bool setHost(const char *const host);
20  void setPort(const unsigned short port);
21 
22  unsigned short port() const;
23 
24  const sockaddr *addr() const;
25  socklen_t addrLength() const;
26 
27  const char *ip() const;
28 
29 private:
30  bool m_valid;
31  sockaddr_in m_addr;
32 };
33 
34 class Socket
35 {
36 public:
37  Socket();
38 
39  bool open(int domain, int type, int protocol);
40  bool isOpen() const;
41  bool setBlocking(const bool blocking);
42  bool setReusable(const bool reusable);
43  bool bind(const unsigned short port);
44  bool connect(const Address &addr);
45  bool disconnect();
46 
47  bool close();
48 
49  ssize_t recv(void *const buffer, const size_t length, int flags = 0);
50  ssize_t recvfrom(void *const buffer, const size_t length, Address &address, int flags = 0);
51 
52  ssize_t send(const void *const buffer, const size_t length, int flags = 0);
53  ssize_t sendto(const void *const buffer, const size_t length, const Address &dest, int flags = 0);
54 
55  socket_fd_t fd() const;
56 
57  static Socket udp();
58  static Socket tcp();
59 
60 private:
61  socket_fd_t m_fd;
62 };
63 
64 #endif