corrected copyright notices
[gnutls.git] / doc / examples / udp.c
blob0c48ac1b5b7ed8b9b4f8ae2c3a8861ebf6cb9805
1 /* This example code is placed in the public domain. */
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
13 #include <netinet/in.h>
14 #include <unistd.h>
16 /* udp.c */
17 int udp_connect (void);
18 void udp_close (int sd);
20 /* Connects to the peer and returns a socket
21 * descriptor.
23 extern int
24 udp_connect (void)
26 const char *PORT = "5557";
27 const char *SERVER = "127.0.0.1";
28 int err, sd, optval;
29 struct sockaddr_in sa;
31 /* connects to server
33 sd = socket (AF_INET, SOCK_DGRAM, 0);
35 memset (&sa, '\0', sizeof (sa));
36 sa.sin_family = AF_INET;
37 sa.sin_port = htons (atoi (PORT));
38 inet_pton (AF_INET, SERVER, &sa.sin_addr);
40 #if defined(IP_DONTFRAG)
41 optval = 1;
42 setsockopt (sd, IPPROTO_IP, IP_DONTFRAG,
43 (const void *) &optval, sizeof (optval));
44 #elif defined(IP_MTU_DISCOVER)
45 optval = IP_PMTUDISC_DO;
46 setsockopt(sd, IPPROTO_IP, IP_MTU_DISCOVER,
47 (const void*) &optval, sizeof (optval));
48 #endif
50 err = connect (sd, (struct sockaddr *) & sa, sizeof (sa));
51 if (err < 0)
53 fprintf (stderr, "Connect error\n");
54 exit (1);
57 return sd;
60 /* closes the given socket descriptor.
62 extern void
63 udp_close (int sd)
65 close (sd);