-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
53 lines (46 loc) · 1.28 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHostAddress>
#include <QDebug>
#include <QFile>
#include <QByteArray>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
socket = new QTcpSocket(this);
qDebug() << "socket created";
connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
socket->connectToHost(QHostAddress::LocalHost, qint16(1337), QIODevice::ReadWrite);
}
MainWindow::~MainWindow()
{
delete ui;
socket->close();
delete socket;
}
void MainWindow::socketConnected()
{
qDebug() << "socked connected";
connect(socket, SIGNAL(readyRead()), this, SLOT(dataAvalaible()));
//socket->write("some chars");
QFile file("test.xml");
if (!file.open(QIODevice::ReadOnly)) qDebug() << "Error - file did not open";
QByteArray bytes = file.readAll();
socket->write(bytes);
socket->flush();
qDebug() << "data send";
}
void MainWindow::dataAvalaible()
{
qint64 size = socket->bytesAvailable();
qDebug() << size;
if (size <=0 || size >=60000)
{
qDebug() << QString("Bad size == " + size);
}
QByteArray arr = socket->read(size);
QString receivedData = QString(arr);
qDebug() << "receivedData: " << receivedData;
}