QTcpSocket

在QTcpSocket中的tcp通信,发送数据,接收数据都可以是QTcpSocket套接字的完成,包括server端,QTcpServer的功能仅仅是作为一个服务器的存在,它只是用来判断是不是有设备连接,连到以后的数据收发,还是用的QTcpSocket;

客户端:

class Client : public QWidget

{

    Q_OBJECT 

public:

    explicit Client(QWidget *parent = 0);

    ~Client();

   public slots:

    void onReadyRead();//有数据接收触发

    void onConnected();//连接成功触发

    void onDisconnected();//断开连接触发

    void on_pushButton_clicked();//连接IP

    void on_pushButton_2_clicked();//发送数据

    void on_pushButton_3_clicked();//断开连接

private:

    Ui::Client *ui;

    QTcpSocket*m_TcpSocket;

};

 

Client::Client(QWidget *parent) :

    QWidget(parent),

    ui(new Ui::Client){

    ui->setupUi(this);

    m_TcpSocket=new QTcpSocket;

    //当socket上有新数据可读时,自动触发

    connect(m_TcpSocket,SIGNAL(readyRead()),this,SLOT(onReadyRead()));

    connect(m_TcpSocket,SIGNAL(connected()),this,SLOT(onConnected()));//连接成功触发

    connect(m_TcpSocket,SIGNAL(disconnected()),this,SLOT(onDisconnected()));//断开连接触发

}

 

void Client::onReadyRead(){

    QByteArray ba=m_TcpSocket->readAll();//读取所有数据

    ui->lineEdit_3->setText(ba.data());

}

 

void Client::onConnected(){

    qDebug()<<"连接成功";

}

 

void Client::onDisconnected(){

    qDebug()<<"断开连接";

}

void Client::on_pushButton_clicked(){

    QString IPstr=ui->lineEdit->text();

    m_TcpSocket->connectToHost(IPstr,5555);//连接IP

    qDebug()<<"尝试连接IP";

    m_TcpSocket->waitForConnected();//等待固定时长来连接

   

}

 

void Client::on_pushButton_2_clicked(){

    QString str=ui->lineEdit_2->text();

    m_TcpSocket->write(str.toLatin1());

    qDebug()<<"发送数据";

}

 

void Client::on_pushButton_3_clicked(){

    m_TcpSocket->disconnectFromHost();//断开连接

}

 

服务器:

class MainWindow : public QMainWindow

{

    Q_OBJECT

public:

    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();

public slots:

    void onNewConnection();//收到请求连接信号触发

    void onReadMessage();//读取信息触发

private slots:

    void on_pushButton_clicked();//发送数据

    void on_pushButton_2_clicked();//断开连接

private:

    Ui::MainWindow *ui;

    QTcpServer*m_TcpServer;

    QTcpSocket*m_TcpClient;

};

MainWindow::MainWindow(QWidget *parent) :

    QMainWindow(parent),

    ui(new Ui::MainWindow)

{

    ui->setupUi(this);

    m_TcpServer=new QTcpServer;

    m_TcpServer->listen(QHostAddress::Any,5555);

    //新连接信号触发

    connect(m_TcpServer,SIGNAL(newConnection()),this,SLOT(onNewConnection()));

    m_TcpClient=m_TcpServer->nextPendingConnection();//获取连接进来的socket

    connect(m_TcpClient,SIGNAL(readyRead()),this,SLOT(onReadMessage()));

    connect(m_TcpClient,SIGNAL(connected()),this,SLOT(onReadMessage()));

 

    move(100,100);//设置窗口初始显示相对屏幕的位置

    Client*client=new Client;

    client->move(1000,100); //设置窗口初始显示相对屏幕的位置

    client->show();

}

void MainWindow::onNewConnection(){

    qDebug()<<"收到请求连接信号";

    m_TcpClient=m_TcpServer->nextPendingConnection();//得到连接进来的Socket

    //有可读的信息,触发读取信息的槽函数

    connect(m_TcpClient,SIGNAL(readyRead()),this,SLOT(onReadMessage()));

    QString ipStr=m_TcpClient->peerAddress().toString();//获取对方的IP

}

void MainWindow::onReadMessage(){

    qDebug()<<"读取信息";

    QByteArray ba=m_TcpClient->readAll();//接收数据

    ui->lineEdit->setText(ba.data());

}

void MainWindow::on_pushButton_clicked(){

    QString str=ui->lineEdit->text();

    m_TcpClient->write(str.toLatin1());//发送数据

}

void MainWindow::on_pushButton_2_clicked(){

    m_TcpClient->disconnectFromHost();//断开连接

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值