/* Example UDP server program */ /* Adapted from http://en.wikipedia.org/wiki/Berkeley_sockets */ #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int sock; struct sockaddr_in sa; int bytes_sent; char buffer[200]; strcpy(buffer, "hello world!"); /* create socket */ sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (-1 == sock) /* if socket failed to initialize, exit */ { printf("Error Creating Socket"); exit(EXIT_FAILURE); } /* init sa with server address and port */ memset(&sa, 0, sizeof sa); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr("127.0.0.1"); sa.sin_port = htons(7654); /* send buffer using socket and destination sa */ bytes_sent = sendto(sock, buffer, strlen(buffer), 0,(struct sockaddr*)&sa, sizeof sa); /* report errors */ if (bytes_sent < 0) { printf("Error sending packet: %s\n", strerror(errno)); exit(EXIT_FAILURE); } /* close the socket when finished */ close(sock); return 0; }