【发布时间】:2021-06-06 03:53:13
【问题描述】:
编辑:第一个回答标题中的问题。它是我的调试器初始化的内存。但是,我没有让程序正常工作。只是回到我的套接字的 boost::asio 。插座很痛。
我正在关注this tutorial,了解如何开始使用套接字发送和接收数据包。我已尽我所能按照教程进行操作,但我的信息前面有一堆╠。有人可以解释为什么我会得到这些吗?它们的 ASCII 码是 204,所以不是空的,也不是 0xFF。如果您还需要什么,请告诉我。
加分:为什么我的控制台会无限输出而不是退出 while 循环?我是新手,不懂。 D:
输出:
buffer: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Hello World!
(乘以无穷)
我的主要课程
#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
}
我的套接字类
#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
}
【问题讨论】:
-
您可能缺少空终止符。
-
在哪里?我在尝试发送的 Hello World 字符串上添加了一个,它不会改变输出。
-
我不知道在哪里。这(通常)是当您错过零终止符时会发生的情况。使用调试器找出答案。
-
试试这个:
unsigned char buffer[256] = {0}; -
请注意,教程中有条件
if ( bytes <= 0 ),您将其更改为if (!bytes_read)。recvfrom在失败时返回一个负数,因此在这种情况下您将打印buffer。
标签: c++ sockets networking winsock2