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