Finally I got answer from MS:
#include <stdio.h> //printf
#include <string.h> //memset
#include <stdlib.h> //exit(0);
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define SERVER "xxx.xxx.xxx.xxx"
#define BUFLEN 512 //Max length of buffer
#define PORT 1709 //The port on which to send data
void die(char* s)
{
perror(s);
exit(1);
}
void UdpTest()
{
struct sockaddr_in si_server;
int s;
unsigned int slen;
struct sockaddr_in si_client; // Additional structure
char buf[BUFLEN];
char message[BUFLEN];
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}
//const char* nic = "wlan0";
//const size_t len = strlen(nic);
memset((char*)&si_server, 0, sizeof(si_server));
si_server.sin_family = AF_INET;
si_server.sin_port = htons(PORT);
if (inet_aton(SERVER, &si_server.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
// Additional initialization
memset((char*)&si_client, 0, sizeof(si_client));
si_client.sin_family = AF_INET;
si_client.sin_addr.s_addr = htonl(INADDR_ANY);
si_client.sin_port = htons(PORT);
bind(s, (struct sockaddr*)&si_client, sizeof(si_client));
for (int i = 0; i < 20; i++)
{
sprintf(message, "This is message %i", i);
printf("Sending: %s\n", message);
//send the message
if (sendto(s, message, strlen(message), 0, (struct sockaddr*)&si_server, sizeof(si_server)) == -1)
{
die("sendto()");
}
//receive a reply and print it
//clear the buffer by filling null, it might have previously received data
memset(buf, '\0', BUFLEN);
int recv_len;
//try to receive some data, this is a blocking call
if (-1 == (recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*)&si_client, &slen)))
{
die("recvfrom()");
}
buf[recv_len] = '\0';
printf("Received: %s\n", buf);
sleep(1);
}
close(s);
}