Qt 服务器 获取发送客户端的QTcpSocket对象 和 该socket的ip和端口号

遇到问题:

        众多客户端发送过来请求数据,如何找到该客户端的 QTcpsocket对象给该对象回复消息?

解决办法:

        QTcpSocket *ptr =   dynamic_cast<QTcpSocket *>(sender());

        解释:通过 dynamic_cast强行转换。QTcpSocket *类型的对象、谁发送了信号就会触发

                   sender()信号、获取该信号的QTcpsocket *对象。

        下面代码可以获取socket的port和ip

    //监听套接字
    tcpServer=new QTcpServer(this);

    //监听
    tcpServer->listen(QHostAddress::Any,8888);

    connect(tcpServer,&QTcpServer::newConnection,
            [=](){
                    //取出建立好连接的套接字
                    tcpSocket=tcpServer->nextPendingConnection();

                    //获取对方的IP和端口
                    QString ip=tcpSocket->peerAddress().toString();
                    qint16 port =tcpSocket->peerPort();
                    QString ipDate=QString("[ip=%1 port=%2] 建立好连接了!!").arg(ip).arg(port);

                    ui->textEdit->append(ipDate);
                    ui->buttonFile->setEnabled(true);
                }

            );

完整代码如下

        

#include "server_code.h"

Server_code::Server_code(QObject *parent)
{
    socket_num =0;
    listen(QHostAddress::Any,8888);
    connect_db();
    qDebug()<<"服务器端口号已经开启: 8888";
}

void Server_code::connect_db()
{
    //创建连接对象
    db = QSqlDatabase::addDatabase("QMYSQL");
    //连接参数

    db.setPort(3306);
//    db.setHostName("47.105.188.189");
    db.setHostName("127.0.0.1");
    db.setDatabaseName("voice");
    db.setUserName("root");
    db.setPassword("123456");

    //如果连接成功
    if (db.open())
    {
        qDebug()<<"连接成功";

        //查询  通过全局指针变量query指向连接成功的地址。
        query = new QSqlQuery();

    }
    else
    {
         qDebug()<<"连接失败";

    }
}

void Server_code::incomingConnection(qintptr socketDescriptor)
/*产生链接的时候执行这个     这个是产生socket的函数*/
{
    //生成tcpSocket对象
    socket = new QTcpSocket(this);



    //绑定这个链接来的兑现到socket中
    socket->setSocketDescriptor(socketDescriptor);


    only_socket.insert(socket_num,socket);

    //第一次连接发送 所有型号数据给客户端
    model();

    //读取他发来的消息、 使用receiveMessage去接收
    connect(socket,SIGNAL(readyRead()),this,SLOT(receiveMessage()));

    //传递数据 谁上线了
//    emit giveMsg(socket->peerAddress().toString() + "上线",2);
    qDebug()<<socket->peerAddress().toString() + "上线";
    socket_num++;

}



void Server_code::receiveMessage()
{

    QTcpSocket* ptr  =dynamic_cast<QTcpSocket*>(sender());

    //读取 字节流的方式  读取客户端发来的数据。
    QByteArray arr = ptr->readAll();
    QString str = arr.data();
    qDebug()<<str;
    //把信号传递出去  其中1代表客户端机型、 后期要加用户名和密码在这里添加。
    emit giveMsg(str,1,ptr);

}

void Server_code::model()
{
    //连接的时候、显示在客户端的型号数据

    query->exec("select 型号 from FRP_attr");
    QString model;
    while(query->next())
    {
         model = model + query->value(0).toString() + ",";
    };

    model = model + "型号";
    QByteArray send_model = model.toUtf8();
    socket->write(send_model);
}




//测试声音系数
void Server_code::model_voice(QString one_model,QTcpSocket* ptr){

    query->exec("select 测试吸声系数 from 型号吸声系数表 where 型号 = '"+one_model+"' and 频率 < 5001");
    QString test_voice;
    while(query->next())
    {
         test_voice = test_voice + query->value(0).toString() + ",";
    };

    test_voice = test_voice + "$$$";

//    qDebug()<<test_voice;

    QByteArray test_voice_lot = test_voice.toUtf8();
    ptr->write(test_voice_lot);
}


//测试物理/声学系数
void Server_code::model_detail_data(QString one_model,QTcpSocket* ptr){

    query->exec("select * from frp_attr where 型号 = '"+one_model+"'");
    QString model_info_data;
    while(query->next())
    {
         model_info_data = model_info_data + query->value(0).toString() + ",";
         model_info_data = model_info_data + query->value(1).toString() + ",";
         model_info_data = model_info_data + query->value(2).toString() + ",";
         model_info_data = model_info_data + query->value(3).toString() + ",";
         model_info_data = model_info_data + query->value(4).toString() + ",";
         model_info_data = model_info_data + query->value(5).toString() + ",";
         model_info_data = model_info_data + query->value(6).toString() + ",";
         model_info_data = model_info_data + query->value(7).toString() + ",";
         model_info_data = model_info_data + query->value(8).toString() + ",";
         model_info_data = model_info_data + query->value(9).toString() + ",";
         model_info_data = model_info_data + query->value(10).toString() + ",";
         model_info_data = model_info_data + query->value(11).toString() + "|";
    };
    model_info_data = model_info_data + "$$$";
    QByteArray model_info_data_lot = model_info_data.toUtf8();
    ptr->write(model_info_data_lot);
}

//理论系数
void Server_code::li_model_voice(QString one_model,QTcpSocket* ptr){

    query->exec("select `理论吸声系数` from 型号吸声系数表 where 型号 = '"+one_model+"'and 频率 < 5001");
    QString li_voice;
    while(query->next())
    {
         li_voice = li_voice + query->value(0).toString() + ",";
    };
//    li_voice = li_voice + "理论吸声系数";
    QByteArray li_voice_lot = li_voice.toUtf8();
    ptr->write(li_voice_lot);
}


  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值