'UDP Packets coming in with wrong formatting

I'm currently working to get some software that is currently running on windows ported over to ubuntu. One aspect of this software handles udp packets with the location of some of our assets using SFML. For some reason after moving OS's the packets are coming in as U� (always with the � symbol.) is this a posix problem? Here is the code I was using:

int32_t
SocketUdpClient::recv(std::string *data)
{
   sf::Socket::Status recvStatus = sf::Socket::Error;
   
   if( m_bOpen && data != NULL )
   {
      char          buf[4096];
      std::size_t   bytesRead;
      sf::IpAddress senderIp;
      uint16_t      senderPort;

      recvStatus = m_socket.receive( buf, 4096, bytesRead, senderIp, senderPort );

      if (recvStatus == sf::Socket::Done)
      {
        std::cout << "Received " << bytesRead << " bytes from " << senderIp << " on port " << senderPort << std::endl;
        std::cout << buf <<std::endl;
          m_senderIp = senderIp.toString();
          if (m_listenAll) 
          { 
              m_peerIp = senderIp.toString();
              
              printf("SocketUdpClient::recvfrom m_senderIp = %s, m_peerIp = %s\n", m_senderIp.c_str(), m_peerIp.c_str());
          }

          if (m_senderIp.compare(m_peerIp) == 0)
          {
              *data = std::string(buf, bytesRead);
              return bytesRead;
              //return senderIp.toString();
          }
      }
      else if (recvStatus == sf::Socket::NotReady) 
      {
          return 0;
      }
   }
   
   return -1;
}


Solution 1:[1]

Change this:

  recvStatus = m_socket.receive( buf, 4096, bytesRead, senderIp, senderPort );

  if (recvStatus == sf::Socket::Done)
  {
    std::cout << "Received " << bytesRead << " bytes from " << senderIp << " on port " << senderPort << std::endl;
    std::cout << buf <<std::endl;

To this:

  bytesRead = 0; // always a good idea
  recvStatus = m_socket.receive( buf, 4095 /* 1 less than buffer size*/, bytesRead, senderIp, senderPort );

  if (recvStatus == sf::Socket::Done)
  {
    std::cout << "Received " << bytesRead << " bytes from " << senderIp << " on port " << senderPort << std::endl;

    buf[bytesRead] = '\0';  // null terminate
    std::cout << buf <<std::endl;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 selbie