Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpserver help
Forum Updated to NodeBB v4.3 + New Features

QTcpserver help

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GonFreecs
    wrote on last edited by
    #1

    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");
    }

    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Welcome to devnet

      Is the connection between both instances established?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GonFreecs
        wrote on last edited by
        #3

        Thanks,

        No the connection is establish on one instance.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          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

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved