EDIT: The first one answers the question in the title. It was initialized memory from my debugger. I didn’t get the program to work however. Just going to go back to boost::asio for my sockets. Sockets are painful.
I’m following this tutorial on how to start sending and receiving packets using sockets. I’ve followed the tutorial to the best of my ability, but I get a bunch of ╠’s in front of my message. Can someone explain why I’m getting these? Their ASCII code is 204 so not null nor is it 0xFF. If you need anything more, please let me know.
Bonus points: Why is my console outputting this infinitely instead of breaking out of the while loop? I’m new and don’t understand. D:
Output:
buffer: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Hello World!
(times infinity)
My main 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 initializen"); 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: %sn", 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 }
My socket 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: %dn", WSAGetLastError()); } else { is_created = true; } #elif if (handle <= 0) { printf("Failed to create socketn"); 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: %dn", WSAGetLastError()); is_bound = false; } else { is_bound = true; } #elif if (bind(handle, (const sockaddr*)&address, sizeof(sockaddr_in)) < 0) { printf("failed to bind socketn"); 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: %dn", 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-blockingn"); 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 socketn"); } } 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: %dn", 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 }
Answer
0xCCCCCCCC
is used by by Microsoft’s C++ debugging runtime library to mark uninitialised stack memory.
204 is 0xCC.
Clearly you’re missing a 0
in your buffer which would signal the end of the data: your output runs into the uninitialised memory.
Of course, don’t rely on any of this behavior – your program behavior is undefined, but these sort of things do help to identify the problem.
Reference: https://en.wikipedia.org/wiki/Magic_number_(programming)#Debug_values