Skip to content

Instantly share code, notes, and snippets.

@3p3r
Created June 10, 2018 21:27
Show Gist options
  • Save 3p3r/a7e534164c6d838a107f96d7b2f5f1dd to your computer and use it in GitHub Desktop.
Save 3p3r/a7e534164c6d838a107f96d7b2f5f1dd to your computer and use it in GitHub Desktop.
quick and dirty C++ function to send a udp packet to an address
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
/**
* @fn udp_send
* @summary quick and dirty C++ function to send a udp packet to an address
* @example udp_send("hello", 6 /* strlen("hello")+1 */, "127.0.0.1", 9898)
*/
bool udp_send(const char *data, int len, const char* host, unsigned port) {
sockaddr_in servaddr;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0) return false; // failed to open a socket, permission issues?
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
if (inet_aton(host, &servaddr.sin_addr)==0)
return close(fd), false; // failed to parse the host address
servaddr.sin_port = htons(port);
if (sendto(fd, data, len, 0, (sockaddr*)&servaddr, sizeof(servaddr)) < 0)
return close(fd), false; // send failed
return close(fd), true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment