- /**********************
- *Write to 5 July 2012*
- *By Mr Rao *
- **********************/
- #ifndef _SERVER_H_
- #define _SERVER_H_
- #include <QDialog>
- #include <QPushButton>
- #include <QTextEdit>
- #include <QGridLayout>
- #include <QTcpSocket>
- #include <QTcpServer>
- class Dialog:public QDialog
- {
- Q_OBJECT
- public:
- Dialog();
- ~Dialog();
- public slots:
- void recv_slot();
- void connect_slot();
- private:
- QPushButton *exitButton;
- QTextEdit *textEdit;
- QGridLayout *layout;
- QTcpServer *tcpServer;
- QTcpSocket *tcpSocket;
- };
- #endif
- #include "server.h"
- Dialog::Dialog()
- {
- exitButton = new QPushButton("exit");
- textEdit = new QTextEdit;
- layout = new QGridLayout;
- tcpServer = new QTcpServer;
- layout->addWidget(textEdit,0,0,4,6);
- layout->addWidget(exitButton,5,4,1,2);
- setLayout(layout);
- tcpServer->listen(QHostAddress::Any , 1573);
- connect(tcpServer,SIGNAL(newConnection()),this,SLOT(connect_slot()));
- connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
- }
- Dialog::~Dialog()
- {
- delete exitButton;
- delete textEdit;
- delete layout;
- delete tcpServer;
- }
- void Dialog::connect_slot()
- {
- tcpSocket = tcpServer->nextPendingConnection();
- connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));
- }
- void Dialog::recv_slot()
- {
- QByteArray byte;
- byte = tcpSocket->readAll();
- textEdit->setPlainText(QString(byte));
- }
- #include <QApplication>
- #include "server.h"
- int main(int argc,char **argv)
- {
- QApplication app(argc,argv);
- Dialog dlg;
- dlg.show();
- return app.exec();
- }
转载于:https://blog.51cto.com/mjrao/1008047