通过 sendto 发送 UDP 数据包

Sending UDP packets via sendto

我想通过 sendto 发送一个 4 字节地址、1 个短整数和 4 字节 int 距离的数据报 UDP 消息。所以它应该正好是 9 个字节。我该怎么做?我已经尝试过转换这些值并将它们放入缓冲区,但没有成功。

sendto(sockfd, &message, sizeof(message), 0, (struct sockaddr*) &server_address, sizeof(server_address));

编辑: 我也试过创建,但是它有12个字节。

 struct msg {
      uint32_t dist;
      uint8_t num ;
       struct in_addr addr;
  
};

最便携的方法是 assemble 自己将字节放入缓冲区:

uint8_t buf[64];  // make sure this is large enough to hold the bytes
uint8_t * b = buf;

uint32_t neDist = htonl(msg.dist);   // send multi-byte values in big-endian form
memcpy(b, &neDist, sizeof(neDist));
b += sizeof(neDist);

memcpy(b, &msg.num, sizeof(msg.num));
b += sizeof(msg.num);

uint32_t neAddr = htonl(msg.addr.s_addr);
memcpy(b, &neAddr, sizeof(neAddr));
b += sizeof(neAddr);

uint32_t numBytesToSend = (b-buf);
sendto(sockfd, buf, numBytesToSend, 0, (struct sockaddr*) &server_address, sizeof(server_address));

...然后在接收端,类似:

uint8_t buf[64];  // make sure this is large enough to hold the bytes
int32_t numBytesReceived = recvfrom(sockfd, buf, sizeof(buf), ...);
if (numBytesReceived > 0)
{
    struct msg m;

    uint8_t * b = buf;
    uint8_t * fib = buf+numBytesReceived;  // pointer to the first invalid byte

    if ((fib-b) >= sizeof(m.dist))
    {
       uint32_t neDist;
       memcpy(&neDist, b, sizeof(neDist));
       b += sizeof(neDist);
       m.dist = ntohl(neDist);  // convert from big-endian back to local-endian

       if ((fib-b) >= sizeof(m.num))
       {
          memcpy(&m.num, b, sizeof(m.num));
          b += sizeof(m.num);

          if ((fib-b) >= sizeof(m.addr.s_addr))
          {
             uint32 neAddr;
             memcpy(&neAddr, b, sizeof(neAddr));
             m.addr.s_addr = ntohl(neAddr);

             printf("At this point, (m) is fully populated and ready for use\n");
          }
       }
    }
}