1 /* This example code is placed in the public domain. */
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
13 #include <netinet/in.h>
17 int udp_connect (void);
18 void udp_close (int sd
);
20 /* Connects to the peer and returns a socket
26 const char *PORT
= "5557";
27 const char *SERVER
= "127.0.0.1";
29 struct sockaddr_in sa
;
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)
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
));
50 err
= connect (sd
, (struct sockaddr
*) & sa
, sizeof (sa
));
53 fprintf (stderr
, "Connect error\n");
60 /* closes the given socket descriptor.