title: NTP implementation C
body: <p>Can someone find what I'm missing on my ntp implementation?</p>
  
  <p>I'm programming an 32-bit microcontroller ARM Cortex M3.
  I have three functions  - wifiSend which calls the bsdUdpClient and bsdUdpServer. The bsdUdpClient sends data to the server and bsdUdpServer listens to the reserved NTP port and receive the data of the NTP server. </p>
  
  <p>I got no error messages at all but the buffer received is empty.</p>
  
  <pre><code>    static void wifiSend(xTimerHandle xTimer){
  
           uint16_t AddrSize = sizeof(SlSockAddrIn_t);
  
           if (STATUS_OK != bsdUdpClient(SERVER_PORT, AddrSize)){
                 printf("Failed to send udp packet\n\r");
                 assert(false);
           }
  
           if (STATUS_OK != bsdUdpServer(SERVER_PORT, AddrSize))
              printf("Failed to receive udp packet\n\r");
      }
  
  
      static returnTypes_t bsdUdpClient(uint16_t port, uint16_t AddrSize){
  
            int16_t Status = (int16_t) ZERO;
  
            memset(packetBuffer, 0, NTP_PACKET_SIZE);
  
            packetBuffer[0] = 0xE3; //0b11100011;   // LI, Version, Mode
            packetBuffer[1] = 0x00;     // Stratum, or type of clock
            packetBuffer[2] = 0x06;     // Polling Interval
            packetBuffer[3] = 0xEC;  // Peer Clock Precision
            // 8 bytes of zero for Root Delay &amp; Root Dispersion
            packetBuffer[12]  = 49;
            packetBuffer[13]  = 0x4E;
            packetBuffer[14]  = 49;
            packetBuffer[15]  = 52;
            Addr.sin_family = SL_AF_INET;
            Addr.sin_port = sl_Htons((uint16_t) port);
            Addr.sin_addr.s_addr = sl_Htonl(SERVER_IP);
  
            SockID = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, (uint32_t) ZERO);
            if (SockID &lt; (int16_t) ZERO)
                 return (SOCKET_ERROR);
  
            Status = sl_SendTo(SockID, packetBuffer, NTP_PACKET_SIZE *     sizeof(uint8_t), (uint32_t) ZERO, (SlSockAddr_t *) &amp;Addr, AddrSize);
  
  
            if (Status &lt;= (int16_t) ZERO) {
                Status = sl_Close(SockID);
                if (Status &lt; 0)
                       return (SEND_ERROR);
                return (SEND_ERROR);
            }
  
            Status = sl_Close(SockID);
            if (Status &lt; 0)
                 return (SEND_ERROR);
  
            return (STATUS_OK);
      }
  
      static returnTypes_t bsdUdpServer(uint16_t port, uint16_t AddrSize){
  
            int16_t Status = (int16_t) ZERO;
  
            LocalAddr.sin_family = SL_AF_INET;
            LocalAddr.sin_port = sl_Htons(5001);
            LocalAddr.sin_addr.s_addr = 0;
  
            SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, (uint32_t) ZERO);
            if (SockID &lt; 0){
                 printf("error on sl_Socket\n\r");
                 return SOCKET_ERROR;
            }
  
            Status = sl_Bind(SockID, (SlSockAddr_t *) &amp;LocalAddr, AddrSize);
            if (Status &lt; 0){
                 printf("problem on sl_Bind\n\r");
                 return SOCKET_ERROR;
            }
  
            Status = sl_RecvFrom(SockID, packetBuffer, NTP_PACKET_SIZE * sizeof(uint8_t), (uint32_t) ZERO, (SlSockAddr_t *) &amp;Addr, &amp;AddrSize);
            if (Status &lt; (int16_t) ZERO){
                 printf("error - no bytes received: %d\n\r", (int16_t)Status);
                 return SOCKET_ERROR;
            }
  
            Status = sl_Close(SockID);
            if (Status &lt; 0)
                 printf("problem on sl_Close\n\r");
  
            uint8_t index3 = packetBuffer[40];
            uint8_t index2 = packetBuffer[41];
            uint8_t index1 = packetBuffer[42];
            uint8_t index0 = packetBuffer[43];
  
            uint16_t highWord = index3 &lt;&lt; 16 | index2;
            uint16_t lowWord = index1 &lt;&lt; 16 | index0;
  
            uint32_t secondsSince1900 = highWord &lt;&lt; 16 | lowWord;
  
            printf("Seconds since 1 Janeiro de 1900: %ld\n\r", secondsSince1900);
  
            return (STATUS_OK);
  
   }
  </code></pre>