Results 1 to 4 of 4

Thread: How do connect to the QT on Server and Android Client Program?

  1. #1
    Join Date
    Jul 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post How do connect to the QT on Server and Android Client Program?

    I tried to connect QT Server with Android client.
    Sending data from Android to QT was good,
    but Android program can't receive data from QT on server.

    ================================================== ====================
    QT code on server
    ================================================== ====================

    void TcpServer::readyRead()
    {
    QTcpSocket *client = (QTcpSocket*)sender();
    while(client->canReadLine())
    {
    QString line = QString::fromUtf8(client->readLine()).trimmed();
    qDebug() << "Read line:" << line;

    client->write("Here from server-Write ");

    //
    //QTextStream out(client);
    //out << "Here from server-TextStream ";
    //out.flush();
    }
    }

    ================================================== ====================
    Android client program code
    ================================================== ====================
    public class TCPClient implements Runnable {
    public static final String SERVERIP = "xx.xx..";
    public static final int SERVERPORT = 8080;
    private BufferedReader networkReader;

    public void run() {
    try {
    InetAddress serverAddr = InetAddress.getByName(SERVERIP);
    Log.d("TCP", "C: Connecting...");
    Socket socket = new Socket(serverAddr, SERVERPORT);
    String message = "Hello... from Client";
    String line;

    try {
    Log.d("TCP", "C: Sending: '" + message + "'");
    PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true );
    out.println(message);
    Log.d("TCP", "C: Sent.");
    Log.d("TCP", "C: Done.");

    networkReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    Thread.sleep(1000);
    line = networkReader.readLine(); <== ( on debugging, line is on Holding or Null )
    Log.d("TCP", "C: Receieved..." + line);

    } catch(Exception e) {
    Log.e("TCP", "S: Error", e);
    } finally {
    socket.close();
    }
    } catch (Exception e) {
    Log.e("TCP", "C: Error", e);
    }
    }
    }

    I don't know hardly why android program can't receive a stream data from QT on server..

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: How do connect to the QT on Server and Android Client Program?

    Just one idea: Your server socket is a local variable. It is supposed to buffer any writes, but perhaps it drops that when it it gets out of scope -- and the client doesn't get a chance to read anything. I'd make it a static variable or make a test into the loop that the write buffer is empty before exiting the function.

  3. #3
    Join Date
    Jul 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: How do connect to the QT on Server and Android Client Program?

    Thanks to your reply.

    the client program receives data stream from Qt Server..after i killed the Qt Process on Server.
    the Lock on client is unfasten when the Qt server process is disconnected from client

    now.. it works normally ( some code on Sever was added )


    void TcpServer::readyRead()
    {
    QString line;

    //QTcpSocket *client = (QTcpSocket*)sender(); <== this was moved to a member Variable
    while(true)
    {
    if (client->canReadLine()) {
    line = QString::fromUtf8(client->readLine()).trimmed();
    qDebug() << "Read line:" << line;
    break;
    }
    }
    qDebug() << "Send line:" << line << " from Server";

    QTextStream out(client);
    out << "Here from server-TextStream ";
    client->flush();

    //client->disconnectFromHost();
    client->setSocketDescriptor(m_iSocketDesc); <= the client program works normally after this line was added.. i don't know why it works

    qDebug() << "server-TextStream";
    }

  4. #4

    Default Re: How do connect to the QT on Server and Android Client Program?

    Hello,

    I am trying to perform the same functionality.
    However, I am not able to connect to the server due to some bug in my code.
    Could you please share your QT and Android codes just to get me started?

    Thanks in advance..

Similar Threads

  1. Replies: 4
    Last Post: 17th October 2011, 14:58
  2. Connect push button in server client mode
    By onlybilal in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2011, 18:29
  3. How to automaticaly connect client to server
    By sksingh73 in forum Newbie
    Replies: 1
    Last Post: 4th July 2010, 21:08
  4. Client Server
    By electronicboy in forum General Programming
    Replies: 3
    Last Post: 29th October 2009, 11:10
  5. program cannot connect to X server
    By Wazman in forum Qt Programming
    Replies: 3
    Last Post: 1st September 2009, 20:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.