#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8890


//This is for the test of connection with tello
void udp_server(int sockfd)
{

    socklen_t len;
    char buf[1024] = {0};
    struct sockaddr_in server_addr;
    int n;
    int opt = 1;
    len = sizeof(server_addr);
    memset(&buf, 0, sizeof(buf));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(PORT);

    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));  //当服务器非正常断开的时候重启服务器，不会进入TIME_WAIT状态

    if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        printf("can not bind\n");
        exit(1);
    }

    while (1) {
        printf("========wait for client's request========\n");
        n = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&server_addr, &len);
        buf[n] = '\0';
        printf("receive client's data: %s\n", buf);
        sendto(sockfd, buf, n, 0, (struct sockaddr *)&server_addr, len);
        printf("send data to client: %s\n", buf);
    }

    close(sockfd);
}


/*
int main(int argc, char **argv)
{
    int sockfd;

    if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
        printf("create socket false\n");
        exit(1);
    }

    udp_server(sockfd);

    exit(0);
}
*/




