嗯,我还在尝试让一台IPC通过QLocalSocket运行。显然,我的套接字连接,连接被接受,但当我尝试发送某些内容时
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;
}我得不到response...the服务器的套接字什么也不做。
服务器的读取实现:
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();...but显然这也不起作用。在客户端的write函数之后立即触发waitForReadyRead,我认为这意味着它现在可以读取...
编辑用于调试的auto conn = connection(...),检查自己是否连接properly....they做
发布于 2016-01-24 06:14:51
注意:你应该检查一下waitForConnected()的返回值。如果客户端不能设法连接到服务器,那么继续下去就没有意义了。
但我认为你的实际问题是,你正在编写一个简单的8位字符串,比如"hallo \0“,但是你正在用QDataStream读取,它需要一个二进制格式(对于QString,这意味着unicode,长度优先),这不匹配。
https://stackoverflow.com/questions/18384394
复制相似问题