QT网络编程 客户端和服务端相互通信

标签:
qt网络编程服务端和客户端相互通tcpnetworkit |
分类: QT学习 |
运行的最终界面如下:http://s8/mw690/c0cc1b8bgx6CYpyaRdJd7&690客户端和服务端相互通信" TITLE="QT网络编程
客户端:
1. 添加网络模块,在.pro中添加
QT+=network
2. 在主界面上添加pushbutton和lineEdit控件,跳转到pushbutton的槽函数
3.在.h中定义相应的槽函数和socket变量(注意添加两个头文件
#include
#include
private:
Ui::MainWindow *ui;
QTcpSocket *tcpSocket;
private slots:
void newtcpconnect();
void datatranslate();
void displayerror(QAbstractSocket::SocketError);
void on_pushButton_clicked();
void readdata();
4.在.cpp中添加相应的出来函数,代码如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle(tr("client...."));
this->tcpSocket = new QTcpSocket(this);
connect(tcpSocket,SIGNAL(connected()),this,SLOT(datatranslate()));
connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(displayerror(QAbstractSocket::SocketError)));
connect(tcpSocket,SIGNAL(readyRead()),this,
SLOT(readdata()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::newtcpconnect()
{
tcpSocket->abort();
tcpSocket->connectToHost(QHostAddress("10.159.80.61"),6666);
}
void MainWindow::datatranslate()
{
this->tcpSocket->write(ui->lineEdit->text().toUtf8());
}
void MainWindow::displayerror(QAbstractSocket::SocketError)
{
qDebug()<<tcpSocket->errorString();
tcpSocket->close();
}
void MainWindow::on_pushButton_clicked()
{
if(ui->lineEdit->text().isEmpty())
ui->lineEdit->setFocus();
else
newtcpconnect();
}
void MainWindow::readdata()
{
QString fromClient = tcpSocket->readAll();
ui->label->setText(fromClient.toUtf8());
}
服务端:
1. 添加网络模块
QT+=network
2. 主界面上添加pushbutton和lineEdit两个控件,
pushbutton跳到槽函数
3. 在.h中添加相应的变量和槽函数以及头文件,代码如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QTcpServer *tcpServer;
QTcpSocket *tcpSocket;
private slots:
void readdata();
void start();
void acceptConnection();
void on_pushButton_clicked();
};
#endif // MAINWINDOW_H
4. 在.cpp中完成相应的槽函数代码如下:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle(tr("server..."));
tcpServer =new QTcpServer(this);
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::start()
{
if(!tcpServer->listen(QHostAddress::Any,6666))
{
close();
return;
}
}
void MainWindow::acceptConnection()
{
tcpSocket =tcpServer->nextPendingConnection();
connect(tcpSocket,SIGNAL(readyRead()),this,
SLOT(readdata()));
}
void MainWindow::readdata()
{
QString fromClient = tcpSocket->readAll();
ui->label->setText(fromClient.toUtf8());
}
void MainWindow::on_pushButton_clicked()
{
// tcpSocket =tcpServer->nextPendingConnection();
tcpSocket->write(ui->lineEdit->text().toUtf8());
}
到此就完成了客户端和服务端之间相互通信了,(*^__^*) ……,虽然很简单,缺花了我两天的时间,刚接触QT还有好多不懂,继续学习中。
前一篇:写给自己的留言
后一篇:2013年10月22日