#include #include #include #include #include #include /* Send a UDP packet using C Author: Tim Pierson, Dartmouth CS55, Winter 2021 From Du: Computer and Internet Security compile gcc send_udp.c -o send_udp run: ./send_upd set up netcat to listen for packet before sending nc -luv 9090 */ void main() { struct sockaddr_in dest_info; char * dest_addr = "127.0.0.1"; int port = 9090; char *data = "Hello World (in C!)\n"; //Create network socket int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); //Provide needed data memset((char *) &dest_info,0,sizeof(dest_info)); dest_info.sin_family = AF_INET; dest_info.sin_addr.s_addr = inet_addr(dest_addr); dest_info.sin_port = htons(port); //send packet sendto(sock, data, strlen(data), 0, (struct sockaddr *) &dest_info, sizeof(dest_info)); close(sock); }