Привожу вам пример клиент-серверного приложения:
Клиент подключается к серверу, при удачном коннекте клиент шлет на сервер файл. Сервер принимает файл и сохраняет его на диск.
серверная часть:
mainwindow.h
C++ (Qt)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QDataStream>
#include <QFile>
#include <QDir>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
QTcpServer *pServer;
QTcpSocket *pSocket;
quint64 nextBlockSize;
private slots:
void newConnection();
void pSocketReadyRead();
};
#endif // MAINWINDOW_H
mainwindow.cppC++ (Qt)
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pServer = new QTcpServer(this);
pSocket = new QTcpSocket(this);
pServer->listen(QHostAddress::Any, 1234);
connect(pServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
}
void MainWindow::newConnection()
{
QTcpSocket *pSocket = pServer->nextPendingConnection();
connect (pSocket, SIGNAL(disconnected()), pSocket, SLOT(deleteLater()));
connect (pSocket, SIGNAL(readyRead()), this, SLOT(pSocketReadyRead()));
nextBlockSize = 0;
}
void MainWindow::pSocketReadyRead()
{
QTcpSocket * socket = static_cast<QTcpSocket*>(sender());
forever
{
QDataStream stream(socket);
stream.setVersion(QDataStream::Qt_4_7);
if (nextBlockSize == 0)
{
if (socket->bytesAvailable() < sizeof(quint64))
return;
stream >> nextBlockSize;
}
if (nextBlockSize > socket->bytesAvailable())
return;
QByteArray arrFile;
stream >> arrFile;
QFile file("C:\\rec.jpg");
file.open(QIODevice::WriteOnly);
file.write(arrFile);
nextBlockSize = 0;
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
клиентская часть:
mainwindow.hC++ (Qt)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QHostAddress>
#include <QDataStream>
#include <QFile>
#include <QDir>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QTcpSocket *pSocket;
private slots:
void on_pushButton_clicked();
void onConnected();
};
#endif // MAINWINDOW_H
mainwindow.cppC++ (Qt)
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pSocket = new QTcpSocket(this);
connect(pSocket, SIGNAL(connected()), this, SLOT(onConnected()));
}
void MainWindow::onConnected()
{
QByteArray block;
QDataStream stream(&block, QIODevice::WriteOnly);
stream.setVersion(QDataStream::Qt_4_7);
QFile file("C:\\test.jpg");
file.open(QIODevice::ReadOnly);
QByteArray buf = file.readAll();
stream << quint64(file.size());
stream << buf;
pSocket->write(block);
pSocket->flush();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
pSocket->connectToHost(QHostAddress::LocalHost, 1234);
}