service network restart
#include <QtGui/QApplication>#include "myserver.h"int main(int argc, char *argv[]){ QApplication app(argc, argv); MyServer server(2323); server.show(); return app.exec();}
#ifndef MYSERVER_H#define MYSERVER_H#include <QWidget>#include <QtGui/QMainWindow>//#include "ui_myserver.h"#include <QtNetwork>#include <QtXml>class QTcpServer;class MyServer : public QWidget{ Q_OBJECTprivate: QTcpServer* m_ptcpServer; QPushButton* pushButton_1; qint16 m_nNextBlockSize;private: void sendToClient(QTcpSocket* pSocket, const QString& str); void copyf(const QString& str1,const QString& str2); public: MyServer(int nPort, QWidget* pwgt =0);public slots: virtual void slotNewConnection(); void slotReadClient(); void slotpushButton_1( );};#endif // MYSERVER_H
#include "myserver.h"//#include "QTcpServer"#include <QWidget>#include <QtGui>#include <QtNetwork>#include <QMessageBox>#include <QTextEdit>#include <QPushButton>//#include "ui_myserver.h"#include <QtXml>QTcpSocket* pClientSocket;MyServer::MyServer(int nPort, QWidget* pwgt /*=0*/) : QWidget(pwgt),m_nNextBlockSize(0){ pushButton_1 = new QPushButton(QObject::tr("data"), this); connect(PushButton_1,SIGNAL(clicked()),this,SLOT(slotPushButton_1())); QGridLayout * pLArch=new QGridLayout(this); pLArch->addWidget(pushButton_1,0,0,1,1); int nPorta=2323; m_ptcpServer = new QTcpServer(this); if (!m_ptcpServer->listen(QHostAddress::Any,nPorta)) { QMessageBox::critical(0, "server error", "Unable to start the server:"+ m_ptcpServer->errorString()); m_ptcpServer->close(); return; } connect(m_ptcpServer, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));}//==================================================================== void MyServer::slotpushButton_1() {// выдача сообщения клиенту QString ss=""; //далее считываю в ss = млн.байт QTextStream out(pClientSocket); out<<ss; }//=========================================================================== void MyServer::slotNewConnection(){ iclientov++;// QTcpSocket* pClientSocket =m_ptcpServer->nextPendingConnection(); connect(pClientSocket,SIGNAL(disconnected()), pClientSocket,SLOT(deleteLater())); connect(pClientSocket,SIGNAL(readyRead()), this,SLOT(slotReadClient()));}//================================== приём сообщений от клиентаvoid MyServer::slotReadClient(){ QFile file("a.xml"); // файл приёма if (file.open(QIODevice::Append)){ QByteArray arrBlock; QTextStream in(pClientSocket); QString str=in.readAll(); arrBlock=str.toAscii () ; file.write(arrBlock); arrBlock.clear (); file.close(); } }
#include <QtGui/QApplication>#include "ServOtl.h"int main(int argc, char *argv[]){ QApplication app(argc, argv); ServOtl server(2323); server.show(); return app.exec();}
#ifndef ServOtl_H#define ServOtl_H#include <QWidget>#include <QtGui/QMainWindow>//#include "ui_myserver.h"#include <QtNetwork>#include <QtXml>class QTcpServer;class QTcpSocket;class QPushButton;class QTextEdit;class QLineEdit;class ServOtl : public QWidget{ Q_OBJECTprivate: QTcpServer* m_ptcpServer; QPushButton* pushButton_1; QTextEdit* m_ptxtInfo; QLineEdit* m_ptxtInput; qint16 m_nNextBlockSize; QTextEdit* m_ptxt1; QTextEdit* m_ptxt2;private: void sendToClient(QTcpSocket* pSocket, const QString& str); void copyf(const QString& str1,const QString& str2); public: ServOtl(int nPort, QWidget* pwgt =0);public slots: virtual void slotNewConnection(); void slotReadClient(); void slotPushButton_1( );};#endif
#include "ServOtl.h"//#include "QTcpServer"#include <QWidget>#include <QtGui>#include <QtNetwork>#include <QMessageBox>#include <QTextEdit>#include <QPushButton>#include <QMessageBox>#include <QtXml>QTcpSocket* pClientSocket;ServOtl::ServOtl(int nPort, QWidget* pwgt /*=0*/) : QWidget(pwgt),m_nNextBlockSize(0){ ServOtl::setGeometry(QRect(20, 20, 510, 500)); pushButton_1 = new QPushButton(QObject::tr("data"), this); connect(pushButton_1,SIGNAL(clicked()),this,SLOT(slotPushButton_1())); m_ptxtInfo= new QTextEdit; m_ptxtInput=new QLineEdit; m_ptxt1= new QTextEdit; m_ptxt1->setReadOnly(true); m_ptxt2= new QTextEdit; m_ptxt2->setReadOnly(true); m_ptxtInfo->setReadOnly(true); QGridLayout * pLArch=new QGridLayout(this); pLArch->addWidget (new QLabel((QApplication::translate("MyServerClass", "\320\277\321\200\320\276\321\202\320\276\320\272\320\276\320\273", 0, QApplication::UnicodeUTF8)) ),0,0,1,1); pLArch->addWidget (new QLabel((QApplication::translate("MyServerClass", "\320\276\320\272\320\275\320\276 \320\277\321\200\320\270\320\275\321\217\321\202\321\213\321\205 \321\201\320\276\320\276\320\261\321\211\320\265\320\275\320\270\320\271", 0, QApplication::UnicodeUTF8)) ),0,1,1,1); pLArch->addWidget(m_ptxt1,1,0,1,1); pLArch->addWidget(m_ptxt2,1,1,1,1); pLArch->addWidget(pushButton_1,2,0,1,1); int nPorta=2323; m_ptcpServer = new QTcpServer(this); if (!m_ptcpServer->listen(QHostAddress::Any,nPorta)) { QMessageBox::critical(0, "server error", "Unable to start the server:"+ m_ptcpServer->errorString()); m_ptcpServer->close(); return; } connect(m_ptcpServer, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));}//==================================================================== void ServOtl::slotPushButton_1() {// выдача сообщения клиенту QString ss="1"; //далее считываю в ss = млн.байт QFile file("d.dat"); // файл приёма if (file.open(QIODevice::ReadOnly)){ QTextStream out(pClientSocket); ss+=file.readAll(); out<<ss; file.close(); } else{ QMessageBox::about(this,"error","file open"); } }//=========================================================================== void ServOtl::slotNewConnection(){// QTcpSocket*// QMessageBox::about(this,"new Connection","slotNewConnection"); pClientSocket =m_ptcpServer->nextPendingConnection(); connect(pClientSocket,SIGNAL(disconnected()), pClientSocket,SLOT(deleteLater())); connect(pClientSocket,SIGNAL(readyRead()), this,SLOT(slotReadClient())); QTime time=QTime::currentTime (); m_ptxt2->append(time.toString()+" "+ "serv << client : "+ (QApplication::translate("MyClientClass","\321\201\320\276\320\265\320\264\320\270\320\275\320\265\320\275\320\270\320\265 \321\203\321\201\321\202\320\260\320\275\320\276\320\262\320\273\320\265\320\275\320\276", 0, QApplication::UnicodeUTF8)));// iclientov=1;}//================================== приём сообщений от клиентаvoid ServOtl::slotReadClient(){ QFile file("a.xml"); // файл приёма if (file.open(QIODevice::Append)){ QByteArray arrBlock; QTextStream in(pClientSocket); QString str=in.readAll(); arrBlock=str.toAscii () ; file.write(arrBlock); arrBlock.clear (); file.close(); QTime time; if(str.length()>100){str=str.left(10)+"...";} QString strMessage= time.toString()+" "+ "serv << client :" + str;// m_ptxtInfo->append(strMessage); m_ptxt2->append(strMessage); } }
TEMPLATE = appTARGET = DEPENDPATH += .INCLUDEPATH += .# InputHEADERS += ServOtl.hSOURCES += main.cpp ServOtl.cppTEMPLATE =appCONFIG += qt warn_on thread pluginINCLUDEPATH += . LANGUAGE = C++QT += gui core sql xml network
#include <QtGui/QApplication>#include "myclient.h"int main(int argc, char *argv[]){ QApplication app(argc, argv); //MyClient client("LocalHost",2323); MyClient client("server1",2323); client.show(); return app.exec();}
#ifndef MYCLIENT_H#define MYCLIENT_H#include <QWidget>#include <QTcpSocket>#include <QtGui/QMainWindow>#include <QtNetwork>#include <QtXml> class QTextEdit;class QLineEdit;class QPushButton;static int insoob=0,inmsg;class MyClient : public QWidget{ Q_OBJECTprivate: QTcpSocket* m_pTcpSocket; QTextEdit* m_ptxtInfo; QLineEdit* m_ptxtInput; qint16 m_nNextBlockSize; QTextEdit* m_ptxt; QTextEdit* m_ptxt1; QTextEdit* m_ptxt2; QPushButton* pushButton_1; QPushButton* pushButton_2; QPushButton* pushButton_3; QPushButton* pushButton_4; QPushButton* pushButton_5; QPushButton* pushButton_6; QPushButton* pushButton_7; QPushButton* pushButton_8; QPushButton* pushButton_9; QPushButton* pushButton_10; QPushButton* pushButton_11; QPushButton* pushButton_12; QPushButton* pushButton_13; QPushButton* pushButton_14; QPushButton* pushButton_15; QPushButton* pushButton_16;public: MyClient(const QString& strHost, int nPort, QWidget* pwgt =0);private slots: void slotReadyRead(); void slotError (QAbstractSocket::SocketError); void slotSendToServer(); void slotConnected();};#endif // MYCLIENT_H
#include "myclient.h"#include <QTcpSocket>#include <QtGui/QMainWindow>#include <QtNetwork>#include <QMessageBox>#include <QTextEdit>#include <QLineEdit>#include <QPushButton>#include <QVBoxLayout>#include <QApplication>#include <QLabel> int iclientov=0; QString sss="";MyClient::MyClient(const QString& strHost, int nPort, QWidget* pwgt /*=0*/):QWidget(pwgt), m_nNextBlockSize(0){ QString str=QDir::currentPath(); MyClient::setGeometry(QRect(20, 20, 710, 520)); MyClient::setWindowTitle(QApplication::translate("MyClientClass", "\320\270\320\274\320\270\321\202\320\260\321\202\320\276\321\200 \320\272\320\273\320\270\320\265\320\275\321\202\320\260", 0, QApplication::UnicodeUTF8)); m_pTcpSocket=new QTcpSocket(this); m_pTcpSocket->connectToHost(strHost, nPort); connect(m_pTcpSocket, SIGNAL(connected()), SLOT(slotConnected()) ); connect(m_pTcpSocket, SIGNAL(readyRead()), SLOT(slotReadyRead()) ); connect(m_pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(slotError(QAbstractSocket::SocketError)) ); m_ptxtInfo= new QTextEdit; m_ptxtInput=new QLineEdit; m_ptxtInfo->setReadOnly(true); QPushButton* pcmd = new QPushButton("&Send"); connect(pcmd, SIGNAL(clicked()), SLOT(slotSendToServer())); connect(m_ptxtInput, SIGNAL(returnPressed()),this, SLOT(slotSendToServer())); QVBoxLayout* pvbxLayout = new QVBoxLayout; pvbxLayout-> addWidget(new QLabel("<H1>Client</H1>")); pvbxLayout-> addWidget(m_ptxtInfo); pvbxLayout-> addWidget(m_ptxtInput); pvbxLayout-> addWidget(pcmd); m_ptxt= new QTextEdit; m_ptxt->setReadOnly(true); m_ptxt1= new QTextEdit;// m_ptxt1->setReadOnly(true); m_ptxt2= new QTextEdit; m_ptxt2->setReadOnly(true); QGridLayout * pLArch=new QGridLayout(this); pLArch->addWidget (new QLabel((QApplication::translate("MyServerClass", "\320\277\321\200\320\276\321\202\320\276\320\272\320\276\320\273", 0, QApplication::UnicodeUTF8)) ),5,0,1,1); pLArch->addWidget (new QLabel((QApplication::translate("MyServerClass", "\320\276\320\272\320\275\320\276 \320\277\321\200\320\270\320\275\321\217\321\202\321\213\321\205 \321\201\320\276\320\276\320\261\321\211\320\265\320\275\320\270\320\271", 0, QApplication::UnicodeUTF8)) ),5,1,1,1); pLArch->addWidget(m_ptxt,6,0,1,1); pLArch->addWidget(m_ptxt2,6,1,1,1); pLArch->addWidget(pcmd,7,0,1,1);}void MyClient::slotReadyRead(){ QTime time; QString strMessage= time.toString()+" "+ "serv >> client :ReadyRead"; m_ptxt->append(strMessage); QFile file("a.xml"); // файл приёма if (file.open(QIODevice::Append)){ QByteArray arrBlock; QTextStream in(m_pTcpSocket); QString str=in.readAll(); arrBlock=str.toAscii () ; file.write(arrBlock); arrBlock.clear (); //QTime time; /*QString*/ strMessage= time.toString()+" "+ "serv >> client :" + str;// m_ptxtInfo->append(strMessage); m_ptxt->append(strMessage); file.close(); //otvet(); }else {m_ptxtInfo->append("slotReadClient:no read file : a.xml");}}void MyClient::slotError(QAbstractSocket::SocketError err){ QString strError="Error: no connect"; //m_ptxtInfo->append(strError); m_ptxt->append(strError);}void MyClient::slotSendToServer(){ QString ss="mmm"; QFile file("d.dat"); // файл приёма if (file.open(QIODevice::ReadOnly)){ QTextStream out(m_pTcpSocket); ss+=file.readAll(); out<<ss; file.close(); } else{ QMessageBox::about(this,"error","file open: d.dat"); } // QTextStream out(m_pTcpSocket); // out<<ss;}void MyClient::slotConnected(){// m_ptxtInfo->append("Received the connected() signal");//======================================= QTime time=QTime::currentTime (); m_ptxt2->append(time.toString()+" "+ "serv >> client : "+ (QApplication::translate("MyClientClass","\321\201\320\276\320\265\320\264\320\270\320\275\320\265\320\275\320\270\320\265 \321\203\321\201\321\202\320\260\320\275\320\276\320\262\320\273\320\265\320\275\320\276", 0, QApplication::UnicodeUTF8))); iclientov=1;}
TEMPLATE = appTARGET = DEPENDPATH += .INCLUDEPATH += .QT += qui network xml xmlpatterns# InputHEADERS += myclient.hSOURCES += main.cpp myclient.cpp