gpt4 book ai didi

c++ - recv() 字符数组大小

转载 作者:行者123 更新时间:2023-11-27 22:46:29 25 4
gpt4 key购买 nike

我正致力于实现一个 C++ 客户端服务器聊天程序,以了解更多/练习套接字编程。我正在使用 winsockV2。

简而言之,客户端程序连接到服务器,服务器将客户端套接字存储在 vector 中客户端程序发送消息供服务器分发给 vector 中的其他客户端。

我认为我遇到的问题是客户端和服务器正在接收消息并将其存储在 char message[256] 中。如果消息短于 256,当我 std::cout << message; 时会显示奇怪的字符有人告诉我这是未初始化的内存。这是输出示例:

k:message from client to other client╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠(■o

是否有某种方法可以创建接收消息大小的字符数组?即

char recvMessage[4096];
int s = recv(socket, recvMessage, sizeof(recvMessage),0);
char recvOutput[strlen(recvMessage)] = recvMessage;
std::cout << recvOutput << std::endl;

否则,您对 recv 的解决方案是什么? '正在发送您不知道其长度的消息?

如果我是个彻头彻尾的白痴,请善待我,我来自 PHP。类如下:

SVR.CPP

参见 receiveMessages()distributeMessages()功能

#include "stdafx.h"
#include "svr.h"

svr::svr()
{
//WSA Business I don't understand
WORD wVersionRequested;
WSADATA wsaData;
int err;

/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
}
//End of WSA Business

//get addressSize
addressSize = sizeof(address);
//set address data members
address.sin_family = AF_INET;
address.sin_port = htons(444);
address.sin_addr.s_addr = INADDR_ANY;

//init sListen
sListen = socket(AF_INET, SOCK_STREAM, 0);
bind(sListen, (sockaddr*)&address, addressSize);
}

svr::~svr()
{
}

void svr::start()
{
std::thread newConnThread(&svr::newConnection, this);
newConnThread.join();
}

void svr::receiveMessages(int clientIndex)
{
std::cout << "\tsvr::recv thread started for client index:" << clientIndex << std::endl;

//create char arr
char recvMessage[256];

//forever
while (true)
{
//receive message and input it to recvMessage char arr.
recv(clients[clientIndex], recvMessage, sizeof(recvMessage), 0);

//if message is not null, send out to other clients
if (recvMessage != NULL)
{
std::cout << "\t\tINFO:Received message of length: " << std::strlen(recvMessage) << " size: " << sizeof(recvMessage) << " : " << recvMessage << std::endl;
distributeMessages(recvMessage, clientIndex);
}
}
}

//distributes messages to all clients in vector. called by receiveMessages function, normally in rMessages thread.
void svr::distributeMessages(std::string message, int clientIndex)
{
for (unsigned int i = 0; i < clients.size(); i++)
{
if (clientIndex != i)
{
send(clients[i], message.c_str(), message.length(), 0);
}
else
{
//would have sent to self, not useful.
}
}
}

//accepts new connections and adds sockets to vector.
void svr::newConnection()
{
//mark for accept, unsure of somaxconn value;
listen(sListen, SOMAXCONN);

std::cout << "\tSERVER: awaiting new connections..." << std::endl;
while (true)
{
//accept connection and push on to vector.
clients.push_back(accept(sListen, (sockaddr*)&address, &addressSize));

//responds to new clients.
const char *message = "Hi, you've successfully connected!";

int clientIndex = clients.size() - 1;
int sent = send(clients[clientIndex], message, 33, 0);

//start new receiveMessage thread
std::thread newClient(&svr::receiveMessages, this, clientIndex);

//detach here, let newConn thread operate without depending on receiveMessages
newClient.detach();
}
std::cout << "\tSERVER: no longer listening for new connections" << std::endl;
}

CLI.CPP

参见 cSend()cRecv()功能

#include "stdafx.h"
#include "cli.h"

cli::cli(char *ip)
{
//WSA
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

// Use the MAKEWORD(lowbyte,highbyte) macro declared in windef.h
wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);

if (err != 0)
{
std::cout << "WSAStartup failed with the error: " << err;
}
}

//get addressSize
addressSize = sizeof(address);

//set address struct data members
address.sin_family = AF_INET;
address.sin_port = htons(444);

//if ip empty, prompt user;
if (ip == NULL)
{
std::string ipInput;
std::cout << "\n\tConnect to which IP: ";
std::cin >> ipInput;
address.sin_addr.s_addr = inet_addr(ipInput.c_str());
}
else
{
address.sin_addr.s_addr = inet_addr(ip);
}

sock = socket(AF_INET, SOCK_STREAM, 0);

std::cout << "\n\tYour username: ";
std::cin >> uname;
}

cli::~cli()
{
}

void cli::start()
{
try
{
//hold string
char message[33];

std::cout << "\n\tcli::start() called";
int conRet;

//connects to server socket & receives a message, stores in it message variable
conRet = connect(sock, (sockaddr*)&address, (int)addressSize);
recv(sock, message, sizeof(message), 0);

std::cout << "\n\tSERVER: " << message;

//starts threads, pass this for object scope.
std::thread sendThread(&cli::cSend, this);
std::thread recvThread(&cli::cRecv, this);

//this function (start) will return/end when send and recv threads end.
sendThread.join();
recvThread.join();
}
catch (std::exception e)
{
std::cerr << e.what() << std::endl;
}
}

void cli::cSend()
{
std::cout << "\n\tcli::send thread started";
//char arr for sending str;
std::string getLine;
while (true)
{
std::cout << "\n\t" << uname << ":" << std::flush;
//set to "" because i suspected the value remains in the string after a loop.
std::string message = "";

//get input, put it in message
std::getline(std::cin, message);

//get full message
std::string fullMessage = uname + ":" + message;

//get constant int, size of fullMessage
const int charArrSize = fullMessage.length();

std::cout << "\t\tINFO: Sending character array of length: " << charArrSize << " size: " << sizeof(fullMessage.c_str()) << " : " << fullMessage.c_str() << std::endl;

//sends it
send(sock, fullMessage.c_str(), charArrSize, 0);
}
}

void cli::cRecv()
{
std::cout << "\n\tcli::recv thread started";

//initialize arr to 0, will hopefully help avoid the weird chars in the cout
char recvMessage[256]{ '\0' };

while (true)
{
recv(sock, recvMessage, sizeof(recvMessage), 0);

std::cout << "\t\tINFO:Received message of length: " << std::strlen(recvMessage) << " size: " << sizeof(recvMessage) << " : " << recvMessage << std::endl;

std::cout << recvMessage << std::endl;
}
}

最佳答案

what is your solution for recv'ing messages which you do not know the length of?

recv() 告诉您它收到的消息的长度。你不必想知道它是什么。这是 recv() 的返回值。

 int s = recv(socket, recvMessage, sizeof(recvMessage),0);

看——好了。它就在你面前。它是 s。当然,如果有错误,s 将是负数,您需要检查它。但是,忽略这个小细节,您的担心就结束了:s 是您刚收到的消息的长度。

 char recvOutput[strlen(recvMessage)] = recvMessage;

那是行不通的。 strlen() 在这里做什么? strlen() 计算字符串的大小,期望该字符串是老式的 C 风格字符串,以 \0 字节结尾。 recv() 不会终止它接收到的带有 \0 字节的任何内容。相反,它返回实际的字符数。

此外,这无论如何都行不通。您不能以这种方式初始化数组。

显然,您在这里的明显意图是期望接收文本字符串作为消息。好吧,由于您选择的语言是 C++,并且您这样标记了您的问题,因此合乎逻辑的结论是您应该使用 C++ 为您处理文本字符串的功能: std::string 类:

std::string recvOutput{recvMessage, recvMessage+s};

给你。任务完成。由于您已经知道 s 中接收到的消息的长度,正如我们之前确定的那样(并且在仔细检查 s 不是负数之后),您可以简单地使用std::string 的现有构造函数,它在给定迭代器或指向字符串开头和结尾的指针的情况下初始化新字符串。

在处理低级操作系统接口(interface)时,比如套接字,你别无选择,只能使用原始数据类型,比如普通的 char 数组和缓冲区,因为这是操作系统唯一需要做的事情明白。但是,由于 C++ 库提供了丰富的模板和类集,您的代码应该在第一时间切换到使用 C++ 类和模板,以便能够使用所有这些资源。因此,一旦您确定了刚刚提出的文本字符串 recv() 有多大,只需将其填充到 std::string 中,然后再计算如何处理它。

关于c++ - recv() 字符数组大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42310469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com