QLocalSocket 不发送任何内容
QLocalSocket doesn't send anything
本文关键字:任何内 QLocalSocket 更新时间:2023-10-16
嗯,我仍在尝试通过QLocalSocket运行IPC。很明显,我的套接字连接了,连接被接受了,但当我试图发送时
void ServiceConnector::send_MessageToServer(QString message) {
if(!connected){
m_socket->connectToServer(m_serverName);
m_socket->waitForConnected();
}
char str[] = {"hallo welt "};
int c = m_socket->write(str,strlen(str));
qDebug() << "written: " << c;
}
我没有得到回应。。。服务器的套接字什么也不做。
服务器的读取实现:
void ClientHandler::socket_new_connection() {
logToFile("incoming connection");
clientConnection = m_server->nextPendingConnection();
socket_StateChanged(clientConnection->state());
auto conn = connect(clientConnection, SIGNAL(disconnected()),this, SLOT(socket_disconnected()));
conn = connect(clientConnection, SIGNAL(stateChanged(QLocalSocket::LocalSocketState)),this,SLOT(socket_StateChanged(QLocalSocket::LocalSocketState)));
conn = connect(clientConnection, SIGNAL(readyRead()), this, SLOT(socket_readReady()));
clientConnection->waitForReadyRead();
socket_readReady();
}
void ClientHandler::socket_readReady(){
logToFile("socket is ready to be read");
logToFile((clientConnection->isOpen())? "open: true":"open: false");
logToFile((clientConnection->isReadable())? "readable: true":"readable: false");
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_0);
if (clientConnection->bytesAvailable() < (int)sizeof(quint16)) {
return;
}
QString message;
in >> message;
logToFile("Message recieved" + message);
send(message);
emit messageReceived(message);
}
输出:
客户端:
[Debug]: ConnectingState
[Debug]: ConnectedState
[Debug]: socket_connected
[Debug]: written: 10
服务器:
[Debug]: incoming connection
[Debug]: socket is ready to be read
[Debug]: open: true
[Debug]: readable: true
套接字的readReady()信号从未发出,所以我决定使用
clientConnection->waitForReadyRead();
socket_readReady();
但显然这也不起作用。waitForReadyRead
在客户端的write
函数之后立即启动,我认为这意味着它现在可以读取。。。
[edit]auto conn = connection(...)
用于调试,以检查它们是否正确连接。。。。他们做
注意:您确实应该检查waitForConnected()的返回值。如果客户端无法连接到服务器,那么继续进行下去就没有意义了。
但我认为您的实际问题是,您正在编写一个简单的8位字符串,如"hallo welt\0",但您使用的是QDataStream,它需要二进制格式(对于QString,这意味着unicode,长度优先),这不匹配。
相关文章:
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- std::任何只用于移动的模板,其中副本ctor内的static_assert等于编译错误,但为什么
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- "... already defined in *.obj"但我没有在同一范围内两次定义任何变量
- C++ 中是否有任何内置阶乘函数?
- 如何在C++中接收任何类型的函数参数并获取函数内传递变量的类型?
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 调用向量内的函数指针不执行任何操作
- 是否有任何挂钩接口在编译时间内更换功能
- 如何检查copy_if是否返回使用ostream_iterator范围内的任何内容
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- c ++中是否有任何内置功能可以在计划的时间运行任务
- 是否有任何内置的 UTF 在 c++ 在窗口中
- 在VC++2010中执行任何内置的ARRAY函数
- C++:如何在不使用任何内置函数的情况下将字符串转换为整数
- 是否有任何内置的CUDA函数允许CUDA内核向主机代码报告错误?
- 我如何在常数时间(O(1))内获得字母表(1-26)中字符的数值/位置,而不使用任何内置方法或函数
- C++中的任何内置哈希方法
- c++库中的任何内置夹子方法