gpt4 book ai didi

c++ - QT read Ready()TCP套接字在需要之前不接收消息

转载 作者:行者123 更新时间:2023-11-28 07:14:21 26 4
gpt4 key购买 nike

这是家庭作业,所以只有一些概念性提示会很好。

此刻我正在创建一个客户端服务器项目,客户端将向服务器发送一条消息(这部分工作得很好),但随后服务器将一条消息发送回客户端,这就是我开始遇到问题的地方,我在 QT 中创建了一个单独的类,在我的客户端中称为连接管理,它处理发送接收消息,这个类由我的 Controller 类使用,它处理我所有 UI 页面的应用程序逻辑,现在当我的 Controller 发送消息时向服务器发回消息,atm 我将其作为回显,我的 ConnectionManagement 类使用 readyRead() 信号处理传入消息,这里的问题是当有人使用我的客户端并说按下一个按钮时,下一个按钮插槽将被调用,并等待直到执行 NextButtonPressed() 或 w/e.在阅读就绪通知之前,有东西被发回给它,我尝试阅读我可以手动调用但它似乎没有任何建议?

我的一小部分代码:

ConnectionManager.cpp

ConnectionManager::ConnectionManager(QObject *parent)
: QTcpServer(parent)
{
socket = new QTcpSocket(this);

connect(socket, SIGNAL(readyRead()), this, SLOT(readyread()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnect()));

socket -> connectToHost("192.168.0.119",60000);
}


QString ConnectionManager::getMessage()
{
//sleep(1);
while (inMessage == "")
{
//my attempt at calling read() manually it will read nothing
QByteArray data = socket->readAll();

//just checkign what i am getting my server will immediately return a trivial
//string i.e "carly, santos, andrew, bob"
std::cout << data.data() << "+ 8" << std::endl;
inMessage = data;
}
return inMessage;
}

void ConnectionManager::sendMessage(QString &message)
{
socket -> write(message.toUtf8());
}

//my socket for reading whenever something is sent?
void ConnectionManager::readyread()
{
QByteArray data = socket->readAll();
inMessage = data;
std::cout << data.data() << "+ 1" << std::endl;
}

现在好了,我的 controller.cpp

void Controller::openTALogin()
{
//TODO send message to Server
QString buffer = Fill::fillBufferState("Client", "TA");
//std::cout << buffer.toUtf8().data() << std::endl;
myConnection->sendMessage(buffer); // <- this works great =)

//sleep(7);
std::cout << "1234" << std::endl;

QString in = myConnection -> getMessage(); //<- gets nothing -.-

std::cout << in.toUtf8().data() << "+ 12" << std::endl;
//TODO recieve List from server
//ForNow
QLinkedList<QString> *testList = new QLinkedList<QString>();

Fill::fillLinkedList(testList, in);
//testList -> push_front("jabba:5:hut:1");
//testList -> push_front("blind:3:mice:2");
//testList -> push_front("eat:5:day:3");
//testList -> push_front("hello:4:goodbye:4");


delete userWindow;
taLoginWindow = new TALoginWindow(this, testList);
taLoginWindow -> show();
testList -> clear();
delete(testList);

此函数执行后会在此处收到消息,我记得使用 C-Style TCP/IP 连接的形式读取会等到有数据才能继续,但在这里什么也得不到似乎很酷?

最佳答案

你发送消息然后立即尝试得到响应,这是行不通的,因为你需要在写入或读取任何内容之前返回到事件循环

将您的 Controller::openTALogin 方法拆分为 2

void Controller::openTALogin()
{
//TODO send message to Server
QString buffer = Fill::fillBufferState("Client", "TA");
//std::cout << buffer.toUtf8().data() << std::endl;
myConnection->sendMessage(buffer); // <- this works great =)
}

void Controller::gotResponce(QString in)
{

std::cout << in.toUtf8().data() << "+ 12" << std::endl;
//TODO recieve List from server
//ForNow
QLinkedList<QString> *testList = new QLinkedList<QString>();

Fill::fillLinkedList(testList, in);
//testList -> push_front("jabba:5:hut:1");
//testList -> push_front("blind:3:mice:2");
//testList -> push_front("eat:5:day:3");
//testList -> push_front("hello:4:goodbye:4");


delete userWindow;
taLoginWindow = new TALoginWindow(this, testList);
taLoginWindow -> show();
testList -> clear();
delete(testList);

}

并在您的 readyRead 中调用第二部分:

void ConnectionManager::readyread()
{
QByteArray data = socket->readAll();
inMessage = data;
std::cout << data.data() << "+ 1" << std::endl;
emit recievedMessage(QString(inMessage)); // using signal here
}

关于c++ - QT read Ready()TCP套接字在需要之前不接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20461572/

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