MyClient::MyClient()
{
actions = 0;
theLayout->addWidget(portNumberUDP,0,2);
theLayout->addWidget(portNumberTCP,1,2);
theLayout->addWidget(Browse1, 0, 1);
theLayout->addWidget(Browse2, 1, 1);
theLayout->addWidget(startUDP,0,3);
theLayout->addWidget(nic,0,4);
theLayout->addWidget(TCPfile,1,0);
theLayout->addWidget(UDPfile,0,0);
theLayout->addWidget(startTCP,1,3);
theLayout->addWidget(lblStatusUDP,2,0,1,3);
theLayout->addWidget(lblStatusTCP,3,0,1,3);
setLayout(theLayout);
connect(startUDP, SIGNAL(clicked()), this, SLOT(requestAnotherUDP()));
connect(startTCP, SIGNAL(clicked()), this, SLOT(requestAnotherTCP()));
connect(Browse1,SIGNAL(clicked()),this,SLOT(catchBrowse1()));
connect(Browse2,SIGNAL(clicked()),this,SLOT(catchBrowse2()));
connect(TCPSocket, SIGNAL(connected()) , this , SLOT(catchConnected()));
}
void MyClient::requestAnotherUDP()
{
actions++;
QFile file(UDPfile
->text
());
int sendData = qrand() % 10;
startUDP->setEnabled(false);
blockSize = 0;
UDPSocket->abort();
UDPSocket->connectToHost(nic->text(),portNumberUDP->text().toInt());
lblStatusUDP
->setText
("UDP: " + QString::number(actions
) + ". Sending file to "+nic
->text
()+" on port " + portNumberUDP
->text
());
while(!infile.atEnd())
{
infile >> block;
block.append(' ');
BigBlock.append(block);
}
in.writeBytes(BigBlock,BigBlock.length());
UDPSocket->disconnect();
UDPSocket->waitForDisconnected(3000);
lblStatusUDP
->setText
("UDP: " + QString::number(actions
) + ". Sent file to "+nic
->text
()+" on port " + portNumberUDP
->text
());
startUDP->setEnabled(true);
}
void MyClient::requestAnotherTCP()
{
actions++;
startTCP->setEnabled(false);
blockSize = 0;
TCPSocket->abort();
TCPSocket->connectToHost(nic->text(),portNumberTCP->text().toInt());
TCPSocket->setReadBufferSize(50000000);
}
void MyClient::catchConnected()
{
qDebug("Connected");
QFile file(TCPfile
->text
());
char point[1500];
if(TCPSocket->isValid())
{
lblStatusTCP
->setText
("TCP: " + QString::number(actions
) + ". Sending file to "+nic
->text
()+" on port " + portNumberTCP
->text
());
QFile newfile
("Test.dat");
int totalbytes = 0;
int totalbytes2 = 0;
int count = -1;
int count2 = -1;
while(!infile.atEnd())
{
count = infile.readRawData(point,1500);
count2 = in.writeRawData(point,count);
if(!TCPSocket->waitForBytesWritten(300))
{
}
totalbytes2 += fi.writeRawData(point,count);
if(count2 == -1)
{
}
}
TCPSocket->waitForBytesWritten(3000);
newfile.waitForBytesWritten(3000);
newfile.close();
startTCP->setEnabled(true);
TCPSocket->flush();
TCPSocket->disconnectFromHost();
TCPSocket->waitForDisconnected(-1);
lblStatusTCP
->setText
("TCP: " + QString::number(actions
) + ". Sent file to "+nic
->text
()+" on port " + portNumberTCP
->text
());
}
}
void MyClient::catchBrowse1()
{
if(!strfile.isEmpty())
{
UDPfile->setText(strfile);
}
}
void MyClient::catchBrowse2()
{
if(!strfile.isEmpty())
{
TCPfile->setText(strfile);
}
}
MyClient::MyClient()
{
actions = 0;
startUDP = new QPushButton("StartUDP");
startTCP = new QPushButton("StartTCP");
Browse1 = new QPushButton("Browse");
Browse2 = new QPushButton("Browse");
portNumberUDP = new QLineEdit("");
portNumberTCP = new QLineEdit("9654");
TCPfile = new QLineEdit("");
UDPfile = new QLineEdit("");
nic = new QLineEdit("10.24.74.115");
theLayout = new QGridLayout;
lblStatusUDP = new QLabel;
lblStatusTCP = new QLabel;
theLayout->addWidget(portNumberUDP,0,2);
theLayout->addWidget(portNumberTCP,1,2);
theLayout->addWidget(Browse1, 0, 1);
theLayout->addWidget(Browse2, 1, 1);
theLayout->addWidget(startUDP,0,3);
theLayout->addWidget(nic,0,4);
theLayout->addWidget(TCPfile,1,0);
theLayout->addWidget(UDPfile,0,0);
theLayout->addWidget(startTCP,1,3);
theLayout->addWidget(lblStatusUDP,2,0,1,3);
theLayout->addWidget(lblStatusTCP,3,0,1,3);
setLayout(theLayout);
TCPSocket = new QTcpSocket();
connect(startUDP, SIGNAL(clicked()), this, SLOT(requestAnotherUDP()));
connect(startTCP, SIGNAL(clicked()), this, SLOT(requestAnotherTCP()));
connect(Browse1,SIGNAL(clicked()),this,SLOT(catchBrowse1()));
connect(Browse2,SIGNAL(clicked()),this,SLOT(catchBrowse2()));
connect(TCPSocket, SIGNAL(connected()) , this , SLOT(catchConnected()));
}
void MyClient::requestAnotherUDP()
{
actions++;
QUdpSocket* UDPSocket = new QUdpSocket;
QFile file(UDPfile->text());
file.open(QIODevice::ReadOnly);
QTextStream infile(&file);
int sendData = qrand() % 10;
startUDP->setEnabled(false);
blockSize = 0;
QByteArray block, BigBlock;
UDPSocket->abort();
UDPSocket->connectToHost(nic->text(),portNumberUDP->text().toInt());
lblStatusUDP->setText("UDP: " + QString::number(actions) + ". Sending file to "+nic->text()+" on port " + portNumberUDP->text());
QDataStream in(UDPSocket);
in.setVersion(QDataStream::Qt_4_0);
while(!infile.atEnd())
{
infile >> block;
block.append(' ');
BigBlock.append(block);
}
in.writeBytes(BigBlock,BigBlock.length());
UDPSocket->disconnect();
UDPSocket->waitForDisconnected(3000);
lblStatusUDP->setText("UDP: " + QString::number(actions) + ". Sent file to "+nic->text()+" on port " + portNumberUDP->text());
startUDP->setEnabled(true);
}
void MyClient::requestAnotherTCP()
{
actions++;
startTCP->setEnabled(false);
blockSize = 0;
TCPSocket->abort();
TCPSocket->connectToHost(nic->text(),portNumberTCP->text().toInt());
TCPSocket->setReadBufferSize(50000000);
}
void MyClient::catchConnected()
{
qDebug("Connected");
QByteArray block, BigBlock;
QFile file(TCPfile->text());
file.open(QIODevice::ReadOnly);
QDataStream infile(&file);
char point[1500];
if(TCPSocket->isValid())
{
lblStatusTCP->setText("TCP: " + QString::number(actions) + ". Sending file to "+nic->text()+" on port " + portNumberTCP->text());
QDataStream in(TCPSocket);
in.setVersion(QDataStream::Qt_4_3);
QFile newfile("Test.dat");
newfile.open(QIODevice::WriteOnly);
QDataStream fi(&newfile);
int totalbytes = 0;
int totalbytes2 = 0;
int count = -1;
int count2 = -1;
while(!infile.atEnd())
{
count = infile.readRawData(point,1500);
count2 = in.writeRawData(point,count);
if(!TCPSocket->waitForBytesWritten(300))
{
QMessageBox::about(this,"False","False");
}
totalbytes2 += fi.writeRawData(point,count);
if(count2 == -1)
{
QMessageBox::about(this,"-1","-1");
}
}
TCPSocket->waitForBytesWritten(3000);
newfile.waitForBytesWritten(3000);
newfile.close();
startTCP->setEnabled(true);
TCPSocket->flush();
TCPSocket->disconnectFromHost();
TCPSocket->waitForDisconnected(-1);
lblStatusTCP->setText("TCP: " + QString::number(actions) + ". Sent file to "+nic->text()+" on port " + portNumberTCP->text());
}
}
void MyClient::catchBrowse1()
{
QString strfile = QFileDialog::getOpenFileName(this,"UDP File");
if(!strfile.isEmpty())
{
UDPfile->setText(strfile);
}
}
void MyClient::catchBrowse2()
{
QString strfile = QFileDialog::getOpenFileName(this,"TCP File");
if(!strfile.isEmpty())
{
TCPfile->setText(strfile);
}
}
To copy to clipboard, switch view to plain text mode
#include "testserver.h"
{
node = 1;
connect(TCPServ,SIGNAL(newConnection()),this,SLOT(createNewSocket()));
start->setText("Start");
start->setCheckable(true);
connect(start,SIGNAL(clicked()),this,SLOT(catchstart()));
theLayout->addWidget(port,0,0);
theLayout->addWidget(nic,0,1);
theLayout->addWidget(start,0,2);
theLayout->addWidget(txtEdit,1,0,1,3);
port->setText("port");
for(int i = nicList.size()-1; i>=0; i--)
{
this->nic->insertItem(i, nicList[i].toString());
}
setLayout(theLayout);
}
void tcpServer::createNewSocket()
{
QTcpSocket * thesocket
= TCPServ
->nextPendingConnection
();
ServerNode* newNode = new ServerNode(this,thesocket,this->txtEdit, node++);
}
void tcpServer::catchstart()
{
if(start->isChecked())
{
start->setText("Stop");
TCPServ
->listen
(QHostAddress(nic
->currentText
()),port
->text
().
toInt());
}
else
{
start->setText("Start");
TCPServ->close();
}
}
void tcpServer
::changeText(QString newstring
) {
txtEdit->append("\n"+newstring);
}
ServerNode::ServerNode()
{
}
{
node = intnode;
totalbytes = 0;
connect(thesocket,SIGNAL(readyRead()),this,SLOT(readNow()));
//connect(thesocket,SIGNAL(disconnected()),this,SLOT(fileDone()));
input.setDevice(thesocket);
socket = thesocket;
file .
setFileName(QString("Test_%1").
arg(node
));
output.setDevice(&file);
}
void ServerNode::readNow()
{
int count = 0;
int count2 = 0;
char point[1500];
while(!input.atEnd())
{
count = input.readRawData(point,1500);
if(count == -1)
{
}
totalbytes+=count;
count2 = output.writeRawData(point,count);
if(count != count2)
{
}
}
//file.waitForBytesWritten(1000);
}
void ServerNode::run()
{
}
void ServerNode::fileDone()
{
}
#include "testserver.h"
tcpServer::tcpServer(): QWidget()
{
node = 1;
TCPServ = new QTcpServer;
connect(TCPServ,SIGNAL(newConnection()),this,SLOT(createNewSocket()));
txtEdit = new QTextEdit;
QGridLayout* theLayout = new QGridLayout;
port = new QLineEdit;
nic = new QComboBox;
start = new QPushButton;
start->setText("Start");
start->setCheckable(true);
connect(start,SIGNAL(clicked()),this,SLOT(catchstart()));
theLayout->addWidget(port,0,0);
theLayout->addWidget(nic,0,1);
theLayout->addWidget(start,0,2);
theLayout->addWidget(txtEdit,1,0,1,3);
port->setText("port");
QList<QHostAddress> nicList = QNetworkInterface::allAddresses();
for(int i = nicList.size()-1; i>=0; i--)
{
this->nic->insertItem(i, nicList[i].toString());
}
setLayout(theLayout);
}
void tcpServer::createNewSocket()
{
QTcpSocket * thesocket = TCPServ->nextPendingConnection();
ServerNode* newNode = new ServerNode(this,thesocket,this->txtEdit, node++);
}
void tcpServer::catchstart()
{
if(start->isChecked())
{
start->setText("Stop");
TCPServ->listen(QHostAddress(nic->currentText()),port->text().toInt());
}
else
{
start->setText("Start");
TCPServ->close();
}
}
void tcpServer::changeText(QString newstring)
{
txtEdit->append("\n"+newstring);
}
ServerNode::ServerNode()
{
}
ServerNode::ServerNode(tcpServer* parent,QTcpSocket* thesocket,QTextEdit* point, int intnode)
{
node = intnode;
totalbytes = 0;
connect(thesocket,SIGNAL(readyRead()),this,SLOT(readNow()));
//connect(thesocket,SIGNAL(disconnected()),this,SLOT(fileDone()));
input.setDevice(thesocket);
socket = thesocket;
file .setFileName(QString("Test_%1").arg(node));
file.open(QIODevice::Append);
output.setDevice(&file);
}
void ServerNode::readNow()
{
int count = 0;
int count2 = 0;
char point[1500];
QByteArray oneBlock;
while(!input.atEnd())
{
count = input.readRawData(point,1500);
if(count == -1)
{
QMessageBox::about(0,"-1","-1");
}
totalbytes+=count;
count2 = output.writeRawData(point,count);
if(count != count2)
{
QMessageBox::about(0,"1!=2","1!=2");
}
}
//file.waitForBytesWritten(1000);
}
void ServerNode::run()
{
}
void ServerNode::fileDone()
{
QFile::rename(QString("Temp__%1.dat").arg(node),QString("Complete__%1.dat").arg(node));
}
To copy to clipboard, switch view to plain text mode
Bookmarks