Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can't Receive Data from Desktop (Qt C++) to android (java)



  • I have created a Qt project and android project. here Desktop is working as a server and android is the receiver.

    Current situation :-

    1. Android is connecting to a server perfectly
    2. Android is sending data and a server receiving data
    3. Server sending data and but android can not receive data <=== My problem)

    Android code
    ->ReadyRead block for reading data

    public class mySocket extends AsyncTask<String,String,String>
    {
    
        Socket s;
        DataOutputStream ds;
        String ip,message;
        int port = 1812;
        TextView t1;
    
        private PrintWriter output;
        private BufferedReader input;
    
        public mySocket(TextView t1) {
            this.t1 = t1;
        }
    
        @Override
        protected String doInBackground(String... strings)
        {
            ip = strings[0];
            message = strings[1];
    
    
            try {
                s = new Socket(ip,port);
    
                output = new PrintWriter(s.getOutputStream());
                input = new BufferedReader(new InputStreamReader(s.getInputStream()));
    
                if(s.isConnected())
                {
                    t1.setText("Connected");
                    output.write("1");
                    output.flush();
                }
                else
                {
                    t1.setText("Not connected");
                }
                new Thread(new ReadyRead()).start();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }
        class ReadyRead implements Runnable
        {
    
            @Override
            public void run() {
                int i = 0;
                Log.e("ReadyRead", "run: Thread Started");
                while (true)
                {
                    i++;
                   // variable i only prits once (I dont know why)
                    Log.e("ReadyRead ", "I = " + i );
    
                    // loop is getting terminated after this line 
                   // and also  the log message of try  block not  execute
                    try
                    {
                        final String message = input.readLine();
                        Log.e("Message From Server", "run: " + message );
                        //t1.setText(message);
                    }
                    // here the catch block's log message is not print
                    catch (IOException e) {
                        Log.e("Error", "run: " + e.getMessage());;
                    }
                }
            }
        }
    }
    
    

    Desktop code:-
    ->Android sending "1" and this function is processing data and sending "1~15".

    
    void MyTcpSocket::myReadyRead()
    {
        // get the information
        QString data = socket->readAll();
        qDebug() << socketDescriptor << " Data in: " << data;
    
        QStringList list = data.split('~');
    
        bool isFirst = true;
        QStringList dataList ;
        int action;
    
        for(int i = 0 ;i < list.length() ; ++i)
        {
            if(isFirst)
            {
                action = list.at(i).toInt() ;
                isFirst = false;
            }
            else
            {
                dataList.push_back(list.at(i));
            }
        }
    
        qDebug() << "serverConnection (myReadReady) : action : " << action;
        qDebug() << "serverConnection (myReadReady) : value : " << dataList;
    
        switch (action)
        {
            case ALLAction::error :
            {
                qDebug() << "serverConnection (myReadReady) : list : " << list;
                break;
            }
            case ALLAction::getTotaltableNo :
            {
                //sending total table Quantity..
                GlobalData g;
                QString tblNo =  XmlManipulation::getData(g.getTagName(g.QtyTable),g.getattribute(g.QtyTable) );
    
                QByteArray msg = setAction(ALLAction::getTotaltableNo,tblNo);
    
                qDebug() << "serverConnection (myReadReady) : ALLAction::getTotaltableNo : sending msg : " << msg ;
                //socket->write(msg);
                QTextStream out(socket);
                out << msg;
    
                break;
            }
            default:
            {
                qDebug() << "serverConnection (myReadReady) : default case called : " << data;
            }
        }
    }
    

Log in to reply