#include #include #include #include //printf #include //exit(0); #include //memset #include #include #include #define SERVER "127.0.0.1" #define BUFLEN 1024 // Max length of buffer #define PORT 8888 // The port on which to send data class udpsocket { struct sockaddr_in addr; int s, slen = sizeof(addr); public: udpsocket(int port); udpsocket(const udpsocket&) = delete; udpsocket& operator=(const udpsocket&) = delete; udpsocket(udpsocket&&); udpsocket& operator=(udpsocket&&); void write(const std::string&,const std::string& dest, int port); void write(const std::vector&,const std::string& dest, int port); std::vector receive(); void close(); }; udpsocket::udpsocket(int port) { if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { throw std::logic_error(std::string("Socket creation failed: ") + strerror(errno)); } std::memset((char*)&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port); if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) { throw std::logic_error(std::string("Socket binding failed: ") + strerror(errno)); } } udpsocket::udpsocket(udpsocket&& o) : addr(o.addr),s(o.s){ o.s = 0; } udpsocket& udpsocket::operator=(udpsocket&& o){ addr = o.addr; s = o.s; o.s = 0; return *this; } void udpsocket::write(const std::string& msg, const std::string& dest, int port){ std::vector _msg(msg.begin(), msg.end()); write(_msg, dest, port); } void udpsocket::write(const std::vector& msg, const std::string& dest, int port){ struct sockaddr_in _dest; std::memset((char*)&_dest, 0, sizeof(sockaddr_in)); _dest.sin_family = AF_INET; _dest.sin_port = htons(PORT); if (inet_aton(dest.c_str(), &_dest.sin_addr) == 0) { throw std::logic_error(std::string("inet_aton() failed: ") + strerror(errno)); } if (sendto(s, msg.data(), msg.size(), 0, (struct sockaddr*)&dest, slen) < 0) { throw std::logic_error(std::string("Could not send packet: ") + strerror(errno)); } } std::vector udpsocket::receive(){ std::vector ret(1024); if (read(s, ret.data(), 1024) <= 0) { throw std::logic_error(std::string("Could not receive packet: ") + strerror(errno)); } return ret; } void udpsocket::close(){ shutdown(s, 2); } void die(const std::string& s) { perror(s.c_str()); exit(1); } int main() { udpsocket sock(5566); sock.write("Hallo", "127.0.0.1",5555); std::cin.get(); return 0; }