QT局域网聊天程序中获取对方的电脑名字或者登录用户名字的解决方案

最近在做一个免费的QT的Helpdesk的项目,为了获取对方电脑的windows登录名字,用了一个巧妙的方法,就是在对方发送的消息后面添加了“///”+“Windows login name”, 然后在readRead解码的时候以///为关键字来获取对方的登录名称,成功了,特地分享给大家,如果有任何的疑问,可以问我。

国内程序下载地址: https://gitee.com/mz13692277450/superhelpdesk/tree/master

国外: https://sourceforge.net/projects/super-helpdesk/

头文件:

QString WindowsUserName;o

QUdpSocket *m_udpSocket = nullptr;
    QString localHostName1;

槽函数:

m_udpSocket = new QUdpSocket(this);

    connect(m_udpSocket,&QUdpSocket::readyRead,this,&HelpDeskClientDialog::on_readyRead)
下面是具体代码:

//Chat module start..................................................................



void HelpDeskClientDialog::BindPort()
{
    //ui->lineEdit_LocalPort->setText ("3000");
    qint16 port =8686; // = ui->lineEdit_LocalPort->text ().toInt ();
    if(m_udpSocket->bind(port))
    {
        ui->plainTextEdit_Chat_Messages->appendPlainText ("**Sucessed to bind port**");
        QString portAdd = "**Bind port: "+QString::number(m_udpSocket->localPort())+"**";
        ui->plainTextEdit_Chat_Messages->appendPlainText (portAdd);
        // ui->pushButton_BindPort->setEnabled(false);
        //  ui->pushButton_UnBindPort->setEnabled(true);
    }
    else
    {
        ui->plainTextEdit_Chat_Messages->appendPlainText("**Failure to bind port: 8687**");
    }
}


void HelpDeskClientDialog::UnBindPort()
{
    //解除绑定
    m_udpSocket->abort();
    //ui->pushButton_BindPort->setEnabled(true);
    // ui->pushButton_UnBindPort->setEnabled(false);
    ui->plainTextEdit_Chat_Messages->appendPlainText("**已解除绑定");
}

void HelpDeskClientDialog::on_readyRead()
{
    //是否还有待读取的传入数据报
    while(m_udpSocket->hasPendingDatagrams())
    {
        QByteArray data;
        //返回待读取的数据报的字节数
        data.resize(m_udpSocket->pendingDatagramSize());
        QHostAddress peerAddr;
        quint16 peerPort;
        //读取数据报的内容
        m_udpSocket->readDatagram(data.data(),data.size(),&peerAddr,&peerPort);
        QString str = data.data();
        QStringList list = str.split("///");
        QString str1 = list.at(list.length()-1);
        QString strReceiveWithoutName = list.at(list.length ()-2);
        //  QString peer = "[From ] +"+peerAddr.toString()+":"+QString::number(peerPort)+"] ";
        QString peer1 = "[From "+str1+"]: ";
        //  ui->plainTextEdit_Chat_Messages->appendPlainText(peer+str);
        QTextCharFormat fmt;
        //字体色
        fmt.setForeground(QBrush(Qt::blue));
        ui->plainTextEdit_Chat_Messages->mergeCurrentCharFormat(fmt);
        ui->plainTextEdit_Chat_Messages->appendPlainText(peer1+strReceiveWithoutName);
    }
}

void HelpDeskClientDialog::on_pushButton_SendMessage_clicked()
{
    QString dstIp = "127.0.0.1"; // = ui->lineEdit_SendTo_IP->text ();
    QHostAddress dstAddr(dstIp);
    //目标端口
    quint16 dstPort = 8687; //= ui->lineEdit_SendTo_Port->text().toInt ();
    QString msg = ui->plainTextEdit_Send_Messages->toPlainText () + "///" + "UUTT";  //因为是在同一台机器上测试,用了固定名字来代替。实际中可以用获取的登录名字。
    QByteArray str = msg.toUtf8();
    //发出数据报
    m_udpSocket->writeDatagram(str,dstAddr,dstPort);
    QTextCharFormat fmt;
    //字体色
    fmt.setForeground(QBrush(Qt::darkRed));
    QString msgLocal = ui->plainTextEdit_Send_Messages->toPlainText ();
    ui->plainTextEdit_Chat_Messages->mergeCurrentCharFormat(fmt);
    ui->plainTextEdit_Chat_Messages->appendPlainText("[" +WindowsUserName +"]:"+msgLocal);
    ui->plainTextEdit_Send_Messages->clear ();
    qDebug() << "Send out message to: " <<str << dstAddr << dstPort << msg;
}
//Chat module end.....................................................................



 

 

 

获取机器名和登录用户名:

 

 

void HelpDeskClientDialog::get_UserName()
{
    WindowsUserName = qgetenv("USERNAME");
    if (WindowsUserName.isEmpty())
    {
        WindowsUserName = qgetenv("NoName");
    }
    ui->lineEdit_UserName->setText (WindowsUserName);
    QTest::qSleep (100);
}
void HelpDeskClientDialog::get_MachineName()
{
    localHostName1 = QHostInfo::localHostName();
    ui->lineEdit_HostName->setText (localHostName1);
    QTest::qSleep (100);
}
QString HelpDeskClientDialog::getIpAdress()
{
    QString localIPAddress = "";
    QList<QHostAddress>listAddress = QNetworkInterface::allAddresses();
    for(int j = 0; j < listAddress.size(); j++)
    {
        if(!listAddress.at(j).isNull() &&
                listAddress.at(j).protocol() ==  QAbstractSocket::IPv4Protocol
                && listAddress.at(j) != QHostAddress::LocalHost)
        {
            localIPAddress = listAddress.at(j).toString();
            ui->lineEdit_IPAddress->setText (localIPAddress);
            return localIPAddress;
        }
    }
    return localIPAddress;

}

 

posted @ 2022-08-30 20:57  商君治国安邦之张莽  阅读(199)  评论(0编辑  收藏  举报