1.TCP Socket
1.核心API概览
- 核⼼类是两个:
QTcpServer
和QTcpSocket
QTcpServer
用于监听端口,和获取客户端连接
listen(const QHostAddress&, quint16 port)
:绑定指定的地址和端⼝号,并开始监听
- 成员方法
- 对标原⽣API:
bind()
和listen()
nextPendingConnection()
:从系统中获取到⼀个已经建⽴好的tcp连接
- 返回⼀个
QTcpSocket
,表⽰这个客⼾端的连接
- 成员方法
- 对标原⽣API:
accept()
newConnection()
:有新的客⼾端建⽴连接好之后触发
QTcpSocket
⽤于客⼾端和服务器之间的数据交互
readAll()
:读取当前接收缓冲区中的所有数据,返回QByteArray
对象
write(const QByteArray &)
:把数据写⼊socket中
deleteLater()
:暂时把socket对象标记为⽆效,Qt会在下个事件循环中析构释放该对象
readyRead()
:有数据到达并准备就绪时触发
disconnected()
:连接断开时触发
2.回显服务器
{
server = new QTcpServer(this);
connect(server, &QTcpServer::newConnection,
this, &Widget::ProcessConnection);
if (!ret = server->listen(QHostAddress::Any, 2333))
{
QMessageBox::critical(this, "服务器启动失败!", server->errorString());
exit(1);
}
}
void Widget::ProcessConnection()
{
QTcpSocket* clientSocket = tcpServer->nextPendingConnection();
QString log = "[" + clientSocket->peerAddress().toString() + ":" +
QString::number(clientSocket->peerPort()) + "] 客户端上线!";
ui->listWidget->addItem(log);
connect(clientSocket, &QTcpSocket::readyRead, this, [=]() {
QString request = clientSocket->readAll();
const QString& response = Process(request);
clientSocket->write(response.toUtf8());
QString log = "[" + clientSocket->peerAddress().toString() + ":" +
QString::number(clientSocket->peerPort()) + "] "
+ " req: " + request + ", resp: " + response;
ui->listWidget->addItem(log);
});
connect(clientSocket, &QTcpSocket::disconnected, this, [=]() {
QString log = "[" + clientSocket->peerAddress().toString() + ":" +
QString::number(clientSocket->peerPort()) + "] 客户端下线!";
ui->listWidget->addItem(log);
clientSocket->deleteLater();
});
}
QString Widget::Process(const QString request)
{
return request;
}
3.回显客户端
{
socket = new QTcpSocket(this);
socket->connectToHost("127.0.0.1", 2333);
connect(socket, &QTcpSocket::readyRead, this, [=]() {
QString response = socket->readAll();
ui->listWidget->addItem("服务器说: " + response);
});
if (!socket->waitForConnected())
{
QMessageBox::critical(this, "连接服务器出错", socket->errorString());
exit(1);
}
}
void Widget::on_pushButton_clicked()
{
const QString& text = ui->lineEdit->text();
socket->write(text.toUtf8());
ui->listWidget->addItem("客户端说: " + text);
ui->lineEdit->setText("");
}
2.HTTP Client
- Qt只提供了Http客户端,并没有提供Http服务端的库
- 关键类主要是三个:
QNetworkAccessManager
, QNetworkRequest
, QNetworkReply
QNetworkAccessManager
提供了HTTP的核⼼操作
get(const QNetworkRequest&)
:发起⼀个HTTP GET请求,返回QNetworkReply
对象
- 本身不是阻塞函数,只负责发出去请求,不负责等请求回来
- 收是
QNetworkReply
负责的,会收到finished
信号
post(const QNetworkRequest&, const QByteArray&)
:发起⼀个HTTP POST请求,返回QNetworkReply
对象
QNetworkRequest
表⽰⼀个HTTP请求(不含body),如果需要发送⼀个带有body的请求(⽐如post
),会在QNetworkAccessManager
的post()
中通过单独的参数来传⼊body
QNetworkRequest(const QUrl&)
:通过URL构造⼀个HTTP请求setHeader(QNetworkRequest::KnownHeaders header, const QVariant& value)
:设置请求头
QNetworkRequest::KnownHeaders
是⼀个枚举类型,常⽤取值
ContentTypeHeader
:描述body的类型ContentLengthHeader
:描述body的⻓度LocationHeader
:⽤于重定向报⽂中指定重定向地址CookieHeader
:设置cookieUserAgentHeader
:设置User-Agent
QNetworkReply
表⽰⼀个HTTP响应,这个类同时也是QIODevice
的⼦类
error()
:获取出错状态errorString()
:获取出错原因的⽂本readAll()
:读取响应bodyheader(QNetworkRequest::KnownHeaders header)
:读取响应指定header的值finished()
:为一个信号,在客⼾端收到完整的响应数据之后触发
- 示例:
{
manager = new QNetworkAccessManager(this);
}
void Widget::on_pushButton_clicked()
{
QUrl url(ui->lineEdit->text());
QNetworkRequest request(url);
QNetworkReply* response = manager->get(request);
connect(response, &QNetworkReply::finished, this, [=]() {
if (response->error() == QNetworkReply::NoError)
{
QString html = response->readAll();
ui->plainTextEdit->setPlainText(html);
}
else
{
ui->plainTextEdit->setPlainText(response->errorString());
}
response->deleteLater();
});
}
3.其他模块
- Qt中还提供了FTP,DNS,SSL等⽹络相关的组件⼯具,有需要可以具体学习,查阅官方文档