A következo program a megadott host megadott UDP portjára adatokat küld.
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #define DATA "Isten hozta ornagyur ..." /* * Hivas: udpkliens hostname port-nr. * * Ez a program a parametereben megadott hostra, es a megadott * UDP-portra kuld egy datagramot. */ main(argc, argv) int argc; char *argv[]; { int sock; struct sockaddr_in server; struct hostent *hp, *gethostbyname(); sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("hiba a socket-nel"); exit(1); } hp = gethostbyname(argv[1]); if (hp == NULL) { fprintf(stderr, "%s: ismeretlen host\n", argv[1]); exit(2); } bcopy(hp->h_addr, &server.sin_addr, hp->h_length); server.sin_family = AF_INET; server.sin_port = htons(atoi(argv[2])); if (sendto(sock,DATA,sizeof(DATA),0,&server,sizeof(server))<0) perror("hiba a sendto-nal"); close(sock); }