frmclient.cpp
资源名称:Webserver.rar [点击查看]
上传用户:yayahi0755
上传日期:2022-05-14
资源大小:876k
文件大小:2k
源码类别:
浏览器
开发平台:
Unix_Linux
- #include "frmclient.h"
- /*
- * Constructs a frmclient which is a child of 'parent', with the
- * name 'name' and widget flags set to 'f'
- *
- * The dialog will by default be modeless, unless you set 'modal' to
- * TRUE to construct a modal dialog.
- */
- static const Q_UINT16 ipcPort = 8000;
- frmclient::frmclient( QWidget* parent, const char* name, bool modal, WFlags fl )
- : clientform( parent, name, modal, fl )
- {
- socket =new QSocket (this);
- connectFlag=FALSE;
- connect( socket, SIGNAL(connected()), this ,SLOT(SocketConnected()));
- connect( socket, SIGNAL(connectionClosed()), this, SLOT(ServerConnectionClosed()) );
- connect( socket, SIGNAL(error(int)), this ,SLOT(SocketError(int)) );
- connect( btnSendPicture, SIGNAL(clicked()), SLOT(sendImage()) );
- connect( btnSendText, SIGNAL(clicked()), SLOT(sendText()) );
- connect( btnSendFile, SIGNAL(clicked()), SLOT(sendFile()) );
- connect( btnConnect, SIGNAL( clicked()), SLOT( connectNet()));
- connect( btnExit, SIGNAL( clicked()), SLOT(exitSystem()));
- socket->connectToHost( "196.168.0.100", ipcPort );
- }
- /*
- * Destroys the object and frees any allocated resources
- */
- frmclient::~frmclient()
- {
- // no need to delete child widgets, Qt does it all for us
- }
- void frmclient::ServerConnectionClosed()
- {
- connectFlag=FALSE;
- txtContent->append (tr("disconnect net"));
- }
- void frmclient::SocketConnected()
- {
- connectFlag=TRUE;
- txtContent->append (tr("connect net"));
- }
- void frmclient::SocketError(int error)
- {
- txtContent->append( tr("Error number %1 occurredn").arg(error) );
- }
- void frmclient::connectNet()
- {
- socket->connectToHost( "192.168.0.100", ipcPort );
- }
- void frmclient::sendImage()
- {
- QString imageName=txtContent->text();
- QImage image( imageName );
- if ( !image.isNull() ) {
- sendPacket( image );
- }
- }
- void frmclient::sendText()
- {
- if(connectFlag)
- sendPacket( txtContent->text() );
- else
- txtContent->append (tr("Net is not connected"));
- }
- void frmclient::sendFile()
- {
- txtContent->setText("");
- }
- void frmclient::sendPacket( const QVariant &v )
- {
- QByteArray ba;
- QDataStream varDs( ba, IO_WriteOnly );
- varDs << v;
- QDataStream ds( socket );
- ds << (Q_UINT32) ba.size();
- socket->writeBlock( ba.data(), ba.size() );
- }
- void frmclient::exitSystem()
- {
- close();
- }