How to receive communication from Multi Client in TcpServer
-
I want to receive communication from Multi Client in TcpServer.
I was able to connect, but when I did a readyRead, only one of the communications was being received.
The client will send the communication at about the same time.
The maximum number of connections is variable.
Can I receive at the same time without using QThread?This is my idea of a connect method.
class ManagementServerLanTcp : public QObject { Q_OBJECT public: ManagementServerLanTcp(); ~ManagementServerLanTcp(); void fnTcpServerSet(QString sTcpServerIp, quint16 uiTcpServerPort); void fnSocketClose(); public slots: void fnServerConnection(); void fnTestRead(); private: QTcpServer* m_pManagementServer; QTcpSocket* m_pManagementSocket; QList<QTcpSocket*> connectList; }; ManagementServerLanTcp::ManagementServerLanTcp() { pucRecvBuffer = new unsigned char[TCP_BUFFSIZE]; pucSendBuffer = new unsigned char[TCP_BUFFSIZE]; m_pManagementServer = new QTcpServer(); } ManagementServerLanTcp::~ManagementServerLanTcp() { if ( m_pManagementSocket != nullptr ) { m_pManagementSocket->close(); delete m_pManagementSocket; } if ( m_pManagementServer != nullptr ) { m_pManagementServer->close(); delete m_pManagementServer; } } void ManagementServerLanTcp::fnTcpServerSet(QString sTcpServerIp, quint16 uiTcpServerPort) { m_pManagementServer->listen(QHostAddress(sTcpServerIp), uiTcpServerPort); connect(m_pManagementServer, SIGNAL(newConnection()), this, SLOT(fnServerConnection())); } void ManagementServerLanTcp::fnServerConnection() { m_pManagementSocket = new QTcpSocket(); m_pManagementSocket = m_pManagementServer->nextPendingConnection(); connectList.append(m_pManagementSocket); int connectListVal = connectList.count() - 1; for (int i = 0; i < connectList.count(); i++){ connect( connectList[connectListVal], SIGNAL(readyRead()), this, SLOT(fnTestRead())); } }
-
@w-tkm you may want to take a look at Threaded Fortune Server example
-
There's no such thing as the "same time", even with threads. You have no control over the arrival of network traffic, its assembly by the operating system, its delivery to your application, or the scheduling of the threads in your application.
You can, and generally should, service multiple clients without using threads. Given the code I can see, threads will only cause you more problems than any gain you think you might get.
Here are some general observations:
- Your constructor does not initialise m_pManagementSocket and destructor assumes it has been. What happens if a connection is never made and you destroy this object?
- Using fixed size buffers somewhere (unseen). This is a risky choice.
- Create and immediately orphan a new QTCpSocket on every connection
- A list of connections with no obvious purpose
- You connect every QTcpSocket in the list to a slot every time a new connection is made.
-
@w-tkm you may want to take a look at Threaded Fortune Server example
-
@Pablo-J-Rogina
OK, Thanks.
I'll take that as a reference.