通过QTcpSocket进行双向通信

时间:2014-02-17 17:13:35

标签: c++ qt raspberry-pi qtcpsocket qtcpserver

我正在编写一个通过服务器和客户端之间的QTcpSocket进行通信的应用程序。

服务器是一个在标准电脑上使用gui的应用程序。

客户端是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();
}

这是代码。所以现在问题描述:

我在Raspberry上启动Server应用程序,然后启动客户端应用程序。 两个应用程序都是连接我在&#34; onNewConnection&#34; Server.cpp中的方法。

但是当我尝试使用&#34; sendMessage&#34;向客户发送内容时在server.cpp中,Client不显示任何内容。 我应该在&#34; readyRead&#34;中接收来自服务器的数据。 client.cpp中的方法,但从不调用它。我错了什么?

我的朋友告诉我检查两个平台上是否有相同版本的QT。所以我检查过它不是,但也许是问题?

Raspberry pi,没有x服务器的Arch linux

QMake version 3.0
Using Qt version 5.2.1 in /usr/lib

服务器,linux mint

QMake version 3.0
Using Qt version 5.2.0 in /home/adam/Qt/5.2.0/gcc_64/lib

您能否提供一些改进此代码的提示?

0 个答案:

没有答案