BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
70b8935
Branches
Tags
master
Blinker
src
linux
Udp4Addr.cpp
implemented UDPv4 socket class
Stefan Schuermans
commited
70b8935
at 2011-11-12 22:47:21
Udp4Addr.cpp
Blame
History
Raw
/* Blinker Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #include <arpa/inet.h> #include <netinet/in.h> #include <sstream> #include <stdlib.h> #include <string> #include <sys/socket.h> #include <sys/types.h> #include "Udp4Addr.h" namespace Blinker { /// constructor Udp4Addr::Udp4Addr() { m_addr.sin_family = AF_INET; m_addr.sin_port = htons(0); m_addr.sin_addr.s_addr = htonl(INADDR_NONE); } /// virtual destructor Udp4Addr::~Udp4Addr() { } /// return address family int Udp4Addr::getFamily() const { return AF_INET; } /** * @brief parse from string format * @param[in] str string format * @return if parsing was successful */ bool Udp4Addr::fromStr(const std::string &str) { std::string::size_type posColon; std::string strIp, strPort; struct in_addr iaIp; unsigned long iPort; const char *szPort; char *szPortEnd; // split address into IP and port posColon = str.find(':'); if (posColon == std::string::npos) return false; strIp = str.substr(0, posColon); strPort = str.substr(posColon + 1); // parse IP if (!inet_aton(strIp.c_str(), &iaIp)) return false; // parse port szPort = strPort.c_str(); iPort = strtoul(szPort, &szPortEnd, 10); if (*szPort == 0 || szPortEnd != 0) return false; m_addr.sin_family = AF_INET; m_addr.sin_port = htons(iPort); m_addr.sin_addr = iaIp; return false; } /** * @brief convert to string format * @return string format */ std::string Udp4Addr::toStr() const { std::stringstream strm; strm << inet_ntoa(m_addr.sin_addr) << ":" << ntohs(m_addr.sin_port); return strm.str(); } } // namespace Blinker