基於Qt網絡編程:
基於tcp協議 c/s模式編程
所需要的類:QTcpServer QTcpSocket
利用qt基於tcp協議編寫c/s模式程序:
兩個類中的信號:
QTcpServer :
newConnection()
QTcpSocket:
readyRead()
connected()
disconnected()
服務器端程序步驟:QTcpServer QTcpSocket
1、.h中聲明一個監聽套接字和通信套接字
.cpp構造函數中:
實例化監聽套接字
2、處於監聽狀態
3、綁定信號onNewConnection()和槽函數
槽函數:獲取通信套接字
4、在onNewConnection()信號槽函數:
(1)接收套接字
(2)readyRead()和槽函數
接收數據
(3)disconnected()和槽函數
關閉通信套接字
5、收/發數據
客戶端程序步驟:QTcpSocket
1、.h中聲明一個通信套接字
.cpp的構造函數中 實例化套接字
2、發出鏈接請求
3、綁定兩個信號和槽函數
connected():鏈接成功后發出信號
槽函數中:獲取鏈接已經成功信息
readyRead():數據接收發出該新號
槽函數:接收數據
disconnected()和槽函數
關閉通信套接字
4、收/發送數據
案例:通過客戶端給服務器發送一個字符串,服務器收到顯示
服務器給客戶端發送一個字符串,客戶端收到后顯示
增加圖形界面,實現聊天功能
客戶端和服務器之間通信,發送任意數據協議包
工程的參考代碼:
服務器端:
widget.h文件內容:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QTcpServer *tcpSever;
QTcpSocket *tcpSocket;
public slots:
void onNewconnection();
void onReadyRead();
void onDisconnect();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QHostAddress>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
tcpSever = new QTcpServer(this);
QHostAddress ipAddr("127.0.0.1");
tcpSever->listen(ipAddr,6565);
connect(tcpSever,SIGNAL(newConnection()),this,SLOT(onNewconnection()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::onNewconnection()
{
tcpSocket = tcpSever->nextPendingConnection();
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(onReadyRead()));
connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(onDisconnect()));
qDebug()<<"new connection";
}
void Widget::onReadyRead()
{
char buf[1024] = {0};
tcpSocket->read(buf,sizeof(buf) - 1);
qDebug()<<buf;
}
void Widget::onDisconnect()
{
tcpSocket->close();
}
main.cpp文件內容:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
客戶端:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpSocket>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QTcpSocket *socketClient;
public slots:
void onConnect();
private slots:
void on_btnSend_clicked();
void on_readyRead();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
socketClient = new QTcpSocket(this);
socketClient->connectToHost("127.0.0.1",6565);
connect(socketClient,SIGNAL(connected()),this,SLOT(onConnect()));
connect(socketClient,SIGNAL(readyRead()),this,SLOT(on_readyRead()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::onConnect()
{
qDebug()<<"connected succesfully";
}
void Widget::on_btnSend_clicked()
{
socketClient->write("hello world");
}
void Widget::on_readyRead()
{
}
main.cpp代碼如下:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}