Results 1 to 5 of 5

Thread: Windows Application and Embbeded system communication using TCP protocol

  1. #1
    Join Date
    Nov 2010
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Windows Application and Embbeded system communication using TCP protocol

    Hello every one i am working on a project where i have to create a Application to control a Embedded System using TCP protocol. The embedded system runs on a ARM 9 processor on which they have flashed a TCP stack, There is a IP address and ID for the processor. Now my problem is what i should use a QTcpSocket or a QTcpServer cause i will be sending some control commands to the embbeded system and it will reply back to me the status of its sub systems.. So there will be both sending and receiving of data. The Network example only describe how to either send data using QTcpServer or receive data using QTcpSocket. Should i use both QTcpSocket and QTcpServer in my program to communicate both ways. .?? Pls suggest me something this is the first time i am working on a networking project.

    Thank you

  2. #2
    Join Date
    Mar 2011
    Hyderabad, India
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    MacOS X Unix/X11 Windows
    Wiki edits

    Default Re: Windows Application and Embbeded system communication using TCP protocol

    Well, First thing this is nothing to do with Qt. It is simple socket programming. Let us see if I can explain you some basic things and get you started.

    1. For a TCP commutation to happen a TCP connection has to made.
    2. For a TCP connection, a client requests for a connection, and a server (which is already listening) has to accept the connection.
    In your case, the embedded system has to act as a server, and keep listening to incoming connection requests. The PC application makes a request to the server (embedded system), and connection is established. Now data can be sent and received, both ways, as long as connection is active. So you can use QTcpSocket at the client end, i.e. PC application, and use QTcpServer & QTcpSocket at the server end, i.e. embedded system end. This way you could connect any number of PC's to the embedded system. (as long as embedded system can accept new connections)

    You could also make the PC as server, and embedded system as client, which will make it possible to connect many embedded systems to one PC. Again it all depends how you want to design you application

    I suggest read some socket/netwok programming basics, else you will be lost figuring out how things work.

  3. The following user says thank you to Santosh Reddy for this useful post:

    nagabathula (16th August 2011)

  4. #3
    Join Date
    Aug 2009
    Thanked 31 Times in 25 Posts
    Qt products

    Default Re: Windows Application and Embbeded system communication using TCP protocol


    As Santosh said, you must first have a server listening on a port.

    You can also have the server running on the PC side. Then your client (the ARM board) can connect to the server at that port. But the actual communication doesn't occur on this port. For every client that connects to the server, a new QTcpSocket must be created that you can use for sending and receiving data to/from the client connection.

    It is better to have the server running on the ARM, but I can't comment on this. If you get a server to work on the ARM, you must only open a QTcpSocket in Qt.

    If you look at the Fortune Cookie example in Qt, you will see that the client connection is closed immediately. So this is not what you want.

    I can give you some code snippets of a project of mine. It shows that for every new incoming client connection, a new object is created that holds information about the newly created connection to the client.

    Note that it is not a fully working example, it is just to show how I did some things to keep the client connection open.


    Qt Code:
    1. /** TCP server that listens for incoming client connections.
    2.   */
    3. class CMyTcpServer : public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit CMyTcpServer(QObject *parent = 0);
    8. QTcpServer *tcpServer;
    9. QString m_sIpAddress;
    10. int m_nIpPort;
    11. QList<CMyTcpClientConnection*> m_lClients;
    12. void removeClient( CMyTcpClientConnection *pClient );
    13. public slots:
    14. void serverNewConnection( void );
    15. };
    17. /** Client connection of a CMyTcpServer.
    18.   */
    19. class CMyTcpClientConnection : public QObject
    20. {
    21. Q_OBJECT
    22. public:
    23. explicit CMyTcpClientConnection( CMyTcpServer *parent, QTcpSocket *clientSocket );
    24. CMyTcpServer *m_pTcpServer;
    25. QTcpSocket *m_pSocket;
    26. public slots:
    27. void doDisconnect( void );
    28. void doReadyRead( void );
    29. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //------ CMyTcpServer() --------------------
    3. CMyTcpServer::CMyTcpServer(QObject *parent) :
    4. QObject(parent)
    5. {
    6. tcpServer = new QTcpServer(this);
    8. if( !tcpServer->listen() ) // unable to start server
    9. do stuff if TCP server cannot be started
    11. connect(tcpServer, SIGNAL(newConnection()), this, SLOT(serverNewConnection()) );
    12. }
    14. //----------- serverNewConnection -------------------
    15. /** Signal handler for newConnection() when a new connection is requested.
    16.   * A new client connection object is created and added to the client list.
    17.   */
    18. void CMyTcpServer::serverNewConnection( void )
    19. {
    20. CMyTcpClientConnection *clientConnection = new CMyTcpClientConnection( this, tcpServer->nextPendingConnection() );
    22. m_lClients.append( clientConnection );
    23. }
    25. //------------------------------------- removeClient --------------------------
    26. /** Removes a specified client from the client list.
    27.   * This is called by the client when its connection is closed.
    28.   */
    29. void CMyTcpServer::removeClient( CMyTcpClientConnection *pClient )
    30. {
    31. int nIndex = m_lClients.indexOf( pClient ); // find position of client in list
    32. if( nIndex >= 0 )
    33. {
    34. qDebug() << "client removed";
    35. m_lClients.removeAt( nIndex ); // remove from list
    36. pClient->deleteLater(); // delete object itself also
    37. }
    38. }
    40. //=======================================
    41. // CMyTcpClientConnection
    42. //========================================
    44. //------------------------------------- CMyTcpClientConnection ------------
    45. /** Constructor.
    46.   * The TCP server is given as parent. It will maintain a list of client
    47.   * connections. The new TCP socket is given as parameter too. This is
    48.   * remembered by this object, and it becomes the parent of it too.
    49.   */
    50. CMyTcpClientConnection::CMyTcpClientConnection( CMyTcpServer *parent, QTcpSocket *clientSocket ) :
    51. QObject(parent)
    52. {
    53. m_pTcpServer = parent;
    54. m_pSocket = clientSocket;
    55. m_pSocket->setParent( this );
    57. connect(clientSocket, SIGNAL(disconnected()), SLOT(doDisconnect()));
    58. connect(clientSocket, SIGNAL(readyRead()), SLOT(doReadyRead()) );
    60. qDebug() << "add TCP client " << m_pSocket->peerAddress().toString() << m_pSocket->peerPort();
    61. }
    63. //------------------------------------- doDisconnect --------------------------
    64. /** Signal handler when peer is closed.
    65.   * Removes this client from the TCP server client connection list.
    66.   * This client connection object will then be deleted.
    67.   */
    68. void CMyTcpClientConnection::doDisconnect( void )
    69. {
    70. m_pTcpServer->removeClient( this );
    71. }
    73. //------------------------------------- doReadyRead --------------------------
    74. /** Signal handler when data is available from peer.
    75.   *
    76.   */
    77. void CMyTcpClientConnection::doReadyRead( void )
    78. {
    79. QByteArray aData = m_pSocket->readAll(); // read all data from socket
    80. ...
    81. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by marcvanriet; 15th August 2011 at 20:56.

  5. The following user says thank you to marcvanriet for this useful post:

    nagabathula (16th August 2011)

  6. #4
    Join Date
    Nov 2010
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: Windows Application and Embbeded system communication using TCP protocol

    Hello marcvanriet and Santosh Reddy thank you so much for the code example and the information you both have given me i just have a vague idea about how i should go about it now i will read on socket/netwok programming basics now first before i can start. i think i will use the PC side as the client and the Embedded system as the Server. And in this project they will connect to only a single client.

    @marcvanriet-- I din't exactly understand how you always keep the client connection open.

    Thank you again Santosh Reddy and marcvanriet for the information you have given me cleared many doubts of mine.


  7. #5
    Join Date
    Oct 2009
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows

    Default Re: Windows Application and Embbeded system communication using TCP protocol


    @marcvanriet-- I din't exactly understand how you always keep the client connection open.
    The TCP protocol allows the bidirectional transmission of streams of bytes. Each endpoint cannot guess in advance when the stream it receives will end. That is why there is a notion of connection: while the connection is open, each endpoint is prepared to receive additional data. It is only by closing the connection that one endpoint notifies the other that the communication is over.

    By contrast, the UDP protocol is used for one-shot communication. A single block of data is transmitted in a single direction. There is no notion of connection here: the receiving end is simply notified that a block of data has arrived; the communication is already over.

    The fortune example keeps things simple by using TCP for one-shot communication only. The server sends a fortune and immediately closes the connection.

    In general you only close the TCP connection when the communication is definitely over. You can leave it open even if there is no data to transmit immediately.

    Using QTCPSocket, call disconnectFromHost() (or delete the object) to close the connection. Use the disconnected() signal to be notified when the connection is closed by the other endpoint.

Similar Threads

  1. gui application for serial port communication
    By jerkymotion in forum Newbie
    Replies: 11
    Last Post: 9th March 2011, 21:43
  2. GUI application for serial communication
    By jerkymotion in forum Newbie
    Replies: 6
    Last Post: 8th March 2011, 20:00
  3. [ASK] QT with TCP protocol in windows??
    By aribriant in forum Newbie
    Replies: 1
    Last Post: 29th September 2010, 13:47
  4. communication between QT application and python
    By HERC in forum Qt Programming
    Replies: 2
    Last Post: 1st February 2010, 10:47
  5. Replies: 4
    Last Post: 13th September 2009, 15:40


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.