QT: A simple program for TCP attachment and qttcp
TCP client interface:
1 # include "mainwindow. h "2 # include" ui_main1_1_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; // clear 14} 15 16 MainWindow: ~ before use ::~ 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 (q1_actsocket:: 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; // temporarily store the data we need to send 45 QDataStream out (& block, QIODevice: WriteOnly); // TCP must use 46 out with the data stream. setVe Rsion (QDataStream: Qt_5_7); // set the data stream version (the server and host versions must be the same) 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; // delete 63 directly if it points to another space TcpSocket = new QTcpSocket (this); // The requested heap space contains the TCP sending and receiving operations 64 tcpIp = ui-> IPLineEdit-> text (); 65 tcpPort = ui-> portLineEdit-> text (); 66 if (tcpIp = NULL | tcpPort = NULL) // determine whether the IP address and PORT are empty. 67 {68 QMessageBox msgBox; 69 msgBox. setText ("IP or PORT is Empty"); 70 msgBox.exe c (); 71 return; 72} 73 tcpSocket-> connectToHost (tcpIp, tcpPort. toInt (); // connect to host 74 connect (tcpSocket, SIGNAL (error (q1_actsocket: SocketError ), This, 75 SLOT (displayError (q1_actsocket: SocketError); // connect 76 connect (tcpSocket, SIGNAL (connected (), this, 77 SLOT (connectUpdata (); // The updated connection button enables 78 connect (tcpSocket, SIGNAL (readyRead (), this, 79 SLOT (readMassage (); // connection for reading information 80 ui-> connnectButton-> setEnabled (false); 81 ui-> disconnectButton-> setEnabled (true ); 82 83} 84 85 void MainWindow: on_disconnectButton_clicked () 86 {87 tcpSock Et-> 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.exe c (); 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 QMainWindow13 {14 Q_OBJECT15 16 public: 17 explicit MainWindow (QWidget * parent = 0); 18 ~ MainWindow (); 19 private slots: 20 void sendMassage (); 21 void readMassage (); 22 void displayError (q1_actsocket: SocketError); 23 void connectUpdata (); 24 void disconnectUpdata (); 25 void on_sendButton_clicked (); 26 void on_clearButton_clicked (); 27 void SKIP (); 28 void on_disconnectButton_clicked (); 29 30 private: 31 // QTcpServer * tcpServer; // you do not need to create a server class. directly create the following socket 32 QTcpSocket * tcpSocket; // directly create a TCP socket Class 33 QString tcpIp; // storage IP address 34 QString tcpPort; // storage port address 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 += widgets10 11 TARGET = TCP_Server12 TEMPLATE = app13 14 # The following define makes your compiler emit warnings if you use15 # any feature of Qt which as been marked as deprecated (the exact warnings16 # depend on your compiler). Please consult the documentation of the17 # deprecated API in order to know how to port your code away from it.18 DEFINES += QT_DEPRECATED_WARNINGS19 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.024 25 26 SOURCES += main.cpp\27 mainwindow.cpp28 29 HEADERS += mainwindow.h30 31 FORMS += mainwindow.ui