Created
February 17, 2018 05:16
-
-
Save tjhv/8582cfd877c4f6c5327446fd1add8348 to your computer and use it in GitHub Desktop.
Old snippet from back in the day.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Jan 4 2009 | |
#include <netdb.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
int send_datagram(char* dstaddr, int dstport, char* buffer) { | |
struct hostent* peer; | |
if (!(peer = gethostbyname(dstaddr))) { | |
printf("Unknown host: %s\r\n", dstaddr); | |
return 1; | |
} | |
int fd; | |
if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { | |
printf("Error creating socket.\r\n"); | |
return 1; | |
} | |
struct sockaddr_in* datagram; | |
datagram->sin_family = AF_INET; | |
datagram->sin_port = htons(dstport); | |
memcpy(&datagram->sin_addr, peer->h_addr, peer->h_length); | |
if (sendto(fd, &buffer, sizeof buffer, 0, (struct sockaddr*) &datagram, sizeof datagram) == -1) { | |
printf("Error sending packet.\r\n"); | |
return 1; | |
} | |
close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment