text: #include "UDPServer.h"
  
  #include <string>
  #include <string.h>
  #include <sys/socket.h>
  #include <arpa/inet.h>
  #include <linux/socket.h>
  #include <unistd.h>
  
  UDPServer::UDPServer()
  {
  
  }
  
  UDPServer::~UDPServer()
  {
  
  }
  
  bool UDPServer::createServer(int &serverSD, int port)
  {
  	if((serverSD = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  	{
  		return false;
  	}
  
  	//Set timeout socket opt
  	struct timeval tv;
  	tv.tv_sec = 3;
  	tv.tv_usec = 0;
  	if (setsockopt(serverSD, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)) < 0)
  		perror("SO_RCVTIMEO error");
  
  	//Set reuse address socket opt
  	int reuseAddr = 1;
  	int state = setsockopt(serverSD, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseAddr, sizeof(reuseAddr));
  	if(state)
  		perror("SO_REUSEADDR error");
  
  	//Try bind udp server
  	struct sockaddr_in sin;
  	memset((char*)&sin, '\0', sizeof(sin));
  	sin.sin_family = AF_INET;
  	sin.sin_port = htons(port);
  	sin.sin_addr.s_addr = htonl(INADDR_ANY);
  	memset(&(sin.sin_zero), 0, 8);
  
  	if (bind(serverSD, (struct sockaddr *)&sin, sizeof(struct sockaddr)))
  	{
  		close(serverSD);
  		return false;
  	}
  
  	return true;
  }
  
  int UDPServer::recvData(int fd, void *buf, int size)
  {
  	return recvfrom(fd, &buf, sizeof(size), 0, (struct sockaddr *) NULL, NULL);
  }
  