Hi,
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.
Regards,
Marc
/** TCP server that listens for incoming client connections.
*/
class CMyTcpServer
: public QObject{
Q_OBJECT
public:
explicit CMyTcpServer
(QObject *parent
= 0);
int m_nIpPort;
QList<CMyTcpClientConnection*> m_lClients;
void removeClient( CMyTcpClientConnection *pClient );
public slots:
void serverNewConnection( void );
};
/** Client connection of a CMyTcpServer.
*/
class CMyTcpClientConnection
: public QObject{
Q_OBJECT
public:
explicit CMyTcpClientConnection
( CMyTcpServer
*parent,
QTcpSocket *clientSocket
);
CMyTcpServer *m_pTcpServer;
public slots:
void doDisconnect( void );
void doReadyRead( void );
};
/** TCP server that listens for incoming client connections.
*/
class CMyTcpServer : public QObject
{
Q_OBJECT
public:
explicit CMyTcpServer(QObject *parent = 0);
QTcpServer *tcpServer;
QString m_sIpAddress;
int m_nIpPort;
QList<CMyTcpClientConnection*> m_lClients;
void removeClient( CMyTcpClientConnection *pClient );
public slots:
void serverNewConnection( void );
};
/** Client connection of a CMyTcpServer.
*/
class CMyTcpClientConnection : public QObject
{
Q_OBJECT
public:
explicit CMyTcpClientConnection( CMyTcpServer *parent, QTcpSocket *clientSocket );
CMyTcpServer *m_pTcpServer;
QTcpSocket *m_pSocket;
public slots:
void doDisconnect( void );
void doReadyRead( void );
};
To copy to clipboard, switch view to plain text mode
//------ CMyTcpServer() --------------------
CMyTcpServer
::CMyTcpServer(QObject *parent
) :{
if( !tcpServer->listen() ) // unable to start server
do stuff if TCP server cannot be started
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(serverNewConnection()) );
}
//----------- serverNewConnection -------------------
/** Signal handler for newConnection() when a new connection is requested.
* A new client connection object is created and added to the client list.
*/
void CMyTcpServer::serverNewConnection( void )
{
CMyTcpClientConnection *clientConnection = new CMyTcpClientConnection( this, tcpServer->nextPendingConnection() );
m_lClients.append( clientConnection );
}
//------------------------------------- removeClient --------------------------
/** Removes a specified client from the client list.
* This is called by the client when its connection is closed.
*/
void CMyTcpServer::removeClient( CMyTcpClientConnection *pClient )
{
int nIndex = m_lClients.indexOf( pClient ); // find position of client in list
if( nIndex >= 0 )
{
qDebug() << "client removed";
m_lClients.removeAt( nIndex ); // remove from list
pClient->deleteLater(); // delete object itself also
}
}
//=======================================
// CMyTcpClientConnection
//========================================
//------------------------------------- CMyTcpClientConnection ------------
/** Constructor.
* The TCP server is given as parent. It will maintain a list of client
* connections. The new TCP socket is given as parameter too. This is
* remembered by this object, and it becomes the parent of it too.
*/
CMyTcpClientConnection
::CMyTcpClientConnection( CMyTcpServer
*parent,
QTcpSocket *clientSocket
) :{
m_pTcpServer = parent;
m_pSocket = clientSocket;
m_pSocket->setParent( this );
connect(clientSocket, SIGNAL(disconnected()), SLOT(doDisconnect()));
connect(clientSocket, SIGNAL(readyRead()), SLOT(doReadyRead()) );
qDebug() << "add TCP client " << m_pSocket->peerAddress().toString() << m_pSocket->peerPort();
}
//------------------------------------- doDisconnect --------------------------
/** Signal handler when peer is closed.
* Removes this client from the TCP server client connection list.
* This client connection object will then be deleted.
*/
void CMyTcpClientConnection::doDisconnect( void )
{
m_pTcpServer->removeClient( this );
}
//------------------------------------- doReadyRead --------------------------
/** Signal handler when data is available from peer.
*
*/
void CMyTcpClientConnection::doReadyRead( void )
{
QByteArray aData
= m_pSocket
->readAll
();
// read all data from socket ...
}
//------ CMyTcpServer() --------------------
CMyTcpServer::CMyTcpServer(QObject *parent) :
QObject(parent)
{
tcpServer = new QTcpServer(this);
if( !tcpServer->listen() ) // unable to start server
do stuff if TCP server cannot be started
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(serverNewConnection()) );
}
//----------- serverNewConnection -------------------
/** Signal handler for newConnection() when a new connection is requested.
* A new client connection object is created and added to the client list.
*/
void CMyTcpServer::serverNewConnection( void )
{
CMyTcpClientConnection *clientConnection = new CMyTcpClientConnection( this, tcpServer->nextPendingConnection() );
m_lClients.append( clientConnection );
}
//------------------------------------- removeClient --------------------------
/** Removes a specified client from the client list.
* This is called by the client when its connection is closed.
*/
void CMyTcpServer::removeClient( CMyTcpClientConnection *pClient )
{
int nIndex = m_lClients.indexOf( pClient ); // find position of client in list
if( nIndex >= 0 )
{
qDebug() << "client removed";
m_lClients.removeAt( nIndex ); // remove from list
pClient->deleteLater(); // delete object itself also
}
}
//=======================================
// CMyTcpClientConnection
//========================================
//------------------------------------- CMyTcpClientConnection ------------
/** Constructor.
* The TCP server is given as parent. It will maintain a list of client
* connections. The new TCP socket is given as parameter too. This is
* remembered by this object, and it becomes the parent of it too.
*/
CMyTcpClientConnection::CMyTcpClientConnection( CMyTcpServer *parent, QTcpSocket *clientSocket ) :
QObject(parent)
{
m_pTcpServer = parent;
m_pSocket = clientSocket;
m_pSocket->setParent( this );
connect(clientSocket, SIGNAL(disconnected()), SLOT(doDisconnect()));
connect(clientSocket, SIGNAL(readyRead()), SLOT(doReadyRead()) );
qDebug() << "add TCP client " << m_pSocket->peerAddress().toString() << m_pSocket->peerPort();
}
//------------------------------------- doDisconnect --------------------------
/** Signal handler when peer is closed.
* Removes this client from the TCP server client connection list.
* This client connection object will then be deleted.
*/
void CMyTcpClientConnection::doDisconnect( void )
{
m_pTcpServer->removeClient( this );
}
//------------------------------------- doReadyRead --------------------------
/** Signal handler when data is available from peer.
*
*/
void CMyTcpClientConnection::doReadyRead( void )
{
QByteArray aData = m_pSocket->readAll(); // read all data from socket
...
}
To copy to clipboard, switch view to plain text mode
Bookmarks