3 #include "stg/sgcp_utils.h"
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netinet/udp.h>
13 using STG::SGCP::UDPProto;
16 : m_sock(socket(AF_INET, SOCK_DGRAM, 0))
25 void UDPProto::connect(const std::string& address, uint16_t port)
27 std::vector<in_addr> addrs = resolve(address);
29 for (size_t i = 0; i < addrs.size(); ++i) {
31 addr.sin_family = AF_INET;
32 addr.sin_port = hton(port);
33 addr.sin_addr = addrs[i];
35 if (::connect(m_sock, reinterpret_cast<const sockaddr*>(&addr), sizeof(addr)) == 0)
39 m_sock = socket(AF_INET, SOCK_DGRAM, 0);
41 throw Error("No more addresses to connect to.");
44 ssize_t UDPProto::write(const void* buf, size_t size)
46 return ::write(m_sock, buf, size);
49 ssize_t UDPProto::read(void* buf, size_t size)
51 return ::read(m_sock, buf, size);