I'm writing an application that communicates via QTcpSocket between Server and Client.
Server is an application with gui on standard pc.
Client is a console application on Raspberry pi.
client.cpp
#include "client.h"
#include <QHostAddress>
#include <cstdio>
Client::Client() :
QObject()
{
socket = new QTcpSocket(this);
printf("try to connect.\n");
connect(socket, SIGNAL(connected()),
this, SLOT(onConnected()));
connect(socket, SIGNAL(readyRead()),
this, SLOT(readyRead()));
}
void Client::onConnected()
{
printf("Connection established.\n");
// char buffer[1024];
// forever
// {
// while(socket->canReadLine())
// {
// QByteArray ba = socket->readLine();
// printf("from server: %s", ba.constData());
// }
// printf(">> ");
// gets(buffer);
// int len = strlen(buffer);
// buffer[len] = '\n';
// buffer[len+1] = '\0';
// socket->write(buffer);
// socket->flush();
// }
}
void Client::readyRead(){
if(socket->canReadLine())
{
QByteArray ba = socket->readLine();
printf("from server: %s", ba.constData());
}else{
printf(":(");
}
}
void Client::connectToServer()
{
socket->connectToHost(QString("192.168.0.103"), 5100);
}
server.cpp
#include "server.h"
#include <QTcpServer>
#include <QTcpSocket>
#include <cstdio>
#include <QtDebug>
Server::Server(int listenPort)
:QObject()
{
port = listenPort;
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()),
this, SLOT(onNewConnection()));
}
void Server::listen()
{
server->listen(QHostAddress::Any, port);
}
void Server::sendMessage(QString message){
qDebug() << message;
QByteArray response;
response.append(message);
qDebug() << socket->peerAddress() ;
socket->write(response);
socket->flush();
}
void Server::onNewConnection()
{
socket = server->nextPendingConnection();
if(socket->state() == QTcpSocket::ConnectedState)
{
qDebug()<<socket->peerAddress();\
emit onConnectionEstablished(socket->peerAddress().toString());
}
connect(socket, SIGNAL(disconnected()),
this, SLOT(onDisconnected()));
connect(socket, SIGNAL(readyRead()),
this, SLOT(onReadyRead()));
}
void Server::onReadyRead()
{
if(socket->canReadLine())
{
QByteArray ba = socket->readLine();
emit onMessageRecieved(QString(ba));
QByteArray response("maslO!");
// some code which parses arrived message
// and prepares response
socket->write(response);
socket->flush();
}
//else just wait for more data
}
void Server::onDisconnected()
{
printf("Connection disconnected.\n");
disconnect(socket, SIGNAL(disconnected()));
disconnect(socket, SIGNAL(readyRead()));
socket->deleteLater();
}
This is the code. So now the problem description:
I'm starting Server application and then Client application on Raspberry. Both apps are connecting I see it in "onNewConnection" method in Server.cpp.
But when I try to send something to Client using "sendMessage" in server.cpp, Client shows nothing. I should recieve data from server in "readyRead" method in client.cpp but it is never called. What did i wrong?
My friend told me to check if i got same version of QT on both platforms. So i checked and it is not but maybe it is the problem?
Raspberry pi, Arch linux without x server
QMake version 3.0
Using Qt version 5.2.1 in /usr/lib
Server, linux mint
QMake version 3.0
Using Qt version 5.2.0 in /home/adam/Qt/5.2.0/gcc_64/lib
Can you also give me some tips to improve this code?