QT += network
QHostAddress IP地址
QHostInfo 主机信息
QNetworkInterface 主机的IP地址和网络接口列表
//获取主机名
QString localHostName = QHostInfo::localHostName();
ui->label_hostname->setText(localHostName);
//获取本机的IP地址
QHostInfo info = QHostInfo::fromName(localHostName);
qDebug()<<"IP:"< address = info.addresses();
foreach (QHostAddress addr, address)
{
//挑选出ipv4的地址
if(addr.protocol() == QAbstractSocket::IPv4Protocol)
qDebug()<
QList addrs = QNetworkInterface::allAddresses();
foreach (QHostAddress addr, addrs)
{
//挑选出ipv4的地址
if(addr.protocol() == QAbstractSocket::IPv4Protocol
&& addr.toString() != "127.0.0.1")
{
qDebug()<label_ip->setText(addr.toString());
}
}
//访问更详细的网络接口信息
QList list = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface i, list) {
qDebug()< addressEntry = i.addressEntries();
foreach (QNetworkAddressEntry j, addressEntry) {
qDebug()<<"IP:"<
QTcpServer:监听套接字(创建socket,绑定,监听,创建连接)
QTcpSocket:通讯套接字(建立连接的时候创建,收发数据)
QT += network
#include QTcpServer mserver;
mserver.listen(QHostAddress::Any, ui->lineEdit->text().toUShort());
connect(&mserver, &QTcpServer::newConnection, this, &MainWindow::new_client);
void MainWindow::new_client()
{
QTcpSocket *lsocket = mserver.nextPendingConnection();//连接获取与新连接客户端的通讯套接字
//可以把新连入的套接字存在一个集合里面
}
lsocket->write(“connect OK”);
connect(lsocket, &QTcpSocket::readyRead, this, &MainWindow::read_data);
void MainWindow::read_data()
{
//获取信号发送者
QTcpSocket *msocket = dynamic_cast(sender());
//读取数据
QString msg = msocket->readAll();
//得到发送者的IP
QString ip = (msocket->peerAddress().toString()).remove(0, 7);
ui->textBrowser->setText(ui->textBrowser->toPlainText().append(msg));
}
mserver.close();
QTcpSocket:通讯套接字(建立连接的时候创建,收发数据)
QT += network
#include QTcpSocket msocket;
msocket.connectToHost(ui->lineEdit_IP->text(), ui->lineEdit_Port->text().toUShort());
连接成功会产生一个connected信号
connect(&msocket, &QTcpSocket::readyRead, this, &MainWindow::read_data);
void MainWindow::read_data() { QString msg = msocket.readAll(); ui->textBrowser->append(msg); }
(5)发送数据
msocket.write(ui->textEdit->toPlainText().toUtf8());
#include //存放请求对象的类
#include //接收服务器回发的数据
#include //http通信过程管理类
#include //json的解析器
#include //对象
#include //数组
#include //值
(1)创建套接字——与服务器进行连接
(2)向服务器发送一个请求数据(http头、请求数据)
(3)如果请求合法,服务器就会返回请求的数据
#include
管理整个http通信,负责发送请求,后期应答
请求:get post put
#include
请求对象——请求数据放在这个对象中
#include
当服务器回应的数据通过应答对象获取
QNetworkAccessManager manager;
(2)调用对象的方法,发送请求
QUrl url(ui->lineEdit->text());
//创建一个请求对象
QNetworkRequest request(url);
//发送请求
manager.get(request);
connect(&manaer, &QNetworkAccessManager::finished, this, &MainWindow::read_data);
void MainWindow::read_data(QNetworkReply *reply)
{
QByteArray array = reply->readAll();//读取所有数据
qDebug()<label->setPixmap(map);
}
sudo apt-get install apache2
cd /var/www/html/
原有文件:
index.html
服务器根路径:http://192.168.1.117
(/var/www/html/)
文件路径:http://192.168.1.117/picture/1.jpg
https://blog.csdn.net/weixin_40877388/article/details/86526154
http://www.weather.com.cn/data/cityinfo/101010100.html
——在对象中只能出现键值对 “city”:“北京”(多个键值对,用 , 隔开)
键必须是字符串
键值对的值:
字符串
整数
浮点数
bool
对象
[{}, {}, {}] 对象数组
[“”, “”, “”] 字符串数组
[1, 2, 3]整数数组
#include josn的解析器,实现json的读取写入
#include 对象
#include 数组
#include 值
#include 过程错误检查
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(array, &error);
if(error.error != QJsonParseError::NoError)
{
qDebug()<<"json error";
return;
}
QJsonObject obj = doc.object();
QJsonObject dataobj = obj.value("data").toObject();
QJsonArray forecast_Array = dataobj.value("forecast").toArray();
QJsonObject day = forecast_Array[1].toObject();
QString type = day.value("type").toString();
ui->textBrowser->append("明天重庆的天气类型为:" + type);
/*
QJsonObject obj = doc.object();
QJsonObject cityobj = obj.value("weatherinfo").toObject();//获取对象,这个对象是一个键的值
QString city = cityobj.value("city").toString();
QString htemp = cityobj.value("temp2").toString();
QString ltemp = cityobj.value("temp1").toString();
ui->textBrowser->append("城市:" + city);
ui->textBrowser->append("最高温度:" + htemp);
ui->textBrowser->append("最低温度:" + ltemp);
*/
QT += network
#include
QUdpSocket
QUdpSocket msocket;
msocket.writeDatagram(ui->textEdit->toPlainText().toUtf8(),
QHostAddress(ui->lineEdit_IP->text()),
ui->lineEdit_Port->text().toUShort());
QUdpSocket msocket;
msocket.bind(QHostAddress::Any, ui->lineEdit_port->text().toUShort());
connect(&msocket, &QUdpSocket::readyRead, this, &MainWindow::read_data);
void MainWindow::read_data()
{
char buf[1024] = {};
QHostAddress address;
quint16 port;
msocket.readDatagram(buf, 1024, &address, &port);
ui->textBrowser>setText("【" + address.toString().remove(0, 7) +
"_" + QString::number(port) + "】"+ ":" + buf);
}
msocket.writeDatagram(ui->textEdit->toPlainText().toUtf8(),
QHostAddress("192.168.1.255"),
ui->lineEdit_Port->text().toUShort());
网关地址:192.168.1.1
普通IP:192.168.1.2~254
广播地址:192.168.1.255
组播地址:
224.0.0.0 ~ 239.255.255.255
224.0.0.0 ~ 224.0.0.255:预留的组播地址(永久组地址)
224.0.1.0 ~ 224.0.1.255:公用组播地址
224.0.2.0 ~ 238.255.255.255:临时组地址(用户可用),全网范围有效
239.0.0.0 ~ 239.255.255.255:本地管理组播地址,仅在特定的本地范围内有效
msocket.joinMulticastGroup(QHostAddress(ui->lineEdit_groupIP->text()));
msocket.leaveMulticastGroup(QHostAddress(ui->lineEdit_groupIP->text()));