为什么我的包裹正面有一堆╠?

Why are there a bunch of ╠ on the front of my packet?

编辑:第一个回答了标题中的问题。它是从我的调试器初始化的内存。但是我没有让程序运行。只是要回到 boost::asio 我的套接字。插座很痛。

我正在关注 this tutorial 如何开始使用套接字发送和接收数据包。我已尽我所能按照教程进行操作,但我的消息前面出现了一堆╠。有人可以解释为什么我得到这些吗?他们的 ASCII 码是 204,所以不是 null 也不是 0xFF。如果您还需要什么,请告诉我。

奖励积分:为什么我的控制台会无限输出而不是跳出 while 循环?我是新手,不懂。 D:

输出:

buffer: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Hello World!

(乘以无穷大)

我的主要class

#include "common_include.h"
#include "Socket.h"
#include "Address.h"

#include <string>

bool Initialize_Sockets();
void Shutdown_Sockets();

int main()
{
    Initialize_Sockets();
    constexpr int port = 30000;
    Sage::Socket socket(30000);
    if (socket.Is_Initialized() == false)
    {
        printf("Socket failed to initialize\n");
        return -1;
    }
    const char data[] = "Hello World!";
    socket.Send(Sage::Address(127, 0, 0, 1, port), data, sizeof(data));

    while (true)
    {
        Sage::Address sender;
        unsigned char buffer[256];
        int bytes_read = socket.Receive(sender, buffer, sizeof(buffer));
        if (!bytes_read)
            break;

        printf("buffer: %s\n", buffer);
    }
    Shutdown_Sockets();
}

bool Initialize_Sockets()
{
#if PLATFORM == PLATFORM_WINDOWS
    WSADATA Wsa_Data;
    return WSAStartup(MAKEWORD(2, 2), &Wsa_Data) == NO_ERROR;
#else
    return true;
#endif
}

void Shutdown_Sockets()
{
#if PLATFORM == PLATFORM_WINDOWS
    WSACleanup();
#endif
}

我的插座class

#include "Socket.h"

Sage::Socket::Socket(unsigned short port)
{
    handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#if PLATFORM == PLATFORM_WINDOWS
    if (handle == INVALID_SOCKET)
    {
        printf("Failed to create socket: %d\n", WSAGetLastError());
    }
    else
    {
        is_created = true;
    }
#elif
    if (handle <= 0)
    {
        printf("Failed to create socket\n");
        is_created = false;
    }
    else
    {
        is_created = true;
    }
#endif

    sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(port);
#if PLATFORM == PLATFORM_WINDOWS
    if (bind(handle, (sockaddr*)&address, sizeof(sockaddr_in)) == SOCKET_ERROR)
    {
        printf("Failed to bind socket: %d\n", WSAGetLastError());
        is_bound = false;
    }
    else
    {
        is_bound = true;
    }
#elif
    if (bind(handle, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0)
    {
        printf("failed to bind socket\n");
        is_bound = false;
    }
    else
    {
        is_bound = true;
    }
#endif

#if PLATFORM == PLATFORM_WINDOWS
    DWORD non_blocking = 1;
    if (ioctlsocket(handle, FIONBIO, &non_blocking) == SOCKET_ERROR)
    {
        printf("Failed to set socket non-blocking: %d\n", WSAGetLastError());
        is_non_blocking = false;
    }
    else
    {
        is_non_blocking = true;
    }
#elif
    int non_blocking = 1;
    if (fcntl(handle, F_SETFL, O_NONBLOCK, non_blocking) == -1)
    {
        printf("Failed to set socket non-blocking\n");
        is_non_blocking = false;
    }
    else
    {
        is_non_blocking = true;
    }
#endif
    is_initialized = is_created && is_bound && is_non_blocking;
    if (!is_initialized)
    {
        printf("Failed to initialize socket\n");
    }
}

int Sage::Socket::Send(Sage::Address address, const void* packet_data, int packet_size)
{
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(address.Get_Address());
    addr.sin_port = htons(address.Get_Port());

    unsigned int sendto_address = address.Get_Address();

    int sent_bytes = sendto(handle, (const char*)packet_data, packet_size, 0, (sockaddr*)&sendto_address, sizeof(sockaddr_in));
#if PLATFORM == PLATFORM_WINDOWS
    if (sent_bytes == SOCKET_ERROR)
    {
        printf("Failed to send packet: %d\n", WSAGetLastError());
        return sent_bytes;
    }
    else if (sent_bytes != packet_size)
    {
        printf("Failed to send all bytes of packet(%d/%d)\n", sent_bytes, packet_size);
        return sent_bytes;
    }
#elif PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
    if (sent_bytes != packet_size)
    {
        printf("Failed to send all bytes of a packet(%d/%d)\n", sent_bytes, packet_size);
        return sent_bytes;
    }
#endif
    return sent_bytes;
}

int Sage::Socket::Receive(Sage::Address& sender, void* data, int size)
{
#if PLATFORM == PLATFORM_WINDOWS
    typedef int socklen_t;
#endif
    sockaddr_in from;
    socklen_t from_length = sizeof(from);

    int bytes_received = recvfrom(handle, (char*)data, size, 0, (sockaddr*)&from, &from_length);

    sender = Sage::Address(from);
    return bytes_received;
}

Sage::Socket::~Socket()
{
#if PLATFORM == PLATFORM_WINDOWS
    closesocket(handle);
#elif PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
    close(handle);
#endif
}

0xCCCCCCCC被微软的C++调试运行时库用来标记未初始化的堆栈内存。

204 是 0xCC。

很明显,您的缓冲区中缺少一个 0,它表示数据结束:您的输出运行到未初始化的内存中。

当然,不要依赖任何这种行为 - 您的程序行为是未定义的,但这些事情确实有助于识别问题。

参考:https://en.wikipedia.org/wiki/Magic_number_(programming)#Debug_values