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. Problem with QUdpSocket

Problem with QUdpSocket

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 13.4k Views
  • 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.
  • S Offline
    S Offline
    soroush
    wrote on last edited by
    #1

    Hi,

    I'm trying to send and receive data to a program on local host (Robocup 2D Soccer Server Simulation program). the QUdpaSocket doesn't bind to server when rcssserver is running, but when it's closed, bind returns true. I also tried to connect using QAbstractSocket::connectToHost, it connects when server is running and I could send messages with write(QByteArray data) but anything is not received from server. (pendingDatagramSize() is -1 and size of QByteArray returned by readAll() is 0 )

    my code is this:

    @
    QUdpSocket socket(this);
    if(socket.bind(QHostAddress::LocalHost,6000)) // returns false when rcssserver in running
    {
    cout << "Connected";
    socket.write(QByteArray("(init urmia-soccer (version 14))"));
    }
    else
    {
    cout << "error";
    }
    @

    and with connect method:

    @
    socket.connectToHost(address,port,QIODevice::ReadWrite);
    if( socket.state() == QAbstractSocket::ConnectedState)
    {
    cout << "Connection stabished" << endl ;
    socket.write(QByteArray("(init teamName (version 14))")); // this works when server is running
    QByteArray b = socket.readAll();
    cout << b.size(); // is always 0. should be at least 256 characters
    cout << "End of reading" << endl ;
    }
    else
    {
    cout << "Error";
    exit(1);
    }
    @

    What I am doing wrong here?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      If you bind a QUdpSocket you are creating a server. As you have already a server running, the bind must fail because only one server can listen on specific tuple of host address and port.

      Your second problem is most probably a race condition. You send your data immediately try to read. It's very likely that there is no data available yet! You must connect signal readyRead of you socket to a slot. See the "docs of QUdpSocket":http://doc.qt.nokia.com/4.7/qudpsocket.html for an example.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • S Offline
        S Offline
        soroush
        wrote on last edited by
        #3

        I changed code to this. But there is no response from server. processData() is never called. but after sending (sense_body) server should return sensor data.
        @
        Connection::Connection(QObject *parent) :
        QObject(parent)
        {
        socket.connectToHost(QHostAddress::LocalHost,6000);
        if(socket.state() == QAbstractSocket::ConnectedState)
        {
        cout << "connection stabished";
        connect(&socket,SIGNAL(readyRead()),this, SLOT(processData()));
        socket.write("(init team-name (version 14));");
        socket.write("(sense_body)");
        // cout << socket.readAll().size(); // if called here, returns 0
        }
        else
        {
        cout << "connection failed";
        }
        }

        void Connection::processData()
        {
        cout << socket.readAll().size(); // is never called.
        }
        @

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          -There is no connect() statement that connects a signal to your slot.-
          (sorry, looked in the wrong place)

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • S Offline
            S Offline
            soroush
            wrote on last edited by
            #5

            Thanks for your reply,

            I tried debuging and found that server emits player disconnection messages immediately after running this line:
            @socket.write("(init team-name (version 14))");@
            but calling socket.state() returns QAbstractSocket::ConnectedState even after server says player is disconnected. Naturally anything is not returned to client when server thinks player is disconnected....

            I think there is a problem with server. that is written in c and c++ completely. it works fine with its own client (rcssclient).

            Any ideas?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Well, we obviously don't know the protocol used by your server, so it is hard to go into that. My cat shattered my last crystal ball, you know...

              What is funny in this, is that you're talking about connecting and disconnecting in the context of UDP. UDP is a connectionless protocol. It just sends datagrams, and does not guarantee that they arrive or arrive in order. There is no concept of a connection (like TCP does offer) in UDP.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                soroush
                wrote on last edited by
                #7

                So,... why server says:
                @
                ...
                A player disconnected : (team-name 1)
                ...
                @
                when I send initialization message? I'm completely confused. :-/

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  How should we know why your server would say that? We have no access to that server or what it does! Perhaps ask the maintainer of that service why it behaves the way it does?

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    [quote author="Andre" date="1302171752"]What is funny in this, is that you're talking about connecting and disconnecting in the context of UDP. UDP is a connectionless protocol. It just sends datagrams, and does not guarantee that they arrive or arrive in order. There is no concept of a connection (like TCP does offer) in UDP. [/quote]

                    I think it's to certain degree because of this paragraph in "QUdpSocket docs":http://doc.qt.nokia.com/4.7/qudpsocket.html":

                    bq. The most common way to use this class is to bind to an address and port using bind(), then call writeDatagram() and readDatagram() to transfer data. If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().
                    [Highlighting by me]

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      That might be it yes. The Qt API is a bit weird in this point.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        [quote author="Andre" date="1302174200"]That might be it yes. The Qt API is a bit weird in this point. [/quote]

                        Once we have the "new stuff":http://developer.qt.nokia.com/blog/view/qt_documentation_our_new_roomieon DevNet we can add a big warning :-)

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          Yep. Already have some places in mind where I'd like to add some thoughts...

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dangelog
                            wrote on last edited by
                            #13

                            [quote author="Andre" date="1302174200"]That might be it yes. The Qt API is a bit weird in this point. [/quote]

                            It's not weird. For decades UNIX has supported connect(2)ing UDP sockets, with well-definite semantics. Qt simply does the same thing.

                            Software Engineer
                            KDAB (UK) Ltd., a KDAB Group company

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on last edited by
                              #14

                              The group of people working with Qt is obviously broader than well-versed unix programmers. I find it weird to talk about connections when you are dealing with a protocol that does not handle connections. I looked it up what it does in Unix. It seems that it does two things:

                              set a default host address where data packages are send to

                              limit receiving datagrams to only those from the host you "connected" to.

                              The QUdpSocket documentation mentions neither, but seems to do something like 1) at least. What's more, it first states that UDP is connectionless, and then in the next paragraph, explains that you need to connect to use certain methods. Excuse me, but I find that weird and inconsistent, no matter what older Unix API may use the same kind of terminology.

                              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