m_pUDPSocket = new QUdpSocket(this); m_pUDPSocket->bind(QHostAddress::Any, m_nPort); sendImOnline(); connect(m_pUDPSocket, SIGNAL(readyRead()), SLOT(slotReadData())); m_pTimer = new QTimer; m_pTimer->start(1000); connect(m_pTimer, SIGNAL(timeout()), SLOT(slotSendImOnline()));
void MainWindow::sendImOnline(){ QByteArray byteArray; QDataStream dataOut(&byteArray, QIODevice::WriteOnly); dataOut<<IM_ONLINE; dataOut<<m_sUserName; m_pUDPSocket->writeDatagram( byteArray , QHostAddress::Broadcast , m_nPort );}
void MainWindow::slotReadData(){ QByteArray byteArray; byteArray.resize(m_pUDPSocket->pendingDatagramSize()); QHostAddress hostAdress; m_pUDPSocket->readDatagram(byteArray.data(), byteArray.size(), &hostAdress, &m_nPort); QDataStream dataIn(&byteArray, QIODevice::ReadOnly); qint32 nRequestOrMessageType; dataIn>>nRequestOrMessageType; ..... ля ля ля, еще код ..... }}
C++ (Qt) sendImOnline(); connect(m_pUDPSocket, SIGNAL(readyRead()), SLOT(slotReadData()));
C++ (Qt) connect(m_pTimer, SIGNAL(timeout()), SLOT(slotSendImOnline()));
void Client::sendImOnlineMessage(){ QByteArray byteArray; QDataStream dataOut(&byteArray, QIODevice::WriteOnly); dataOut<<quint8(0); dataOut<<m_sLocalHostName; m_pUdpSocket->writeDatagram( byteArray , QHostAddress::Broadcast , m_nPort );}
m_pUdpSocket = new QUdpSocket; m_pUdpSocket->bind(QHostAddress::Any, m_nPort);
m_pUdpSocket = new QUdpSocket; m_pUdpSocket->bind(QHostAddress::Any, m_nPort); connect(m_pUdpSocket, SIGNAL(readyRead()), SLOT(slotReadMessage()));
void Server::slotReadMessage(){ QMessageBox::information(this, "...", "Got message!");}