QT編寫簡單的TCP附源程序。本站提示廣大學習愛好者:(QT編寫簡單的TCP附源程序)文章只能為提供參考,不一定能成為您想要的結果。以下是QT編寫簡單的TCP附源程序正文
TCP客戶端界面:
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) : 5 QMainWindow(parent), 6 ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 ui->sendButton->setEnabled(false); 10 ui->disconnectButton->setEnabled(false); 11 ui->IPLineEdit->setText("192.168.3.4"); 12 ui->portLineEdit->setText("4001"); 13 tcpSocket = NULL;//使用前先清空 14 } 15 16 MainWindow::~MainWindow() 17 { 18 delete tcpSocket; 19 delete ui; 20 } 21 22 void MainWindow::sendMassage(){} 23 24 void MainWindow::readMassage() 25 { 26 QByteArray data=tcpSocket->readAll(); 27 ui->clearLineEdit->setText(QString(data)); 28 } 29 30 void MainWindow::displayError(QAbstractSocket::SocketError) 31 { 32 QMessageBox::warning (this, tr("Warnning"), tcpSocket->errorString ()); 33 tcpSocket->close (); 34 ui->connnectButton->setEnabled (true); 35 ui->disconnectButton->setEnabled (false); 36 ui->sendButton->setEnabled (false); 37 } 38 39 void MainWindow::on_sendButton_clicked() 40 { 41 QString sendmessage; 42 sendmessage = ui->sendLineEdit->text(); 43 /* if(sendmessage == NULL) return; 44 QByteArray block;//暫時存儲我們需要發送的數據 45 QDataStream out(&block,QIODevice::WriteOnly);//TCP必須和數據流一起使用 46 out.setVersion(QDataStream::Qt_5_7);//設置數據流的版本(服務器和主機版本一定相同) 47 out << sendmessage; 48 tcpSocket->write(block);*/ 49 QByteArray data; 50 data.append(sendmessage); 51 tcpSocket->write(data); 52 } 53 54 void MainWindow::on_clearButton_clicked() 55 { 56 ui->clearLineEdit->clear(); 57 } 58 59 void MainWindow::on_connnectButton_clicked() 60 { 61 flag = false; 62 if(tcpSocket) delete tcpSocket;//如果有指向其他空間直接刪除 63 tcpSocket = new QTcpSocket(this);//申請堆空間有TCP發送和接受操作 64 tcpIp = ui->IPLineEdit->text(); 65 tcpPort = ui->portLineEdit->text(); 66 if(tcpIp==NULL||tcpPort==NULL)//判斷IP和PORT是否為空 67 { 68 QMessageBox msgBox; 69 msgBox.setText("IP or PORT is Empty"); 70 msgBox.exec(); 71 return; 72 } 73 tcpSocket->connectToHost(tcpIp,tcpPort.toInt());//連接主機 74 connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this, 75 SLOT(displayError(QAbstractSocket::SocketError)));//錯誤連接 76 connect(tcpSocket,SIGNAL(connected()),this, 77 SLOT(connectUpdata()));//更新連接之後按鈕的使能 78 connect(tcpSocket,SIGNAL(readyRead()),this, 79 SLOT(readMassage()));//讀取信息的連接 80 ui->connnectButton->setEnabled (false); 81 ui->disconnectButton->setEnabled (true); 82 83 } 84 85 void MainWindow::on_disconnectButton_clicked() 86 { 87 tcpSocket->abort(); 88 delete tcpSocket; 89 tcpSocket=NULL; 90 disconnectUpdata(); 91 } 92 93 void MainWindow::connectUpdata() 94 { 95 if(!flag) 96 { 97 QMessageBox msgBox; 98 msgBox.setText("TCP connect successful"); 99 msgBox.exec(); 100 ui->connnectButton->setEnabled(false); 101 ui->sendButton->setEnabled(true); 102 ui->disconnectButton->setEnabled(true); 103 ui->IPLineEdit->setEnabled(false); 104 ui->portLineEdit->setEnabled(false); 105 } 106 flag=true; 107 } 108 109 void MainWindow::disconnectUpdata() 110 { 111 ui->connnectButton->setEnabled(true); 112 ui->sendButton->setEnabled(false); 113 ui->disconnectButton->setEnabled(false); 114 ui->IPLineEdit->setEnabled(true); 115 ui->portLineEdit->setEnabled(true); 116 }
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QTcpServer> 6 #include <QTcpSocket> 7 #include <QMessageBox> 8 namespace Ui { 9 class MainWindow; 10 } 11 12 class MainWindow : public QMainWindow 13 { 14 Q_OBJECT 15 16 public: 17 explicit MainWindow(QWidget *parent = 0); 18 ~MainWindow(); 19 private slots: 20 void sendMassage(); 21 void readMassage(); 22 void displayError(QAbstractSocket::SocketError); 23 void connectUpdata(); 24 void disconnectUpdata(); 25 void on_sendButton_clicked(); 26 void on_clearButton_clicked(); 27 void on_connnectButton_clicked(); 28 void on_disconnectButton_clicked(); 29 30 private: 31 //QTcpServer *tcpServer;//不用再建立服務器類了,直接建立下面的套接字 32 QTcpSocket *tcpSocket;//直接建立TCP套接字類 33 QString tcpIp;//存儲IP地址 34 QString tcpPort;//存儲端口地址 35 bool flag; 36 Ui::MainWindow *ui; 37 38 }; 39 40 #endif // MAINWINDOW_H
1 #------------------------------------------------- 2 # 3 # Project created by QtCreator 2017-02-23T10:33:02 4 # 5 #------------------------------------------------- 6 7 QT += core gui 8 QT += network 9 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 11 TARGET = TCP_Server 12 TEMPLATE = app 13 14 # The following define makes your compiler emit warnings if you use 15 # any feature of Qt which as been marked as deprecated (the exact warnings 16 # depend on your compiler). Please consult the documentation of the 17 # deprecated API in order to know how to port your code away from it. 18 DEFINES += QT_DEPRECATED_WARNINGS 19 20 # You can also make your code fail to compile if you use deprecated APIs. 21 # In order to do so, uncomment the following line. 22 # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 25 26 SOURCES += main.cpp\ 27 mainwindow.cpp 28 29 HEADERS += mainwindow.h 30 31 FORMS += mainwindow.ui