window的QT作为TCP客户端,ubuntu的python作为TCP服务端

客户端

*.pro文件加入

QT       += network

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:

    void on_pushButton_Connect_clicked();

    void on_pushButton_Send_clicked();

    void socket_Read_Data();

//    void socket_Disconnected();

private:
    Ui::MainWindow *ui;
    QTcpSocket *socket;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QPushButton"
#include "QTime"
#include "QTextCodec"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    resize(1000,800);
    socket = new QTcpSocket();

    //连接信号槽
    QObject::connect(socket, &QTcpSocket::readyRead, this, &MainWindow::socket_Read_Data);
    connect(ui->pushButtondelete,&QPushButton::clicked,this,[=](){

        socket->disconnectFromHost();
        ui->pushButton_Connect->setText("连接");
        ui->pushButton_Send->setEnabled(false);
        qDebug() << "Disconnected!";
    });
    connect(ui->pushButtonclear,&QPushButton::clicked,this,[=](){

        ui->textEdit_Send->setPlainText("");
        ui->textEdit_Recv->setPlainText("");
    });
    ui->pushButton_Send->setEnabled(false);
    ui->lineEdit_IP->setText("192.168.1.103");
    ui->lineEdit_Port->setText("1234");

}

MainWindow::~MainWindow()
{
    delete this->socket;
    delete ui;
}

void MainWindow::on_pushButton_Connect_clicked()
{
    if(ui->pushButton_Connect->text() == tr("连接"))
    {
        QString IP;
        int port;

        //获取IP地址
        IP = ui->lineEdit_IP->text();
        //获取端口号
        port = ui->lineEdit_Port->text().toInt();

        //取消已有的连接
        socket->abort();
        //连接服务器
        socket->connectToHost(IP, port);

        //等待连接成功
        if(!socket->waitForConnected(30000))
        {
            qDebug() << "Connection failed!";
            return;
        }
        qDebug() << "Connect successfully!";

        //发送按键使能
        ui->pushButton_Send->setEnabled(true);

    }

}

void MainWindow::on_pushButton_Send_clicked()
{
    qDebug() << "Send: " << ui->textEdit_Send->toPlainText();
     //获取文本框内容并以ASCII码形式发送
    QTextCodec *c = QTextCodec::codecForName("UTF-8");
    QTextEncoder* codec = c->makeEncoder(QTextCodec::IgnoreHeader);
    QByteArray sendData = codec->fromUnicode(QString(ui->textEdit_Send->toPlainText()));

    socket->write(sendData);
    socket->flush();
}

void MainWindow::socket_Read_Data()
{
    QByteArray buffer;
    QTime nowtime = QDateTime::currentDateTime().time();
    //读取缓冲区数据
    buffer = socket->readAll();
    if(!buffer.isEmpty())
    {
        QString str = ui->textEdit_Recv->toPlainText();
        str+="[";
        str+=nowtime.toString("hh:mm:ss");
        str+="]192.168.1.103:";
        str+=trUtf8(buffer);
        str+='\n';
        //刷新显示
        ui->textEdit_Recv->setText(str);
    }
}


main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

ui界面

window的QT作为TCP客户端,ubuntu的python作为TCP服务端_第1张图片

window的QT作为TCP客户端,ubuntu的python作为TCP服务端_第2张图片

服务端

# coding=utf-8
import socket  # socket模块
# import commands  # 执行系统命令模块
if __name__ == "__main__":

    HOST = '192.168.1.103'
    PORT = 1234
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 定义socket类型,网络通信,TCP
    s.bind((HOST, PORT))  # 套接字绑定的IP与端口
    s.listen(1)  # 开始TCP监听
    while 1:
        conn, addr = s.accept()  # 接受TCP连接,并返回新的套接字与IP地址
        print('Connected by', addr)  # 输出客户端的IP地址
        print("等待消息")
        while 1:
                  
            data = conn.recv(100)  # 把接收的数据实例化
            if data:
                print(">>>",data.decode())
                conn.sendall(data)
            else:
                conn, addr = s.accept()
    conn.close()  # 关闭连接

你可能感兴趣的:(qt,tcp/ip,ubuntu)