Qt---QTcpSocket连接http服务器
2016-06-29 20:55
344 查看
tcp.pro
#------------------------------------------------- # # Project created by QtCreator 2016-06-29T20:24:24 # #------------------------------------------------- QT += core network QT -= core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = tcp CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp\ client.cpp HEADERS += \ client.h
client.h
#ifndef CLIENT_H #define CLIENT_H #include <QTcpSocket> class Client: public QObject { Q_OBJECT public: Client(): m_socket(0){} ~Client(){} void startConnect(QString host, quint16 prot); //连接主机 protected slots: void onConnected(); //发送 void onReadyRead(); //读取 private: QTcpSocket *m_socket; }; #endif
client.cpp
#include "client.h" #include <QDebug> void Client::startConnect(QString host, quint16 port) { m_socket = new QTcpSocket(this); connect(m_socket, SIGNAL(connected()), this, SLOT(onConnected())); connect(m_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead())); m_socket->connectToHost(host, port); //连接主机 } void Client::onConnected() { m_socket->write("GET / HTTP/1.1\r\n\r\n"); //向服务器端发送数据,http头部 } void Client::onReadyRead() //接收数据 { qDebug() << m_socket->readAll(); //打印出来 }
main.cpp
#include "mainwindow.h" #include "client.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Client client; client.startConnect("www.baidu.com", 80); return a.exec(); }
相关文章推荐
- Qt学习笔记
- Qt---安装
- Qt下重做右键菜单
- Qt之操作Excel
- RGB32
- 机器人操作系统ROS Indigo 入门学习(9)——理解ROS rqt_console和 roslaunch
- Qt 设置对话框背景(使用调色板,设置它的画刷,画刷可以是图片)
- Qt: qt中获取文件路径和文件名
- QSlider的qss美化
- 关于QProcess的进程中的运行先后测试
- Qt下 QString转char*
- Qt 基本绘图技术
- Qt学习(四)---实例涂鸦画板mspaint
- Qt QLineEdit 仿QQ签名框
- WIN7+Qt5.5.0+qwt-6.1.3环境搭建
- Qt实时显示系统时间
- qt windows分发工具使用(windoployqt)
- qtcreator增加doxygen注释
- QT中代码中与设计器中控件信号与SLOT连接(原来还可以这样连接)
- Win7下qt5.3.1+opencv2.4.9编译环境的搭建(好多 Opencv2.4.9源码分析的博客)