如何使用QTcpSocket监听qt中的特定端口?

时间:2014-04-08 07:17:17

标签: c++ qt sockets ubuntu

我使用QTcpSocket在两个应用程序之间进行通信。一个是c ++程序,另一个是用PHP编写的网页。

目标是使用套接字将数据从我的网页发送到我的c ++程序。

我不知道如何在特定端口上打开连接,例如12345,如果我有任何数据,请听取它。

到目前为止,我已经编写了以下代码:

MainWindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Mysocket = new QSocket(this);
    Mysocket->Initialization();
}

MainWindow::~MainWindow()
{
    delete ui;
}

QSocket.cpp

#include "qsocket.h"
#define SOCKET_PORT 12345
QSocket::QSocket(QObject *parent) :
    QObject(parent)
{
}


void QSocket::Initialization()
{
    //connected
    socket = new QTcpSocket(this);
    socket->connectToHost("localhost",SOCKET_PORT);
    connect(socket,SIGNAL(connected()),this,SLOT(connected()));
    connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
    connect(socket,SIGNAL(bytesWritten(qint64)),this,SLOT(bytesWritten(qint64)));
    connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    if(!socket->waitForConnected(1000))
    {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::question(0,"Error","Error in Socket Connection",
                                        QMessageBox::Yes|QMessageBox::No);
        socket->close();
    }
    socket80->close();
}
void QSocket::connected()
{
        QMessageBox::StandardButton reply;
        int PortNumber = socket->localPort();
        reply = QMessageBox::question(0,"Connected","Socket Connection is Established",
                                        QMessageBox::Yes|QMessageBox::No);

        qDebug()<<"I'm Listening to Port: "<<PortNumber<<"on the local host \n";
}

void QSocket::disconnected()
{
    qDebug()<<"Disconnected .... \n";
    socket->close();
}

void QSocket::bytesWritten(qint64 bytes)
{
        qDebug()<<"We wrote "<<bytes<<" \n";
}

void QSocket::readyRead()
{
    qDebug()<<"Reading .... \n";
    qDebug()<<socket->bytesAvailable();
    qDebug()<<socket->readAll();
}

当我运行此代码时,它不会打开任何连接并出错,它会进入此if语句。

if(!socket->waitForConnected(1000))
{
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(0,"Error","Error in Socket Connection",
                                    QMessageBox::Yes|QMessageBox::No);
    socket->close();
}

如果我将端口号更改为80,它可以正常工作,并且所有功能都能正常工作。 通过阅读SO中的一些帖子,我意识到解决方案是使用QTcpServer,但我没有任何特定的经验。 这里的问题是什么?!

P.S。

我的平台规格。

Ubuntu 13.10

Qt 5.2.1

1 个答案:

答案 0 :(得分:3)

您可以使用以下示例服务器。您可以通过调用start_listen(int port_no)来开始侦听端口。

#include <QtNetwork>
#include <QMessageBox>

class server : public QTcpServer {
    Q_OBJECT
public:
    explicit server(QObject *parent = 0);
    ~server();
    QTcpSocket server_socket;
public slots:
    void tcpReady();
    void tcpError( QAbstractSocket::SocketError error );
    bool start_listen(int port_no);
protected:
    void incomingConnection( int descriptor );
};

server::server(QObject *parent) : QTcpServer(parent) {
    connect( &server_socket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(tcpError(QAbstractSocket::SocketError)) );
    connect( &server_socket, SIGNAL(readyRead()),
             this, SLOT(tcpReady()) );
    server_socket.setSocketOption(QAbstractSocket::KeepAliveOption, true );
}

server::~server() {
    server_socket.disconnectFromHost();
    server_socket.waitForDisconnected();
}

void server::tcpReady() {
    QByteArray array = server_socket.read(erver_socket.bytesAvailable());
}

void server::tcpError(QAbstractSocket::SocketError error) {
    QMessageBox::warning( (QWidget *)this->parent(), tr("Error"),tr("TCP error: %1").arg( server_socket.errorString() ) );
}

bool server::start_listen(int port_no) {
    if( !this->listen( QHostAddress::Any, port_no ) ) {
        QMessageBox::warning( (QWidget *)this->parent(), tr("Error!"), tr("Cannot listen to port %1").arg(port_no) );
    }
    else
        return true;
}

void server::incomingConnection(int descriptor) {
    if( !server_socket.setSocketDescriptor( descriptor ) ) {
        QMessageBox::warning( (QWidget *)this->parent(), tr("Error!"), tr("Socket error!") );
        return;
    }
}