Listing 2. udpsnd
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char **argv) {
int sd, nsd, saddr_len, n;
int uno=1;
struct sockaddr_in addr;
char buffer[1000];
if (argc<3) {
printf("udpsnd <address> <string>\n");
exit(0);
}
bzero(&addr, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(6600);
sd = socket(PF_INET, SOCK_DGRAM, 0);
inet_aton(argv[1], &addr.sin_addr);
strcpy(buffer, argv[2]);
sendto(sd, buffer, strlen(buffer), 0,
(struct sockaddr *)&addr, sizeof(addr));
close(sd);
}
Copyright © 1994 - 2018 Linux Journal. All rights reserved.