QT下TCP協議通信的Client端

 

  1. /********************** 
  2.  *Write to 5 July 2012* 
  3.  *By Mr Rao           * 
  4.  **********************/ 
  5. #ifndef _CLIENT_H_ 
  6. #define _CLIENT_H_ 
  7.  
  8. #include <QDialog> 
  9. #include <QPushButton> 
  10. #include <QTextEdit> 
  11. #include <QLineEdit> 
  12. #include <QLabel> 
  13. #include <QVBoxLayout> 
  14. #include <QHBoxLayout> 
  15. #include <QTcpSocket> 
  16.  
  17. class Dialog:public QDialog 
  18.     Q_OBJECT 
  19. public
  20.     Dialog(); 
  21.     ~Dialog(); 
  22.  
  23. public slots: 
  24.     void send_slot(); 
  25.     void connect_slot(); 
  26. private
  27.     QLabel *lab; 
  28.     QLabel *lab1; 
  29.     QLineEdit *lineEdit; 
  30.     QLineEdit *lineEditPort; 
  31.     QPushButton *connectButton; 
  32.     QPushButton *sendButton; 
  33.     QPushButton *exitButton; 
  34.     QTextEdit *textEdit; 
  35.     QTcpSocket *tcpSocket; 
  36.     QString strIP; 
  37.     QString strPort; 
  38. }; 
  39. #endif 

 

  1. #include "client.h" 
  2.  
  3. Dialog::Dialog() 
  4.     connectButton = new QPushButton("connect"); 
  5.     sendButton = new QPushButton("send"); 
  6.     exitButton = new QPushButton("exit"); 
  7.     lab = new QLabel(tr("To:")); 
  8.     lab1 = new QLabel(tr("Port:")); 
  9.     lineEdit = new QLineEdit; 
  10.     lineEditPort = new QLineEdit; 
  11.     textEdit = new QTextEdit; 
  12.  
  13.     QHBoxLayout *topLayout = new QHBoxLayout; 
  14.     topLayout->addWidget(lab); 
  15.     topLayout->addWidget(lineEdit); 
  16.     topLayout->addWidget(lab1); 
  17.     topLayout->addWidget(lineEditPort); 
  18.  
  19.     QHBoxLayout *centLayout = new QHBoxLayout; 
  20.     centLayout->addWidget(textEdit); 
  21.  
  22.     QHBoxLayout *lastLayout = new QHBoxLayout; 
  23.     lastLayout->addWidget(connectButton); 
  24.     lastLayout->addWidget(sendButton); 
  25.     lastLayout->addWidget(exitButton); 
  26.       
  27.     QVBoxLayout *mainLayout = new QVBoxLayout; 
  28.     mainLayout->addLayout(topLayout); 
  29.     mainLayout->addLayout(centLayout); 
  30.     mainLayout->addLayout(lastLayout); 
  31.  
  32.     setLayout(mainLayout); 
  33.       
  34.     sendButton->setDisabled(false); 
  35.     tcpSocket = new QTcpSocket; 
  36.       
  37.     connect(connectButton,SIGNAL(clicked()),this,SLOT(connect_slot())); 
  38.     connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot())); 
  39.     connect(exitButton,SIGNAL(clicked()),this,SLOT(close())); 
  40. Dialog::~Dialog() 
  41.     delete lab; 
  42.     delete lineEdit; 
  43.     delete connectButton; 
  44.     delete sendButton; 
  45.     delete exitButton; 
  46.     delete textEdit; 
  47.     delete tcpSocket; 
  48. void Dialog::connect_slot() 
  49.     this->strIP = this->lineEdit->text(); 
  50.     this->strPort = this->lineEditPort->text(); 
  51.     tcpSocket->connectToHost(strIP , this->strPort.toInt(0 , 10)); 
  52. void Dialog::send_slot() 
  53.     QString catstr = textEdit->toPlainText(); 
  54.     tcpSocket->write(qPrintable(catstr)); 

  1. #include <QApplication> 
  2. #include "client.h" 
  3.  
  4. int main(int argc,char **argv) 
  5.     QApplication app(argc,argv); 
  6.     Dialog dlg; 
  7.     dlg.show();  
  8.     return app.exec(); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章