Android网络连接.可以写数据,但不能读数据.使用QT服务器

Android network connection. Can write data but cannot read it. Using QT Server

本文关键字:数据 不能读 使用 QT 服务器 Android 连接 网络      更新时间:2023-10-16

我正在遵循一个修改的例子,我在网上找到的,以便与我在我的PC(使用Qt)中编写的程序进行通信。当接收到数据时,Qt函数真的很简单

void FileTransferServer::on_dataReady(){
    QByteArray buffer = socket->readAll();
    ui->teLog->append("Received: " + QString(buffer));
    QByteArray data;
    QString msg = "Soy Qt!!";
    data = msg.toLatin1();
    qint32 value =  socket->write(data);
    ui->teLog->append("Sending reply of " + QString::number(value) + " bytes");
}

在android中,我已经为TCP客户端实现了AsyncTask。客户端主要功能的代码如下:

public void run(){
        try {
            //here you must put your computer's IP address.
            InetAddress serverAddr = InetAddress.getByName(SERVER_ADDRESS);
            System.err.println("Attempting connection...");
            //create a socket to make the connection with the server
            Socket socket = new Socket(serverAddr, SERVER_PORT);
            try {
                //send the message to the server
                out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                System.err.println("Connection successful attempting to send message");
                //To receive messages
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                int i = 0;
                while (i < 10) {
                    i++;
                    out.println("This is Android!!");
                    // Waiting for response.
                    boolean msgRead = false;
                    String msg;
                    while (!msgRead) {
                        System.err.println("Attempting to read a message");
                        msg = in.readLine();
                        System.err.println("Message received: " + msg);
                        if (msg != null) {
                            //output.append("Reply: " + msg);
                            System.err.println("Reply: " + msg);
                            msgRead = true;
                        }
                    }
                }
                System.err.println("All done!");

            } catch (Exception e) {
                System.err.println("Error on sending message: " + e.getMessage());
            } finally {
                //the socket must be closed. It is not possible to reconnect to this socket
                // after it is closed, which means a new socket instance has to be created.
                socket.close();
            }
        } catch (Exception e) {
            System.err.println("Error on connection: " + e.getMessage() + " And also " + e.toString());
        }

我的问题是,即使我可以从电脑发送数据到我的手机,它不能反过来工作。

这是我的控制台输出(用于我的手机)

09-06 20:54:32.619 31231-31245/? W/System.err: Attempting connection...
09-06 20:54:32.629 31231-31245/? W/System.err: Connection successful attempting to send message
09-06 20:54:32.629 31231-31245/? W/System.err: Attempting to read a message

而我的简单Qt App的输出是:

Accepted connection from ::ffff:192.168.1.107. Local address is ::ffff:192.168.1.110
Received: This is Android!!
Sending reply of 8 bytes

但是,程序在尝试读取消息时挂起,如上所示。

当我退出Qt程序时,我得到了大量的这些

09-06 20:56:39.569 31231-31245/cmp.cmpreceiver W/System.err: Attempting to read a message
09-06 20:56:39.569 31231-31245/cmp.cmpreceiver W/System.err: Message received: null
谁能告诉我我做错了什么?

由于Nagle的算法(即TCP_NODELAY),您的有效负载可能太小而无法立即发送。

你可能需要像这样禁用它:

socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
相关文章: