您的位置:首页 > 编程语言 > Qt开发

QT5网络编程笔记 单客户端与多客户端

2017-10-09 08:57 375 查看
//QHostInfo类作用,获取主机名,也可以通过主机名来查找IP地址,或者通过IP地址来反向查找主机名。
QString localHostName = QHostInfo::localHostName();   //获取计算机名称
qDebug() << "LocalHostName:" << localHostName;
//获取IP地址
QHostInfo info = QHostInfo::fromName(localHostName);  //获取ip地址,包括ipv6
qDebug() << "IP Address:" << info.addresses();
foreach(QHostAddress address, info.addresses())  //取出IPv4地址
{
if (address.protocol() == QAbstractSocket::IPv4Protocol)
qDebug() << "IPv4 Address:" << address.toString();
ui->comboBox->addItem(address.toString());
}
foreach (QHostAddress address, QNetworkInterface::allAddresses())//去除所有地址包括127.0.0.1
{
qDebug() << "Address:" << address;
}
 服务器端设置:        QTcpserver QTcpsocket     QTcpserver主要负责监听端口,如果有链接 获得客户端套接字 if(ui->port->text().isEmpty())
   {
QMessageBox::about(this,tr("提示"),tr("端口没有设置"));
}
if(ui->listen->text() == tr("侦听"))
   {
//从输入框获取端口号
int port = ui->port->text().toInt();
//监听指定的端口
if(!m_tcpserver->listen(QHostAddress::Any, port))
   {
//若出错,则输出错误信息
qDebug()<<m_tcpserver->errorString();
return;
    }
qDebug()<< "Listen succeessfully!";
}
  当监听到链接时会产生newconnect()信号,然后创建客户端套接字 (这个是单客户端)//获取客户端连接
m_tcpsocket = m_tcpserver->nextPendingConnection();
//连接QTcpSocket的信号槽,以读取新数据
QObject::connect(m_tcpsocket, &QTcpSocket::readyRead, this, &MainWindow::socket_Read_Data);
QObject::connect(m_tcpsocket, &QTcpSocket::disconnected, this, &MainWindow::socket_Disconnected);
ui->show->append(m_tcpsocket->peerAddress().toString());  //获得客户端的ip地址
//发送按键使能
ui->send->setEnabled(true);
qDebug() << "A Client connect!";
将textedit的数据送到指定的客户端上, qDebug()<<"send:"<<ui->sendtext->toPlainText(); 
QByteArray datasend=ui->sendtext->toPlainText().toLocal8Bit();
m_tcpsocket->write(datasend);
m_tcpsocket->flush();
ui->sendtext->clear();
读取网络数据并解析:QByteArray requestData;
//读取缓冲区数据
requestData = m_tcpsocket->readAll();
if(requestData.at(0)==102)
{
twoBytes=requestData.mid(1,4); //
int j = twoBytes[3] & 0x000000ff;
j |= ((twoBytes[2] << 8) & 0x0000ff00);
j |= ((twoBytes[1] << 16) & 0x00ff0000);
j |= ((twoBytes[0] << 24) & 0xff000000);
twoBytes2=requestData.mid(5,4); //
int d = twoBytes2[3] & 0x000000ff;
d |= ((twoBytes2[2] << 8) & 0x0000ff00);
d |= ((twoBytes2[1] << 16) & 0x00ff0000);
d |= ((twoBytes2[0] << 24) & 0xff000000);
twoBytes3=requestData.mid(9,4);//
int m = twoBytes3[3] & 0x000000ff;
m |= ((twoBytes3[2] << 8) & 0x0000ff00);
m |= ((twoBytes3[1] << 16) & 0x00ff0000);
m |= ((twoBytes3[0] << 24) & 0xff000000);
twoBytes4=requestData.mid(13,4);//
union MyUnion
   {
unsigned char info[4];
float f;
    }FLOAT_DATA;
FLOAT_DATA.info[0]=twoBytes4[0];
FLOAT_DATA.info[1]=twoBytes4[1];
FLOAT_DATA.info[2]=twoBytes4[2];
FLOAT_DATA.info[3]=twoBytes4[3];
twoBytes5=requestData.mid(17,4);
union YouUnion
   {
unsigned char info[4];
float f;
    }FLOAT_DATA1;
FLOAT_DATA1.info[0]=twoBytes5[0];
FLOAT_DATA1.info[1]=twoBytes5[1];
FLOAT_DATA1.info[2]=twoBytes5[2];
FLOAT_DATA1.info[3]=twoBytes5[3];
  }
requestData.clear();    //清除缓冲区
多客户端与单客户端的区别在于产生newconnect()信号时的处理方式
m_tcpsocket = m_tcpserver->nextPendingConnection();
socket_list->append(m_tcpsocket); //定义一个QList<QTcpSocket>数据类型的对象,存储链接的客户端套接字信息。
//连接QTcpSocket的信号槽,以读取新数据
QObject::connect(m_tcpsocket, &QTcpSocket::readyRead, this, &MainWindow::socket_Read_Data);
QObject::connect(m_tcpsocket, &QTcpSocket::disconnected, this, &MainWindow::socket_Disconnected);
ui->show->append(m_tcpsocket->peerAddress().toString());
//发送按键使能
ui->send->setEnabled(true);
qDebug() << "A Client connect!";
多客户端解析数据时的方式QByteArray message;
for(int i = 0;i < socket_list->length();i ++)
{
message = socket_list->at(i)->readAll() ;
if(!message.isEmpty())
   {
qDebug()<< message;
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: