c++ - qt tcp connection under linux -



c++ - qt tcp connection under linux -

i'm trying create client-server application on qt. have no problems algorithms. qt fine. works fine, under windows. when running application under linux, client connects server , writes info socket->write(mydataarray) = mydataarray.length(), server not process readyread signal. problem occurs under linux. have ideas of going on? utilize ubuntu 14.04 , windows 7, qt version 5.3.2.

client code:

#include "mainwindow.h" #include "ui_mainwindow.h" enum connection_type{ ssl_connection = 0, tcp_connection = 1 }; clientmainwindow::clientmainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow) { ui->setupui(this); this->socket = null; sockettype = ssl_connection; msgi = 0; } clientmainwindow::~clientmainwindow() { delete ui; } qabstractsocket* clientmainwindow::getsocket() { bool waserror = false; qabstractsocket *socket; if (ssl_connection == this->sockettype) { qstring keyfilename = "baseenicskey.key"; qstring certificatefilename = "baseenicscertificate.csr"; qssl::keyalgorithm algorithm = qssl::rsa; qssl::encodingformat encodingformat = qssl::pem; qbytearray passphrase = "pass0"; qsslsocket *sslsocket = new qsslsocket(this); qbytearray key; qfile filekey(keyfilename); if(filekey.open(qiodevice ::readonly)) { key = filekey.readall(); filekey.close(); } else { waserror = true; qdebug() << filekey.errorstring(); } qsslkey sslkey(key, algorithm, encodingformat, qssl::privatekey, passphrase); sslsocket->setprivatekey(sslkey); qbytearray certificate; qfile filecertificate(certificatefilename); if(filecertificate.open(qiodevice ::readonly)) { certificate = filecertificate.readall(); filecertificate.close(); }else { qdebug() << filecertificate.errorstring() << filecertificate.filename(); waserror = true; } if(!waserror){ qsslcertificate sslcertificate; sslcertificate = qsslcertificate(certificate); sslsocket->addcacertificate(sslcertificate); sslsocket->setlocalcertificate(sslcertificate); qlist<qsslcertificate> sslcertificates; sslcertificates.append(sslcertificate); sslsocket->setdefaultcacertificates(sslcertificates); sslsocket->setpeerverifymode(qsslsocket::verifypeer); sslsocket->setprotocol(qssl::sslv3); socket = sslsocket; } else{ socket = null; } } else { socket = new qtcpsocket(); } homecoming socket; } void clientmainwindow::readyreadslot() { qdatastream in(this->socket); in.setversion(qdatastream::qt_5_0); quint32 blocksize = 0; qdebug() << "client::readdata"; if (this->socket->bytesavailable() >= (int)sizeof(quint32)) { in >> blocksize; if (this->socket->bytesavailable() >= blocksize) { qbytearray b; in >> b; qdebug() << "from client :"<< b; } else { qdebug() << "error : this->tcpsocket->bytesavailable() < blocksize"; } } else { qdebug() << "error : this->tcpsocket->bytesavailable() < (int)sizeof(quint32)\n"; } } void clientmainwindow::newdataforwriteslot(qbytearray b) { b.append("--"); b.append(qstring::number(msgi)); msgi++; qdebug() << "newdataforwriteslot: " << b; qbytearray block; qdatastream out(&block, qiodevice::writeonly); out.setversion(qdatastream::qt_5_0); out << (quint32)0; out << b; out.device()->seek(0); out << (quint32)(block.size() - sizeof(quint32)); qdebug() << "block length" << block.length(); qdebug() << "write length"<< this->socket->write(block); this->socket->flush(); } void clientmainwindow::sslerrorslot(qlist<qsslerror> error) { qdebug() << "sslerrorslot: " << error; ((qsslsocket*)this->socket)->ignoresslerrors(); } void clientmainwindow::socketerrorslot(qabstractsocket::socketerror error) { qdebug() << "socketerrorslot: " << error; } void clientmainwindow::isencryptionreadyslot() { qdebug() << "isencryptionreadyslot"; } void clientmainwindow::isconnectedslot() { qdebug() << "isconnectedslot"; } void clientmainwindow::encryptedbyteswrittenslot(qint64 x) { qdebug() << "encryptedbyteswrittenslot: " << x; } void clientmainwindow::connecttoserver() { delete this->socket; this->socket = getsocket(); if (ssl_connection == this->sockettype) { connect(((qsslsocket*)this->socket), signal(sslerrors(qlist<qsslerror>)), this, slot(sslerrorslot(qlist<qsslerror>)) ); connect(((qsslsocket*)this->socket), signal(encryptedbyteswritten(qint64)), this, slot(encryptedbyteswrittenslot(qint64)) ); } connect(this->socket, signal(connected()), this, slot(isconnectedslot())); connect(this->socket, signal(readyread()), this, slot(readyreadslot()) ); connect(this, signal(newdataforwritesignal(qbytearray)), this, slot(newdataforwriteslot(qbytearray)) ); connect(this->socket, signal(error(qabstractsocket::socketerror)), this, slot(socketerrorslot(qabstractsocket::socketerror)) ); qstring hostname = "127.0.0.1"; int port = 9600; if (ssl_connection == this->sockettype) { ((qsslsocket*)this->socket)->connecttohostencrypted( hostname, port, qsslsocket::readwrite, qsslsocket::ipv4protocol); } else { this->socket->connecttohost( hostname, port, qsslsocket::readwrite, qsslsocket::ipv4protocol); } if(!this->socket->waitforconnected()) { qdebug() << "connection time out"; } this->newdataforwriteslot("hi i'm client. it's first connection."); } void clientmainwindow::on_pushbutton_clicked() { this->sockettype = ssl_connection; this->connecttoserver(); } void clientmainwindow::on_pushbutton_2_clicked() { this->sockettype = tcp_connection; this->connecttoserver(); } void clientmainwindow::on_pushbutton_3_clicked() { this->newdataforwriteslot("hi, i'm client!"); }

server code:

#include "mainwindow.h" #include "ui_mainwindow.h" enum connection_type{ ssl_connection = 0, tcp_connection = 1 }; servermainwindow::servermainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow) { ui->setupui(this); this->server = new qtcpserver(); connect(this->server, signal(newconnection()), this, slot(incomingconnectionslot())); connect(this->server, signal(accepterror(qabstractsocket::socketerror)), this, slot(servererrorslot(qabstractsocket::socketerror)) ); qhostaddress adress; adress.setaddress("127.0.0.1"); int port = 9600; this->sockettype = ssl_connection; this->socket = null; if (!this->server->listen(adress, port)) { qdebug() << "server not start"; } msgi = 0 ; } servermainwindow::~servermainwindow() { delete ui; } void servermainwindow::incomingconnectionslot() { disconnect(this->socket, signal(readyread()), this, slot(readyreadslot())); disconnect(this, signal(newdataforwritesignal(qbytearray)), this, slot(newdataforwriteslot(qbytearray)) ); disconnect(this->socket, signal(error(qabstractsocket::socketerror)), this, slot(socketerrorslot(qabstractsocket::socketerror)) ); if (ssl_connection == sockettype) { disconnect(((qsslsocket*)this->socket), signal(peerverifyerror(qsslerror)), this, slot(peerverrefyerrorslot(qsslerror)) ); disconnect(((qsslsocket*)this->socket), signal(encrypted()), this, slot(isencryptionreadyslot()) ); disconnect(((qsslsocket*)this->socket), signal(encryptedbyteswritten(qint64)), this, slot(encrypteddatawriteslot(qint64))); } delete this->socket; this->socket = this->getsocket(); this->incomingconnection( this->server->nextpendingconnection()->socketdescriptor()); connect(this->socket, signal(readyread()), this, slot(readyreadslot()) ); connect(this->socket, signal(error(qabstractsocket::socketerror)), this, slot(socketerrorslot(qabstractsocket::socketerror)) ); if (ssl_connection == sockettype) { connect(((qsslsocket*)this->socket), signal(encrypted()), this, slot(isencryptionreadyslot()) ); connect(((qsslsocket*)this->socket), signal(peerverifyerror(qsslerror)), this, slot(peerverrefyerrorslot(qsslerror)) ); connect(((qsslsocket*)this->socket), signal(encryptedbyteswritten(qint64)), this, slot(encrypteddatawriteslot(qint64))); ((qsslsocket*)this->socket)->startserverencryption(); } qdebug() << "is socket open" << this->socket->isopen(); this->newdataforwriteslot("hi i'm server! it's first connection!"); } void servermainwindow::incomingconnection(qintptr socketdescriptor) { qdebug() << "incomingconnection" << socketdescriptor; this->socket->setsocketdescriptor(socketdescriptor); } qabstractsocket* servermainwindow::getsocket() { bool waserror = false; qabstractsocket *socket; if (ssl_connection == this->sockettype) { qstring keyfilename = "baseenicskey.key"; qstring certificatefilename = "baseenicscertificate.csr"; qssl::keyalgorithm algorithm = qssl::rsa; qssl::encodingformat encodingformat = qssl::pem; qbytearray passphrase = "pass0"; qsslsocket *sslsocket = new qsslsocket(this); qbytearray key; qfile filekey(keyfilename); if(filekey.open(qiodevice ::readonly)) { key = filekey.readall(); filekey.close(); } else { waserror = true; qdebug() << filekey.errorstring(); } qsslkey sslkey(key, algorithm, encodingformat, qssl::privatekey, passphrase); sslsocket->setprivatekey(sslkey); qbytearray certificate; qfile filecertificate(certificatefilename); if(filecertificate.open(qiodevice ::readonly)) { certificate = filecertificate.readall(); filecertificate.close(); }else { qdebug() << filecertificate.errorstring() << filecertificate.filename(); waserror = true; } if(!waserror){ qsslcertificate sslcertificate; sslcertificate = qsslcertificate(certificate); sslsocket->addcacertificate(sslcertificate); sslsocket->setlocalcertificate(sslcertificate); qlist<qsslcertificate> sslcertificates; sslcertificates.append(sslcertificate); sslsocket->setdefaultcacertificates(sslcertificates); sslsocket->setpeerverifymode(qsslsocket::querypeer); sslsocket->setprotocol(qssl::sslv3); socket = sslsocket; } else{ socket = null; } } else { socket = new qtcpsocket(); } homecoming socket; } void servermainwindow::readyreadslot() { qdatastream in(this->socket); in.setversion(qdatastream::qt_5_0); quint32 blocksize = 0; qdebug() << "server::readdata"; if (this->socket->bytesavailable() >= sizeof(quint32)) { in >> blocksize; qdebug() << "this->socket->bytesavailable" << this->socket->bytesavailable(); if (this->socket->bytesavailable() >= blocksize) { qbytearray b; in >> b; qdebug() << "from client: " << b; } else { qdebug() << "error : this->tcpsocket->bytesavailable() < blocksize"; qbytearray b; in >> b; qdebug() << b; } } else { qdebug() << "error : this->tcpsocket->bytesavailable() < (int)sizeof(quint32)"; } } void servermainwindow::newdataforwriteslot(qbytearray b) { b.append("--"); b.append(qstring::number(msgi)); msgi++; qdebug() << "newdataforwriteslot: "<< b; qbytearray block; qdatastream out(&block, qiodevice::writeonly); out.setversion(qdatastream::qt_5_0); out << (quint32)0; out << b; out.device()->seek(0); out << (quint32)(block.size() - sizeof(quint32)); qdebug() << "block length" << block.length(); qdebug() << "write length"<< this->socket->write(block); this->socket->flush(); } void servermainwindow::peerverrefyerrorslot(qsslerror error) { qdebug() << "peerverrefyerrorslot: " << error; } void servermainwindow::socketerrorslot(qabstractsocket::socketerror error) { qdebug() << "socketerrorslot: " << error; } void servermainwindow::isencryptionreadyslot() { qdebug() << "isencryptionreadyslot"; } void servermainwindow::servererrorslot(qabstractsocket::socketerror error) { qdebug() << "servererrorslot: " << error; } void servermainwindow::on_checkbox_clicked() { if(ui->checkbox->ischecked()) { this->sockettype = ssl_connection; } else { this->sockettype = tcp_connection; } } void servermainwindow::encrypteddatawriteslot(qint64 x) { qdebug() << "encrypteddatawrite" << x; } void servermainwindow::on_writebutton_clicked() { this->newdataforwriteslot("i'm server!"); }

c++ linux qt qtcpsocket qtcpserver

Comments

Popular posts from this blog

javascript - THREE.js reposition vertices for RingGeometry -

javascript - I need to update the text of a paragraph by inline edit -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -