Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Can't Receive Data from Desktop (Qt C++) to android (java)
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
1 Posts 1 Posters 190 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AnmolA Offline
    AnmolA Offline
    Anmol
    wrote on last edited by
    #1

    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;
            }
        }
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved