I just try. But failed. This in my code. Can you help me for fix it ?
Thank's before
Btw, I created project with Qt-creator
Server Code
main.cpp
#include <QtCore/QCoreApplication>
#include "server.h"
int main(int argc, char *argv[])
{
Server server;
{
qCritical( "Cannot listen to port 9876." );
return 1;
}
return a.exec();
}
#include <QtCore/QCoreApplication>
#include "server.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Server server;
if( !server.listen( QHostAddress::Any, 9876 ) )
{
qCritical( "Cannot listen to port 9876." );
return 1;
}
return a.exec();
}
To copy to clipboard, switch view to plain text mode
server.h
#ifndef SERVER_H
#define SERVER_H
#include <QtNetwork>
{
public:
Server();
protected:
void incomingConnection( int descriptor );
};
#endif
#ifndef SERVER_H
#define SERVER_H
#include <QtNetwork>
class Server : public QTcpServer
{
public:
Server();
protected:
void incomingConnection( int descriptor );
};
#endif
To copy to clipboard, switch view to plain text mode
server.cpp
#include "server.h"
#include "thread.h"
{
}
void Server::incomingConnection( int descriptor )
{
ServerThread *thread = new ServerThread( descriptor, this );
connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
thread->start();
}
#include "server.h"
#include "thread.h"
Server::Server() : QTcpServer()
{
}
void Server::incomingConnection( int descriptor )
{
ServerThread *thread = new ServerThread( descriptor, this );
connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
thread->start();
}
To copy to clipboard, switch view to plain text mode
thread.h
#ifndef THREAD_H
#define THREAD_H
#include <QtNetwork>
#include <QtGui>
class ServerThread
: public QThread{
public:
ServerThread
( int descriptor,
QObject *parent
);
void run();
private:
int m_descriptor;
};
#endif
#ifndef THREAD_H
#define THREAD_H
#include <QtNetwork>
#include <QtGui>
class ServerThread : public QThread
{
public:
ServerThread( int descriptor, QObject *parent );
void run();
private:
int m_descriptor;
};
#endif
To copy to clipboard, switch view to plain text mode
thread.cpp
#include "thread.h"
ServerThread
::ServerThread( int descriptor,
QObject *parent
) : QThread( parent
){
m_descriptor = descriptor;
}
void ServerThread::run()
{
if( !socket.setSocketDescriptor( m_descriptor ) )
{
qDebug( "Socket error!" );
return;
}
QFile file("images/linux.png");
socket.write( data );
socket.disconnectFromHost();
//socket.waitForDisconnected();
}
#include "thread.h"
ServerThread::ServerThread( int descriptor, QObject *parent ) : QThread( parent )
{
m_descriptor = descriptor;
}
void ServerThread::run()
{
QTcpSocket socket;
if( !socket.setSocketDescriptor( m_descriptor ) )
{
qDebug( "Socket error!" );
return;
}
QFile file("images/linux.png");
QByteArray data=file.readAll();
socket.write( data );
socket.disconnectFromHost();
//socket.waitForDisconnected();
}
To copy to clipboard, switch view to plain text mode
Client Code
main.cpp
#include <QtGui/QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
Dialog w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui>
#include <QtNetwork>
namespace Ui
{
class Dialog;
}
{
Q_OBJECT
public:
~Dialog();
private:
Ui::Dialog *ui;
private slots:
void on_pushButton_clicked();
void tcpReady();
};
#endif // DIALOG_H
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui>
#include <QtNetwork>
namespace Ui
{
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QTcpSocket socket;
private slots:
void on_pushButton_clicked();
void tcpReady();
void tcpError( QAbstractSocket::SocketError error );
};
#endif // DIALOG_H
To copy to clipboard, switch view to plain text mode
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
: QDialog(parent
), ui
(new Ui
::Dialog) {
ui->setupUi(this);
connect( &socket, SIGNAL(readyRead()), this, SLOT(tcpReady()) );
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
socket.abort();
socket.connectToHost( ui->lineEdit->text(), 9876 );
}
void Dialog::tcpReady()
{
QFile file("linuxmu.png");
ds << ar;
file.close();
}
{
return;
tr("TCP error: %1").arg( socket.errorString() ) );
ui->label->setText( tr("<i>No Image</i>") );
ui->pushButton->setEnabled( true );
}
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
connect( &socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(tcpError(QAbstractSocket::SocketError)) );
connect( &socket, SIGNAL(readyRead()), this, SLOT(tcpReady()) );
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked()
{
socket.abort();
socket.connectToHost( ui->lineEdit->text(), 9876 );
}
void Dialog::tcpReady()
{
QByteArray ar=socket.readAll();
QFile file("linuxmu.png");
file.open(QIODevice::WriteOnly);
QDataStream ds(&file);
ds << ar;
file.close();
}
void Dialog::tcpError( QAbstractSocket::SocketError error )
{
if( error == QAbstractSocket::RemoteHostClosedError )
return;
QMessageBox::warning( this, tr("Error"),
tr("TCP error: %1").arg( socket.errorString() ) );
ui->label->setText( tr("<i>No Image</i>") );
ui->pushButton->setEnabled( true );
}
To copy to clipboard, switch view to plain text mode
Bookmarks