|
Packit Service |
4684c1 |
/* This example code is placed in the public domain. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
4684c1 |
#include <config.h>
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <stdio.h>
|
|
Packit Service |
4684c1 |
#include <stdlib.h>
|
|
Packit Service |
4684c1 |
#include <string.h>
|
|
Packit Service |
4684c1 |
#include <sys/types.h>
|
|
Packit Service |
4684c1 |
#include <sys/socket.h>
|
|
Packit Service |
4684c1 |
#include <arpa/inet.h>
|
|
Packit Service |
4684c1 |
#include <netinet/in.h>
|
|
Packit Service |
4684c1 |
#include <unistd.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* udp.c */
|
|
Packit Service |
4684c1 |
int udp_connect(void);
|
|
Packit Service |
4684c1 |
void udp_close(int sd);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Connects to the peer and returns a socket
|
|
Packit Service |
4684c1 |
* descriptor.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
extern int udp_connect(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
const char *PORT = "5557";
|
|
Packit Service |
4684c1 |
const char *SERVER = "127.0.0.1";
|
|
Packit Service |
4684c1 |
int err, sd;
|
|
Packit Service |
4684c1 |
#if defined(IP_DONTFRAG) || defined(IP_MTU_DISCOVER)
|
|
Packit Service |
4684c1 |
int optval;
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
struct sockaddr_in sa;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* connects to server
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
sd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
memset(&sa, '\0', sizeof(sa));
|
|
Packit Service |
4684c1 |
sa.sin_family = AF_INET;
|
|
Packit Service |
4684c1 |
sa.sin_port = htons(atoi(PORT));
|
|
Packit Service |
4684c1 |
inet_pton(AF_INET, SERVER, &sa.sin_addr);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#if defined(IP_DONTFRAG)
|
|
Packit Service |
4684c1 |
optval = 1;
|
|
Packit Service |
4684c1 |
setsockopt(sd, IPPROTO_IP, IP_DONTFRAG,
|
|
Packit Service |
4684c1 |
(const void *) &optval, sizeof(optval));
|
|
Packit Service |
4684c1 |
#elif defined(IP_MTU_DISCOVER)
|
|
Packit Service |
4684c1 |
optval = IP_PMTUDISC_DO;
|
|
Packit Service |
4684c1 |
setsockopt(sd, IPPROTO_IP, IP_MTU_DISCOVER,
|
|
Packit Service |
4684c1 |
(const void *) &optval, sizeof(optval));
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
err = connect(sd, (struct sockaddr *) &sa, sizeof(sa));
|
|
Packit Service |
4684c1 |
if (err < 0) {
|
|
Packit Service |
4684c1 |
fprintf(stderr, "Connect error\n");
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return sd;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* closes the given socket descriptor.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
extern void udp_close(int sd)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
close(sd);
|
|
Packit Service |
4684c1 |
}
|