From 09b7e077f10fe324774f759086f3596947d69b19 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Mon, 6 Aug 2018 18:35:13 +0200 Subject: Send multiple packets in one system call --- src/UDPClient/UDPClient.cpp | 13 +++++++++++++ src/UDPClient/UDPClient.h | 1 + 2 files changed, 14 insertions(+) (limited to 'src/UDPClient') diff --git a/src/UDPClient/UDPClient.cpp b/src/UDPClient/UDPClient.cpp index 1d427ba..b9d55d0 100644 --- a/src/UDPClient/UDPClient.cpp +++ b/src/UDPClient/UDPClient.cpp @@ -64,12 +64,21 @@ { throw udp_client_server_runtime_error(("invalid address or port: \"" + addr + ":" + decimal_port + "\"").c_str()); } + f_socket = socket(f_addrinfo->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP); if(f_socket == -1) { freeaddrinfo(f_addrinfo); throw udp_client_server_runtime_error(("could not create socket for: \"" + addr + ":" + decimal_port + "\"").c_str()); } + + if (connect(f_socket, f_addrinfo->ai_addr, f_addrinfo->ai_addrlen)) + { + close(f_socket); + freeaddrinfo(f_addrinfo); + throw udp_client_server_runtime_error(("could not connect socket for: \"" + addr + ":" + decimal_port + "\"").c_str()); + } + printf("Created client %d\n", f_port); } @@ -140,3 +149,7 @@ int UDPClient::send(const char *msg, std::size_t size){ return sendto(f_socket, msg, size, 0, f_addrinfo->ai_addr, f_addrinfo->ai_addrlen); } + + int UDPClient::msend(int n, struct mmsghdr *msg){ + return sendmmsg(f_socket, msg, n, 0); + } diff --git a/src/UDPClient/UDPClient.h b/src/UDPClient/UDPClient.h index f6cf0d6..f1c8a8d 100644 --- a/src/UDPClient/UDPClient.h +++ b/src/UDPClient/UDPClient.h @@ -33,6 +33,7 @@ public: std::string get_addr() const; int send(const char *msg, size_t size); + int msend(int n, struct mmsghdr *msg); private: int f_socket; -- cgit v1.2.3