/**
 * @override QTcpServer的槽函数:收到TCP连接请求
 *
 * 创建子线程,每个线程的销毁槽函数
 */
void MasterMultiThreadTcpServer::incomingConnection()
{
    tempsocket = tcpServer->nextPendingConnection();

    if (tempsocket == NULL)
        return;

    for (int i = 0; i < numOfThreads; i++)
    {
        QString ip = threads.at(i)->getClientIP();
        if (ip.compare(tempsocket->peerAddress().toString()) == 0)
        {
            threads.at(i)->setTcpSocket(tempsocket);

            qDebug() << "connection from slave";
            //emit signalMsgToMaster("-7, new connection");
            return;
        }
    }
    MasterServerThread *thread = new MasterServerThread();
    connect(thread, SIGNAL(signalMsg(QString)), this, SLOT(printReceivedMsg(QString)), Qt::QueuedConnection);
    connect(thread, SIGNAL(signalError(QString)), this, SLOT(printReceivedError(QString)), Qt::QueuedConnection);
    connect(thread, SIGNAL(closeConnection(QString)), this, SLOT(removeServerThread(QString)));
    thread->setTcpSocket(tempsocket); // 在connet之后保证onConnection槽函数中的消息也能被接收到

    /*****************************************/
    slavercdmutex.lock();
    threads.append(thread);
    numOfThreads++;
    slavercdmutex.unlock();
    qDebug() << "connection from slave";
    //emit signalMsgToMaster("-7, new connection");
}
Exemple #2
0
void MainWindow::on_pushButton_signal_clicked()
{
	static int counter = 0;

	QString msg = QString("counter %1").arg(counter++);

	qDebug("emit signal : %s", msg.toAscii().constData());
	emit signalMsg(msg);
}
Exemple #3
0
void Server::incomingConnection(qintptr handle)
{
    TcpClientSocket *client = new TcpClientSocket(this);
    client->setSocketDescriptor(handle);

    connect(client, SIGNAL(signalMsg(qintptr)), this, SLOT(slotReadMsg(qintptr)));
    connect(client, SIGNAL(signalDisconnected(qintptr, QString)), this, SLOT(slotDisconnected(qintptr, QString)));

    clientList.insert(handle, client);
}
void OfficeServerThread::initTcpSocket()
{
    connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(emitdisconnect()));
    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMessageFromSlave()), Qt::DirectConnection);
    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
                this, SLOT(displayErrorFromSlave(QAbstractSocket::SocketError)), Qt::DirectConnection);


    // 一个主线程和n个子线程通信怎么搞?这样用消息槽搞不定,消息的参数无法传入子线程
    connect(ServerProgram::getServerProgramInstance()->getMultiThreadTcpServer(),
            SIGNAL(msgToBeSent(const QString&)), this, SLOT(sendMessage(const QString&)), Qt::DirectConnection);
    connect(ServerProgram::getServerProgramInstance()->getMultiThreadTcpServer(),
            SIGNAL(msgToBeSentToOneSlave(QString&, QString&)), this, SLOT(sendMessageToOne(QString&, QString&)), Qt::DirectConnection);

    //connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(mineDeleteLater()));
    QHostAddress address =  tcpSocket->peerAddress();
    clientip = address.toString();
    setClientIPStr(clientip);
    qDebug()<<"address:"<<address;

    emit signalMsg(QString("%1,-7").arg(clientidstr));
    qDebug() << "init ok";
}
Exemple #5
0
void MainWindow::on_pushButton_clicked()
{
	qDebug("run dlg");


	Dialog dlg(this);
	;

	connect(this, SIGNAL(signalMsg(QString)), &dlg, SLOT(slotMsg(QString)));

	dlg.show();

#if 0
	if ( dlg.exec() == QDialog::Accepted )
	{
		qDebug("dlg accepted");
	}
	else
	{
		qDebug("dlg rejected");
	}
#endif
}
Exemple #6
0
void Server::slotReadMsg(qintptr sockd)
{
    emit signalMsg(sockd);
}