⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.cpp

📁 一个服务器端的通信程序
💻 CPP
字号:
#include <qsocket.h>#include <qserversocket.h>#include <qtopia/qpeapplication.h>#include <qvbox.h>#include <qtextview.h>#include <qlabel.h>#include <qpushbutton.h>#include <qtextstream.h>#include <qstring.h>#include <stdlib.h>#include <qlineedit.h>       //QString str;     QPushButton *send;    QTextView *infoText;      QLineEdit *inputText;    //char information[100];class ClientSocket:public QSocket{    Q_OBJECTpublic:     ClientSocket( int sock,QObject *parent=0, const char *name=0) :     QSocket(parent,name)   {    line=0;    connect(this,SIGNAL(readyRead()),SLOT(readClient()) );    connect(this,SIGNAL(connectionClosed()),SLOT(connectionClosed()) );    connect(send,SIGNAL(clicked()),this, SLOT(senddata()) );    setSocket(sock);        }    ~ClientSocket()    {    }public slots:	        void readClient()    {    while(canReadLine() )       {      /*  QTextStream os( this );        str = readLine();        os << line << ":" << str;        line++;*/        infoText->append(readLine());                }     //strcpy(information, (const char*)str);     //infoText->append((const char*)str);     //fprintf(stderr,"z:%s",information);     }     void connectionClosed()    {     delete this;    }        void senddata()   {     QTextStream os(this);     os << inputText->text() << "\n";     inputText->setText("");    }private:     int line;     };class SimpleServer :public QServerSocket{   Q_OBJECT public:   SimpleServer(QObject * parent=0):    QServerSocket(1500,1,parent)  {    if( !ok() )    {    qWarning("Failed to bind port 1500");    exit(1);    }  }  ~SimpleServer()  {  } void newConnection(int socket)  {  (void)new ClientSocket(socket,this);   emit newConnect();  } signals:   void newConnect();};class ServerInfo:public QVBox{  Q_OBJECTpublic:  ServerInfo()   {     QString itext = QString(     "This is a small server example.\n"     "Connect with the client now."     );    QLabel *lb = new QLabel (itext,this);    lb->setAlignment(AlignHCenter);    infoText = new QTextView(this);    inputText = new QLineEdit(this);    send = new QPushButton("Send",this);    QPushButton *quit = new QPushButton("Quit",this);    SimpleServer *server = new SimpleServer(this);    connect(server,SIGNAL(newConnect()),SLOT(newConnect()));    connect(quit, SIGNAL(clicked()),qApp,SLOT(quit()) );             }  ~ServerInfo()    {    } public slots:   void newConnect()    {     infoText->append("New connection\n");        }   /* public:     QTextView *infoText;*/};int main(int argc,char** argv){  QPEApplication app(argc,argv);  ServerInfo info;  app.setMainWidget(&info);  info.show();  return app.exec();}#include "server.moc"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -