/* Blinker
Copyright 2011-2014Stefan Schuermans <stefan@blinkenarea.org>
Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
a blinkenarea.org project */
#include <fcntl.h>
#include <netinet/in.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "Io.h"
#include "Udp4Addr.h"
#include "Udp4Sock.h"
namespace Blinker {
/// constructor
Udp4Sock::Udp4Sock()
{
int flags, opt;
// create socket
m_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (m_fd == -1)
return;
// switch to nonblocking mode
flags = fcntl(m_fd, F_GETFL);
if (flags == -1 || fcntl(m_fd, F_SETFL, flags | O_NONBLOCK) != 0) {
close(m_fd);
m_fd = -1;
return;
}
// enable address re-use
opt = 1;
if (setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) != 0) {
close(m_fd);
m_fd = -1;
return;
}
}
/// destructor
Udp4Sock::~Udp4Sock()
{
// exit if not initialized
if (m_fd == -1)
return;