![](https://i-blog.csdnimg.cn/blog_migrate/7d3a15cdc1b49e068b99a242390b2430.png)
cilent端
cilent.pro修改的
![](https://i-blog.csdnimg.cn/blog_migrate/906d326f2d1415423c659a745adfcef8.png)
头文件
widget.cpp
#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();
void InitClientWidget();
private slots:
void OnReadyRead();
void on_connect_clicked();
void on_connectbt_clicked();
void on_sendbt_clicked();
private:
Ui::Widget *ui;
QTcpSocket *m_pSocket;
};
#endif // WIDGET_H
修改的
![](https://i-blog.csdnimg.cn/blog_migrate/3e1f44305e3bdc5a1d46d39210717889.png)
源文件
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.InitClientWidget();
w.show();
return a.exec();
}
修改的
![](https://i-blog.csdnimg.cn/blog_migrate/1ab9c442f37adb4548aaa50423516a93.png)
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include<QHostAddress>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_pSocket=NULL;
}
Widget::~Widget()
{
delete ui;
}
void Widget::InitClientWidget()
{
qDebug()<<"Widget::InitClientWidget start";
if(NULL!=m_pSocket)
{
return;
}
m_pSocket=new QTcpSocket(this);
connect(m_pSocket,SIGNAL(readyRead()),this,SLOT(OnReadyRead()));
qDebug()<<"Widget::InitClientWidget end";
}
void Widget::OnReadyRead()
{
qDebug()<<"Widget::OnReadyRead start";
QByteArray arr=m_pSocket->readAll();
qDebug()<<arr;
ui->receivedata->setText(QString(arr));
qDebug()<<"Widget::OnReadyRead end";
}
void Widget::on_connectbt_clicked()
{
qDebug()<<"Widget::on_connectbt_clicked start";
if(NULL==m_pSocket)
{
return;
}
m_pSocket->connectToHost(QHostAddress("127.0.0.1"),8888);
if(m_pSocket->waitForConnected(1000))
{
qDebug()<<"connect ok";
}
qDebug()<<"Widget::on_connectbt_clicked end";
}
void Widget::on_sendbt_clicked()
{
qDebug()<<"Widget::on_sendbt_clicked start";
QString str=ui->lineEdit->text();
if(str.length()<=0)
{
return;
}
m_pSocket->write(str.toStdString().data());
ui->receivedata->setText(str.toStdString().data());
qDebug()<<"Widget::on_sendbt_clicked end";
}
修改的
![](https://i-blog.csdnimg.cn/blog_migrate/4afb67ac2b755a86f0066b9c0bc5eaa3.png)
界面文件
widget.ui
![](https://i-blog.csdnimg.cn/blog_migrate/0b4a4f9fde62263d50e7316ef85811e1.png)
server端
server.pro修改的
![](https://i-blog.csdnimg.cn/blog_migrate/d201dc9339619d248ed87f4745d2df97.png)
头文件
widget.cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QTcpServer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void InitWidget();
private slots:
void OnNewConnection();
void OnReadReady();
void on_pushButton_clicked();
private:
Ui::Widget *ui;
QTcpServer *m_pServer;//m member
};
#endif // WIDGET_H
修改的
![](https://i-blog.csdnimg.cn/blog_migrate/cef7adea1c1ca01e3fc2be397bd37560.png)
源文件
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.InitWidget();
w.show();
return a.exec();
}
修改的
![](https://i-blog.csdnimg.cn/blog_migrate/dfe9683461b29ee4f2b8c55c0b87183c.png)
widget.cpp
#include<QDebug>
#include<QHostAddress>
#include<QTcpSocket>
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_pServer=NULL;
}
Widget::~Widget()
{
delete ui;
}
void Widget::InitWidget()
{
qDebug()<<"void Widget::InitWidget() start";
if(NULL !=m_pServer)
{
return;
}
m_pServer=new QTcpServer(this);
connect(m_pServer,SIGNAL(newConnection()),this,SLOT(OnNewConnection()));
qDebug()<<"void Widget::InitWidget() end";
}
void Widget::OnNewConnection()
{
qDebug()<<"Widget::OnNewConnection start";
QTcpSocket *pSocket=m_pServer->nextPendingConnection();
connect(pSocket,SIGNAL(readyRead()),this,SLOT(OnReadReady()));
// ui->lineEdit_2->setText(m_pServer);
qDebug()<<"Widget::OnNewConnection end";
}
void Widget::OnReadReady()
{
qDebug()<<"Widget::OnReadReady start";
QTcpSocket *pSocket=(QTcpSocket *) sender();
QByteArray arr=pSocket->readAll();
qDebug()<< pSocket<<" "<<arr;
QString str="send data to client";
ui->lineEdit_2->setText(arr);//显示
qDebug()<<"Widget::OnReadReady end";
}
void Widget::on_pushButton_clicked()
{
qDebug()<<"Widget::on_pushButton_clicked start";
QString strIP=ui->edit_ip->text();
QString strPort=ui->edit_port->text();
m_pServer->listen(QHostAddress(strIP),strPort.toShort());
qDebug()<<"Widget::on_pushButton_clicked end";
}
修改的
![](https://i-blog.csdnimg.cn/blog_migrate/ec121474167b83dc715eb564ef6a4b22.png)
界面文件
widget.ui
![](https://i-blog.csdnimg.cn/blog_migrate/7926f3b897a5ba334dc8117e5f89f84e.png)