我正在使用 Qt 框架在 C++ 中开发客户端-服务器应用程序,但客户端可以是 android 手机和计算机(Qt 客户端应用程序)现在我在处理服务器端的数据接收时遇到了麻烦;服务器没有正确接收数据。
首先,我使用以下方法在服务器(Qt 应用程序)和客户端(Qt 应用程序)之间进行了良好的发送和接收: 消息的大小保留在数据包的开头,以帮助检查整个消息是否是收到与否。
这是向客户端发送消息的方法
void Server::send(const QString &message)
{
QByteArray paquet;
QDataStream out(&paquet, QIODevice::WriteOnly);
out << (quint16) 0; // just put 0 at the head of the paquet to reserve place to put the size of the message
out << message; // adding the message
out.device()->seek(0); // coming back to the head of the paquet
out << (quint16) (paquet.size() - sizeof(quint16)); // replace the 0 value by the real size
clientSocket->write(paquet); //sending...
}
每次收到一个 paquet 时都会调用此插槽。
void Server::dataReceived()
{
forever
{
// 1 : a packet has arrived from any client
// getting the socket of that client (recherche du QTcpSocket du client)
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (socket == 0)
return;
QDataStream in(socket);
if (dataSize == 0) // if we don't know the size of data we are suppose to receive...
{
if (socket->bytesAvailable() < (int)sizeof(quint16)) // we haven't yet receive the size of the data completly then return...
return;
in >> dataSize; // now we know the amount of data we should get
}
if (socket->bytesAvailable() < dataSize)
return;
// Here we are sure we got the whole data then we can startreadind
QString message;
in >> message;
//Processing....
dataSize = 0; // re-initialize for the coming data
}
}
这在服务器与 Qt 应用程序客户端对话时运行良好,因为在那里使用了相同的方法,并且 quint16 的大小将保持相同悬停它不适用于 android 客户端,然后我尝试了另一种方式想忽略发送的消息的大小,但是以某种方式格式化消息,以便我可以知道它从哪里开始和在哪里结束,然后通过一些控件我可以得到它但是我被困在这里,导致读取的数据没有'打印时不包含任何内容,但他的大小有一个值(甚至根据客户端发送的文本量而变化)!
void Server::dataReceived() // a packet is received!
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (socket == 0)
return;
QByteArray data= socket->readAll(); //reading all data available
QString message(data)
qDebug() << data; // this prints nothing!
qDebug() << data.size();// But this prints a non null number, wich means we got something, and that number varies according to the amount of text sent!
qDebug() << message; // this also prints notghing!
}
PS:它甚至不适用于 Qt 应用程序客户端。
你能帮我找出问题所在吗,我有点困惑 tcp 协议是如何处理数据的,如果你可以并且还建议我这样做的好方法。
这是我为此目的制作的android类
class QTcpSocket implements Runnable {
private String ip="";
private int port;
private Socket socket;
private PrintWriter printWriter;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
public QTcpSocket(String ip, int port) {
this.ip = ip;
this.port = port;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getIp() {
return this.ip;
}
public void setPort(int port) {
this.port = port;
}
public void run() {
try {
socket = new Socket(this.ip, this.port);
dataOutputStream = new DataOutputStream( socket.getOutputStream() );
dataInputStream = new DataInputStream(socket.getInputStream());
String response = dataInputStream.readUTF();
dataOutputStream.writeUTF("Hello server!");
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String message) {
try {
dataOutputStream.writeUTF(message);
}catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
printWriter.flush();
printWriter.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isClosed() {
return socket.isClosed();
}
}