Android网络连接。可以写入数据但无法读取数据。使用QT服务器

时间:2016-09-06 23:57:47

标签: android c++ qt

我正在关注我在网上找到的修改示例,以便与我在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());

        }

我的问题是,即使我可以将数据从PC发送到我的手机,但它无法正常工作。

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

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

任何人都可以告诉我我做错了吗?

1 个答案:

答案 0 :(得分:0)

由于Nagle's algorithm AKA TCP_NODELAY,您的有效负载可能太小而无法立即发送。

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

socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);