QTcpserver help



  • Hello Anyone,

    I am designing a game that each user will execute the exact program. I would like to have a connection where both users can send and receive updates from one another. When I execute my program I can receive update from the other user however I am not receiving them. Basically a one way connection. Any ideas?

    @
    #include "server.h"
    #include "scene.h"

    server::server(QWidget *parent) :
    QWidget(parent)
    {
    //open login menu
    ui = new login(this);
    ui->exec();

    //determine if player one or two
    playerNumber = ui->playerNumber;
    
    qDebug() << "server.cpp entered";
    
    //server for current game
    serverBl = new QTcpServer(this);
    tcpsocket = new QTcpSocket;
    sendSocket = new QTcpSocket;
    serverBl->setMaxPendingConnections(1);
    
    if(!serverBl->listen(QHostAddress::Any,ui->Port->toInt()))
    {
        QMessageBox::critical(this,"Connection Error!",serverBl->errorString());
        //create message stating that connect fail
    }
    
    //if there is pending connect send to be processed
    connect(serverBl,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
    
    sendSocket->connectToHost(ui->IP->toLatin1(),ui->Port->toInt());
    

    }

    void server::acceptConnection()
    {
    //ready for next connection
    tcpsocket = serverBl->nextPendingConnection();

    //check to see if socket is valid
    if(!tcpsocket->isValid())
    {
        QMessageBox::critical(this,"Connection Error !",tcpsocket->errorString());
    }
    else
    {
        //inform user that connection is valid
        qDebug() << "Connection is valid ! Connection established to player 2";
    }
    
    //send to socket to be process
    connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(receiveData()));
    

    }

    void server::sendPosData(float pos)
    {
    QString temp = QString::number(pos);

    qDebug() << "data sending: " << temp;
    
    sendSocket->write(temp.toLatin1());
    

    }

    void server::receiveData()
    {
    QByteArray instantMessage = tcpsocket->readLine();

    if(instantMessage.toFloat())
    {
        emit opponentPos(instantMessage.toFloat());
    }
    else
        emit opponentFire();
    

    }

    void server::sendFire()
    {
    qDebug() << "send fire";
    sendSocket->write("fire");
    }

    @


  • Moderators

    Welcome to devnet

    Is the connection between both instances established?



  • Thanks,

    No the connection is establish on one instance.


  • Moderators

    Assuming that you are using the same classes on both sides.

    In lines 18, 19 you are assigning two sockets as below.
    @
    tcpsocket = new QTcpSocket;
    sendSocket = new QTcpSocket;
    @

    later on
    @
    void server::acceptConnection()
    {
    //ready for next connection
    tcpsocket = serverBl->nextPendingConnection();
    @

    this is over writing the socket and creating a memory leak as far as I see.

    QTcpServer delivers you a pointer to a QTcpSocket for use when another client is connecting. This is your pointer then for communicating to the client connected.

    Certainly you may setup two sockets, but that is not required. Depending on which application is started first, the other may connect with a client. Both applications are already connected and no additional connection required in addition.

    Essentially I would propose having only one pointer to a QTcpSocket on each side. This can be assigned to an object in the constructor. When an in-coming connection is reported you have to delete this, when it is not connected. You need some mechanism for checking who is connecting to whom.

    You may want to check the functionality of the fortune "client":http://qt-project.org/doc/qt-5/qtnetwork-fortuneclient-example.html and "server.":http://qt-project.org/doc/qt-5/qtnetwork-fortuneserver-example.html


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.