[英]QLocalSocket doesn't send anything
well, i'm still trying to get a IPC running through a QLocalSocket. 好吧,我仍在尝试通过QLocalSocket运行IPC。 Obviously my socket connects, the connection gets accepted, but when i try to send something
显然我的套接字已连接,该连接已被接受,但是当我尝试发送某些内容时
void ServiceConnector::send_MessageToServer(QString message) {
if(!connected){
m_socket->connectToServer(m_serverName);
m_socket->waitForConnected();
}
char str[] = {"hallo welt\0"};
int c = m_socket->write(str,strlen(str));
qDebug() << "written: " << c;
}
i get no response...the server's socket does just nothing. 我没有回应...服务器的套接字什么都不做。
server's read implementation: 服务器的读取实现:
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);
}
outputs: 输出:
client: 客户:
[Debug]: ConnectingState
[Debug]: ConnectedState
[Debug]: socket_connected
[Debug]: written: 10
server: 服务器:
[Debug]: incoming connection
[Debug]: socket is ready to be read
[Debug]: open: true
[Debug]: readable: true
the socket's readReady() signal is never emitted, so i decided to use 套接字的readReady()信号从不发出,所以我决定使用
clientConnection->waitForReadyRead();
socket_readReady();
...but obviously that doesn't work either. ...但是显然那也不行。
waitForReadyRead
is fired immediately after the write
function of the client, which i think means that it is now ready to read... 客户端的
write
功能后立即触发waitForReadyRead
,我认为这意味着它现在可以读取...
[edit] the auto conn = connection(...)
was for debugging, to check if they connect properly....they do [编辑]
auto conn = connection(...)
用于调试,以检查它们是否正确连接。...
Note: you should really check the return value of waitForConnected(). 注意:您应该真正检查waitForConnected()的返回值。 There's no point in proceeding further if the client doesn't manage to connect to the server.
如果客户端无法管理连接到服务器,则没有任何进一步的意义。
But I think your actual issue is that you're writing a simple 8-bit string like "hallo welt\\0" but you're reading with QDataStream which expects a binary format (for QString, that means unicode, and length first), this doesn't match. 但我认为您的实际问题是,您正在编写一个简单的8位字符串,例如“ hallo welt \\ 0”,但您正在使用QDataStream进行读取,该数据需要二进制格式(对于QString,这意味着unicode,并且长度优先),这不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.