QTcpSocket 连接
我正在尝试使用 QTcpSocket 和 QTcpServer 编写聊天。 我的几段代码
客户端
ChatClient::ChatClient(QObject *parent)
: QObject(parent) {
connect(&tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(error(QAbstractSocket::SocketError)));
connect(&tcpSocket, SIGNAL(connected()),
this, SLOT(requestForID()));
connect(&tcpSocket, SIGNAL(readyRead()),
this, SLOT(receiveMessage()));
tcpSocket.connectToHost(QHostAddress::LocalHost, PORT);
}
void ChatClient::requestForID() {
qDebug() << "Connected, requesting for ID";
QByteArray segment;
QDataStream out(segment);
out.setVersion(QDataStream::Qt_4_7);
out << (quint16)0 << ID;
out.device()->seek(0);
out << (quint16)(segment.size() - sizeof(quint16));
tcpSocket.write(segment);
}
void ChatClient::error(QAbstractSocket::SocketError error) {
qDebug() << "Socket error" << error;
}
服务器
ChatServer::ChatServer(QObject *parent)
: QObject(parent) {
if (!tcpServer.listen(QHostAddress::LocalHost, PORT)) {
qDebug() << "Unable to start the server"
<< tcpServer.errorString();
}
connect(&tcpServer, SIGNAL(newConnection()),
this, SLOT(processConnection()));
}
客户端套接字永远无法连接。错误永远不会被打印。 端口=6178。 运行 KUbuntu。从 bash Ping 到 localhost 成功。 我做错了什么?
I'm trying to write a chat using QTcpSocket and QTcpServer.
Several pieces of my code
Client
ChatClient::ChatClient(QObject *parent)
: QObject(parent) {
connect(&tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(error(QAbstractSocket::SocketError)));
connect(&tcpSocket, SIGNAL(connected()),
this, SLOT(requestForID()));
connect(&tcpSocket, SIGNAL(readyRead()),
this, SLOT(receiveMessage()));
tcpSocket.connectToHost(QHostAddress::LocalHost, PORT);
}
void ChatClient::requestForID() {
qDebug() << "Connected, requesting for ID";
QByteArray segment;
QDataStream out(segment);
out.setVersion(QDataStream::Qt_4_7);
out << (quint16)0 << ID;
out.device()->seek(0);
out << (quint16)(segment.size() - sizeof(quint16));
tcpSocket.write(segment);
}
void ChatClient::error(QAbstractSocket::SocketError error) {
qDebug() << "Socket error" << error;
}
Server
ChatServer::ChatServer(QObject *parent)
: QObject(parent) {
if (!tcpServer.listen(QHostAddress::LocalHost, PORT)) {
qDebug() << "Unable to start the server"
<< tcpServer.errorString();
}
connect(&tcpServer, SIGNAL(newConnection()),
this, SLOT(processConnection()));
}
Client socket never gets connected. Error is never being printed.
PORT = 6178.
Runnig KUbuntu. Ping to localhost from bash is successful.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在你的代码中没有看到任何错误,你确定你的 Qt 和“网络”工作正常吗? Qt 应该会发出错误,但至少你这里的代码片段对我来说看起来是正确的。也许您的代码永远不会被调用,请向方法添加一些调试消息。
最后,您可以构建 Qt 网络示例并测试它是否在您的计算机上运行。如果您没有示例,请查看此处:http://doc。 qt.io/qt-5/examples-network.html(Fortune TCP 服务器/客户端)
I don't see any errors in your code, are you sure your Qt and "network" is working properly? Qt should emit an error but at least your code pieces here look correct to me. Maybe your code never gets called, add some debug messages to the methods.
At last you can build the Qt Network examples and test if that is working on your machine. If you don't have the examples take a look here: http://doc.qt.io/qt-5/examples-network.html (Fortune Server/Client for TCP)