QT网络编程需要在pro文件中添加QT += network
一、QHostInfo
网络信息
//获得本机主机名
QString localHostName = QHostInfo::localHostName();
//通过主机名获取主机信息
QHostInfo hostInfo = QHostInfo::fromName(localHostName);
//获取所有的IP地址列表
QList<QHostAddress> listAddress = hostInfo.addresses();
二、QNetworkInterface
一个主机IP地址和网络接口列表
QString detail="";
//获取所有的网络硬件接口
QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();
for(int i=0;i<list.count();i++)
{
QNetworkInterface interface=list.at(i);
detail=detail+tr("设备:")+interface.name()+"\n";
detail=detail+tr("硬件地址:")+interface.hardwareAddress()+"\n";
//每个网络接口包含0个或多个IP地址
QList<QNetworkAddressEntry> entryList=interface.addressEntries();
for(int j=0;j<entryList.count();j++)
{
QNetworkAddressEntry entry=entryList.at(j);
detail=detail+"\t"+tr("IP 地址:")+entry.ip().toString()+"\n";
detail=detail+"\t"+tr("子网掩码:")+entry.netmask().toString()+"\n";
detail=detail+"\t"+tr("广播地址:")+entry.broadcast().toString()+"\n";
}
}
QMessageBox::information(this,tr("Detail"),detail);
三、UDP编程
//server
QUdpSocket *udpSocket = new QUdpSocket(this);
//QHostAddress::Broadcast:广播到本机的所有IP,当非本机时,使用接收端主机IP
udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,5555);
//client
QUdpSocket *udpSocket = new QUdpSocket(this);
//当有数据到来时,发出readyread信号
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
bool result=udpSocket->bind(5555); //绑定端口
void UdpClient::dataReceived()
{
while(udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
//根据接收的数据大小来重新定义QByteArray的大小
datagram.resize(udpSocket->pendingDatagramSize());
//接收数据包
udpSocket->readDatagram(datagram.data(),datagram.size());
//获取具体数据
QString msg=datagram.data();
}
}
四、TCP编程
//server
//只是截取了重要代码,不要直接拷贝,要根据自己的程序结构添加到对应的文件
QList<QTcpSocket *> tcpClientSocketList;
QTcpSocket * socket;
QTcpServer *tcpServer = new QTcpServer(this);
//监听指定端口
tcpServer->listen(QHostAddress::Any,6666);
//有新的连接时会发出newConnection信号
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
void MainWindow::acceptConnection()
{
socket = tcpServer->nextPendingConnection();
//将新连接的Socket放到List中进行管理
tcpClientSocketList.append(socket);
connect(socket,SIGNAL(readyread()),this , SLOT(dataReceive()));
}
void MainWindow::dataReceive()
{
QString datas = tcpSocket->readAll();
ui->lineEdit->setText(datas);
}
//client
QTcpSocket * tcpSocket ;
this->tcpSocket = new QTcpSocket(this);
//连接到服务器
tcpSocket->connectToHost("127.0.0.1",6666);
connect(tcpSocket,SIGNAL(connected()),this,SLOT(slotConnected()));
connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
//连接到服务器后处理函数
void TcpClient::slotConnected()
{
}
//发送数据
void TcpClient::slotSend()
{
if(sendLineEdit->text()=="")
{
return ;
}
QString msg=userName+":"+sendLineEdit->text();
tcpSocket->write(msg.toLatin1(),msg.length());
sendLineEdit->clear();
}
//断开连接
void TcpClient::slotDisconnected()
{
}
//接收数据
void TcpClient::dataReceived()
{
while(tcpSocket->bytesAvailable()>0)
{
QByteArray datagram;
datagram.resize(tcpSocket->bytesAvailable());
tcpSocket->read(datagram.data(),datagram.size());
QString msg=datagram.data();
contentListWidget->addItem(msg.left(datagram.size()));
}
}