Results 1 to 6 of 6

Thread: UDP Network. receiving pakets

  1. #1
    Join Date
    Apr 2015
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up UDP Network. receiving pakets

    I am trying to write a programm that communicates with a server over the network. the server implementation is given, i can not change it.

    the servers IP address is the 141.47.69.35 and the server aplication listens to port 11111. the server answers to the source port of the request. as a protocoll udp is used. the example code works fine for transmitting data to the server. with a network sniffer i can see that the server responds correcly. but the function readPendingDatagrams() is never called. i gues there is anything wrong with the ports, but i really have no more idea...

    Qt Code:
    1. #include "cmyudp.h"
    2. #include "iostream"
    3.  
    4. using namespace std;
    5.  
    6. MyUDP::MyUDP(QObject *parent) : QObject(parent)
    7. {
    8. // initialize port
    9. port = 11111;
    10.  
    11. // create a QUDP socket
    12. udpReceiveSocket = new QUdpSocket(this);
    13.  
    14. // creat host ip
    15. hostIp = new QHostAddress("141.47.69.35");
    16.  
    17. // bind socket
    18. udpReceiveSocket->connectToHost(*hostIp, port, QIODevice::ReadWrite);
    19.  
    20. // setup receiver calllback function
    21. connect(udpReceiveSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()))
    22.  
    23. }
    24.  
    25. void MyUDP::HelloUDP()
    26. {
    27. // create send data
    28. QByteArray Data;
    29. Data.append("Hallo Hilfe!");
    30. udpReceiveSocket->write(Data);
    31. }
    32.  
    33. void MyUDP::readPendingDatagrams()
    34. {
    35. cout << "received!" << endl;
    36. while (udpReceiveSocket->hasPendingDatagrams()) {
    37. cout << "received!" << endl;
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: UDP Network. receiving pakets

    You are missing the bind() call.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2015
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: UDP Network. receiving pakets

    Thanks for your response.
    Is it possible to mix bind() and connectToHost()? As i understood either the one or the other should be used. Where is the bind() missing? i also tryed solutions only with bind() and the writeDatagram() functions. the result was the same: receiving a paket didn't work.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: UDP Network. receiving pakets

    bind() is needed if you want your UDP socket to listen for data.

    connectToHost() is just a convenience for not having to specifiy the receiver when sending packets. I doubt it would conflict with bind().

    "Connect" doesn't have any inherent meaning for UDP, the function is in QUdpSocket because it is in its base class and the developers decided to make it do something useful (allowing to you use write without specifying the receipient) instead of it just doing nothing (or generating an error),

    Cheers,
    _

  5. #5
    Join Date
    Apr 2015
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: UDP Network. receiving pakets

    OK, i think i got this. How can i bind a socket to the port that is used as a source port for transmitting? The transmitter picks its source port randomly, thats fine so far. But how do i tell the socket that it has to listen to that randomly choosen port? It would not be a Problem if the Server response would go to a defined port at client side. But i have to listen to a port that i actually don't know when calling bind().

    Thanks for your help.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: UDP Network. receiving pakets

    Just to be sure: have you checked if binding to a port makes this also the port used for sending?

    If not, you could try connectToHost() and then accessing localPort() for the value to bind()

    Cheers,
    _

Similar Threads

  1. Receiving Streaming frames
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 25th May 2012, 09:14
  2. Receiving reply from page
    By Trok in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2011, 12:31
  3. receiving XButtonEvent but not QMouseEvent
    By vivek.m in forum Qt Programming
    Replies: 0
    Last Post: 17th August 2010, 12:46
  4. mouseMoveEvent receiving zero's
    By JohnTh in forum Newbie
    Replies: 2
    Last Post: 11th September 2009, 17:31
  5. Function not receiving QPixmap
    By nmuntz in forum Qt Programming
    Replies: 14
    Last Post: 3rd February 2009, 00:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.