主机信息查询

QT += network
#include <QHostInfo>
#include <QNetworkInterface>
QList<QHostAddress> addList=host.addresses();
aHost.protocol();
QHostInfo::localHostName();
QHostInfo hostInfo=QHostInfo::fromName(hostName);
QHostInfo::lookupHost(hostname,this,SLOT(lookedUpHostInfo(QHostInfo)));
QList<QNetworkInterface> list=QNetworkInterface::allInterfaces();
QList<QNetworkAddressEntry> entryList=aInterface.addressEntries();
TCP通信


#include <QtNetwork>
QTcpServer *tcpServer=new QTcpServer(this);
QString IP=ui->comboIP->currentText();
quint16 port=ui->spinPort->value();
tcpServer->listen(addr,port);
QTcpSocket *tcpSocket=tcpServer->nextPendingConnection();
QString msg=ui->editMsg->text();
QByteArray str=msg.toUtf8();
str.append('\n');
tcpSocket->write(str);
while(tcpSocket->canReadLine())
ui->plainTextEdit->appendPlainText(+tcpSocket->readLine());
if (tcpServer->isListening())
{
tcpServer->close();
}
tcpSocket->deleteLater();
tcpServer->close();;
QTcpSocket* tcpClient=new QTcpSocket(this);
QString addr=ui->comboServer->currentText();
quint16 port=ui->spinPort->value();
tcpClient->connectToHost(addr,port);
tcpClient->disconnectFromHost();
UDP


#include <QUdpSocket>
QUdpSocket *udpSocket=new QUdpSocket(this);
udpSocket->bind(port)
while(udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress peerAddr;
quint16 peerPort;
udpSocket->readDatagram(datagram.data(),datagram.size(),&peerAddr,&peerPort);
QString str=datagram.data();
}
QString msg=ui->editMsg->text();
QByteArray str=msg.toUtf8();
udpSocket->writeDatagram(str,targetAddr,targetPort);
udpSocket->writeDatagram(str,QHostAddress::Broadcast,targetPort);
udpSocket->abort();
组播



QUdpSocket *udpSocket=new QUdpSocket(this);
QHostAddress groupAddress=QHostAddress(IP);
udpSocket>setSocketOption(QAbstractSocket::MulticastTtlOption,1);
if (udpSocket->bind(QHostAddress::AnyIPv4, groupPort, QUdpSocket::ShareAddress))
{
udpSocket->joinMulticastGroup(groupAddress);
}
udpSocket->leaveMulticastGroup(groupAddress);
udpSocket->abort();
HTTP




#include <QDesktopServices>
#include <QtNetwork>
QUrl newUrl = QUrl::fromUserInput(urlSpec);
QNetworkAccessManager networkManager;
QNetworkReply *reply = networkManager.get(QNetworkRequest(newUrl));
reply->readAll();
reply->deleteLater();