遇到問題:
????????眾多客戶端發(fā)送過來(lái)請(qǐng)求數(shù)據(jù),如何找到該客戶端的 QTcpsocket對(duì)象給該對(duì)象回復(fù)消息?
解決辦法:
????????QTcpSocket *ptr =?? dynamic_cast<QTcpSocket *>(sender());
? ? ? ? 解釋:通過?dynamic_cast強(qiáng)行轉(zhuǎn)換。QTcpSocket *類型的對(duì)象、誰(shuí)發(fā)送了信號(hào)就會(huì)觸發(fā)
? ? ? ? ? ? ? ? ? ?sender()信號(hào)、獲取該信號(hào)的QTcpsocket *對(duì)象。
????????下面代碼可以獲取socket的port和ip
//監(jiān)聽套接字
tcpServer=new QTcpServer(this);
//監(jiān)聽
tcpServer->listen(QHostAddress::Any,8888);
connect(tcpServer,&QTcpServer::newConnection,
[=](){
//取出建立好連接的套接字
tcpSocket=tcpServer->nextPendingConnection();
//獲取對(duì)方的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);
}
);
完整代碼如下文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-531534.html
????????文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-531534.html
#include "server_code.h"
Server_code::Server_code(QObject *parent)
{
socket_num =0;
listen(QHostAddress::Any,8888);
connect_db();
qDebug()<<"服務(wù)器端口號(hào)已經(jīng)開啟: 8888";
}
void Server_code::connect_db()
{
//創(chuàng)建連接對(duì)象
db = QSqlDatabase::addDatabase("QMYSQL");
//連接參數(shù)
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)
/*產(chǎn)生鏈接的時(shí)候執(zhí)行這個(gè) 這個(gè)是產(chǎn)生socket的函數(shù)*/
{
//生成tcpSocket對(duì)象
socket = new QTcpSocket(this);
//綁定這個(gè)鏈接來(lái)的兌現(xiàn)到socket中
socket->setSocketDescriptor(socketDescriptor);
only_socket.insert(socket_num,socket);
//第一次連接發(fā)送 所有型號(hào)數(shù)據(jù)給客戶端
model();
//讀取他發(fā)來(lái)的消息、 使用receiveMessage去接收
connect(socket,SIGNAL(readyRead()),this,SLOT(receiveMessage()));
//傳遞數(shù)據(jù) 誰(shuí)上線了
// emit giveMsg(socket->peerAddress().toString() + "上線",2);
qDebug()<<socket->peerAddress().toString() + "上線";
socket_num++;
}
void Server_code::receiveMessage()
{
QTcpSocket* ptr =dynamic_cast<QTcpSocket*>(sender());
//讀取 字節(jié)流的方式 讀取客戶端發(fā)來(lái)的數(shù)據(jù)。
QByteArray arr = ptr->readAll();
QString str = arr.data();
qDebug()<<str;
//把信號(hào)傳遞出去 其中1代表客戶端機(jī)型、 后期要加用戶名和密碼在這里添加。
emit giveMsg(str,1,ptr);
}
void Server_code::model()
{
//連接的時(shí)候、顯示在客戶端的型號(hào)數(shù)據(jù)
query->exec("select 型號(hào) from FRP_attr");
QString model;
while(query->next())
{
model = model + query->value(0).toString() + ",";
};
model = model + "型號(hào)";
QByteArray send_model = model.toUtf8();
socket->write(send_model);
}
//測(cè)試聲音系數(shù)
void Server_code::model_voice(QString one_model,QTcpSocket* ptr){
query->exec("select 測(cè)試吸聲系數(shù) from 型號(hào)吸聲系數(shù)表 where 型號(hào) = '"+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);
}
//測(cè)試物理/聲學(xué)系數(shù)
void Server_code::model_detail_data(QString one_model,QTcpSocket* ptr){
query->exec("select * from frp_attr where 型號(hào) = '"+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);
}
//理論系數(shù)
void Server_code::li_model_voice(QString one_model,QTcpSocket* ptr){
query->exec("select `理論吸聲系數(shù)` from 型號(hào)吸聲系數(shù)表 where 型號(hào) = '"+one_model+"'and 頻率 < 5001");
QString li_voice;
while(query->next())
{
li_voice = li_voice + query->value(0).toString() + ",";
};
// li_voice = li_voice + "理論吸聲系數(shù)";
QByteArray li_voice_lot = li_voice.toUtf8();
ptr->write(li_voice_lot);
}
到了這里,關(guān)于Qt 服務(wù)器 獲取發(fā)送客戶端的QTcpSocket對(duì)象 和 該socket的ip和端口號(hào)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!