|
@@ -1,59 +1,34 @@
|
|
-#include <arpa/inet.h>
|
|
|
|
-#include <iostream>
|
|
|
|
-#include <stdexcept>
|
|
|
|
-#include <netdb.h> //printf
|
|
|
|
-#include <stdlib.h> //exit(0);
|
|
|
|
-#include <cstring> //memset
|
|
|
|
-#include <sys/socket.h>
|
|
|
|
-#include <unistd.h>
|
|
|
|
-#include <netinet/in.h>
|
|
|
|
-#include <sys/types.h>
|
|
|
|
-#include <vector>
|
|
|
|
-
|
|
|
|
-#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<char>&,const std::string& dest, int port);
|
|
|
|
- std::vector<char> receive();
|
|
|
|
- void close();
|
|
|
|
-};
|
|
|
|
-udpsocket::udpsocket(int port) {
|
|
|
|
|
|
+#include "udpsocket.hpp"
|
|
|
|
+udpsocket::udpsocket(int port) : m_port(port){
|
|
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
|
|
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
|
|
throw std::logic_error(std::string("Socket creation failed: ") + strerror(errno));
|
|
throw std::logic_error(std::string("Socket creation failed: ") + strerror(errno));
|
|
}
|
|
}
|
|
std::memset((char*)&addr, 0, sizeof(addr));
|
|
std::memset((char*)&addr, 0, sizeof(addr));
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
- addr.sin_port = htons(port);
|
|
|
|
|
|
+ addr.sin_port = htons(this->port());
|
|
|
|
|
|
if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
|
if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
|
throw std::logic_error(std::string("Socket binding failed: ") + strerror(errno));
|
|
throw std::logic_error(std::string("Socket binding failed: ") + strerror(errno));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-udpsocket::udpsocket(udpsocket&& o) : addr(o.addr),s(o.s) {
|
|
|
|
|
|
+udpsocket::udpsocket(udpsocket&& o) : addr(o.addr),s(o.s),m_port(o.m_port) {
|
|
o.s = 0;
|
|
o.s = 0;
|
|
|
|
+ o.m_port = 0;
|
|
}
|
|
}
|
|
udpsocket& udpsocket::operator=(udpsocket&& o){
|
|
udpsocket& udpsocket::operator=(udpsocket&& o){
|
|
addr = o.addr;
|
|
addr = o.addr;
|
|
s = o.s;
|
|
s = o.s;
|
|
|
|
+ m_port = o.m_port;
|
|
o.s = 0;
|
|
o.s = 0;
|
|
|
|
+ o.m_port = 0;
|
|
return *this;
|
|
return *this;
|
|
}
|
|
}
|
|
-void udpsocket::write(const std::string& msg, const std::string& dest, int port){
|
|
|
|
|
|
+void udpsocket::write(const std::string& msg, const std::string& dest, int port)const{
|
|
std::vector<char> _msg(msg.begin(), msg.end());
|
|
std::vector<char> _msg(msg.begin(), msg.end());
|
|
write(_msg, dest, port);
|
|
write(_msg, dest, port);
|
|
}
|
|
}
|
|
-void udpsocket::write(const std::vector<char>& msg, const std::string& dest, int port){
|
|
|
|
|
|
+void udpsocket::write(const std::vector<char>& msg, const std::string& dest, int port)const{
|
|
hostent *server_host;
|
|
hostent *server_host;
|
|
errno = 0;
|
|
errno = 0;
|
|
server_host = gethostbyname(dest.c_str());
|
|
server_host = gethostbyname(dest.c_str());
|
|
@@ -66,31 +41,22 @@ void udpsocket::write(const std::vector<char>& msg, const std::string& dest, int
|
|
memcpy(&server_addr.sin_addr, server_host->h_addr,
|
|
memcpy(&server_addr.sin_addr, server_host->h_addr,
|
|
sizeof(struct in_addr)); server_addr.sin_port = htons(port);
|
|
sizeof(struct in_addr)); server_addr.sin_port = htons(port);
|
|
/* send a message */
|
|
/* send a message */
|
|
- while(true)
|
|
|
|
if(sendto(s, msg.data(), msg.size(), 0,(const sockaddr*)&server_addr, sizeof(server_addr)) < 0){
|
|
if(sendto(s, msg.data(), msg.size(), 0,(const sockaddr*)&server_addr, sizeof(server_addr)) < 0){
|
|
throw std::logic_error(std::string("Could not send packet: ") + strerror(errno));
|
|
throw std::logic_error(std::string("Could not send packet: ") + strerror(errno));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-std::vector<char> udpsocket::receive(){
|
|
|
|
|
|
+std::vector<char> udpsocket::receive()const{
|
|
std::vector<char> ret(1024);
|
|
std::vector<char> ret(1024);
|
|
- if (read(s, ret.data(), 1024) <= 0) {
|
|
|
|
|
|
+ int l;
|
|
|
|
+ if ((l = read(s, ret.data(), 1024)) <= 0) {
|
|
throw std::logic_error(std::string("Could not receive packet: ") + strerror(errno));
|
|
throw std::logic_error(std::string("Could not receive packet: ") + strerror(errno));
|
|
}
|
|
}
|
|
|
|
+ ret.resize(l);
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
void udpsocket::close(){
|
|
void udpsocket::close(){
|
|
shutdown(s, 2);
|
|
shutdown(s, 2);
|
|
}
|
|
}
|
|
-void die(const std::string& s) {
|
|
|
|
- perror(s.c_str());
|
|
|
|
- exit(1);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-int main() {
|
|
|
|
- udpsocket sock(5566);
|
|
|
|
- std::vector<char> d(512,'a');
|
|
|
|
- while(true)
|
|
|
|
- sock.write(d.data(), "8.8.8.8" ,5555);
|
|
|
|
- std::cin.get();
|
|
|
|
- return 0;
|
|
|
|
|
|
+int udpsocket::port()const {
|
|
|
|
+ return this->m_port;
|
|
}
|
|
}
|