當前使用Qt的QTcpsocket庫和python 3的套接字類。我已經獲得了Qt的C++ fortune客戶端/服務器示例來構建和正確運行。但是,它適用於C++的客戶端和服務器。要求是服務器正在運行Python。Python 3套接字和QTcpsocket之間的通信失敗(C++)
# Server.py
import socket
# ...
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("localhost", 45000)
sock.listen(1) # queuing up 1 request for now.
(clientsocket, address) = sock.accept() # waits until client connects.
chunk = clientsocket.recv(1024).decode() # client is now connect
print(chunk) #prints out message from client
msg = "Hello from the server"
msg = str.encode(msg)
# send the message back to the client
clientsocket.sendall(msg)
和
// Client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include <QtNetwork/QtNetwork>
class Client : public QObject {
Q_OBJECT
public:
Client();
QTcpSocket *m_socket;
QHostAddress m_serverAddr = QHostAddress("127.0.0.1");
quint16 m_serverPort = 45000;
private:
QDataStream m_dataStream;
void testConnect();
};
#endif
和
// client.cpp
Client::Client() {
m_socket = new QTcpSocket(this);
m_dataStream.setDevice(m_socket);
m_dataStream.setVersion(QDataStream::Qt_4_0);
testConnect();
}
void Client::testConnect() {
m_socket->abort(); // if m_socket is not already connected, this does nothing
m_socket->connectToHost(m_serverAddr, m_serverPort);
if (m_socket->waitForConnected(30000)) {
qDebug() << "Connected to server...";
m_socket->write("Hello server from client"); // is received!
m_socket->waitForBytesWritten();
m_socket->waitForReadyRead();
qDebug() << "Reading: " << m_socket->bytesAvailable();
m_dataStream.startTransaction();
QString nextFortune;
m_dataStream >> nextFortune;
if (!m_dataStream.commitTransaction()) {
qDebug() << "Read errors have occurred."; // prints when connected to python server. not desired behavior
m_socket->close();
return;
}
// This prints when running the Qt fortune c++ server, but not the python server (above).
qDebug() << "No read errors occurred during read transactions.";
qDebug() << nextFortune;
}
}
什麼最終發生的是服務器從客戶端接收的消息沒有問題,但是當服務器試圖發送回覆與clientsocket.sendall(msg)
,m_dataStream.commitTransaction()
返回false。我最初的直覺是,python方面的編碼是錯誤的。 QDataStream是否需要特殊編碼?
的文檔QDataStream :: commitTransaction():
布爾QDataStream :: commitTransaction()
完成讀取事務。如果在交易過程中沒有發生讀取錯誤 ,則返回
true
;否則返回false
。
此外,運行程序後,這裏是C的輸出++客戶端:
Connected to server...
Reading: 25
Read errors have occurred.
謝謝是的,這使我能夠在客戶端正確接收消息。非常感謝你! – user3701175