Results 1 to 8 of 8

Thread: udpsocket cannot bind,

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default udpsocket cannot bind,

    I am running a udpsocket client program which writes to and reads from server. when I run it, bind() returns false. Although I can still send data but I am wondering why sockets cannot bind and trigger readState slot.

    As I checked a udp client does not bind, so I only set the port when initialized it. later I write like this:

    Qt Code:
    1. void UDP::UDPInit(int port)
    2. {
    3. socketPort = port;
    4. udpsocket = new QUdpSocket(this);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void UDP::sendCommand(QByteArray data)
    2. {
    3. if(udpsocket->writeDatagram(data.data(),QHostAddress(ip),socketPort)==-1)
    4. emit clientLogMessage(QString("UDPCLIENT : Write problem !"));
    5. else
    6. udpsocket->flush();
    7.  
    8. // QByteArray datagram;
    9. // int m = udpsocket->read(datagram.data(), 11);
    10. // qDebug() << errno;
    11. // qDebug() << m;
    12.  
    13. while (!udpsocket->hasPendingDatagrams()) {
    14. QByteArray datagram;
    15. datagram.resize(udpsocket->pendingDatagramSize());
    16. qDebug() << udpsocket->pendingDatagramSize();
    17. udpsocket->readDatagram(datagram.data(), datagram.size());
    18. emit dataReceived(datagram);
    19. }
    20. }
    21.  
    22. but the result is -1 and it does not read anything from it. WHY? how can I solve this problem?
    To copy to clipboard, switch view to plain text mode 
    Last edited by saman_artorious; 23rd October 2013 at 13:27.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: udpsocket cannot bind,

    Your code extracts do not call bind(): you will not receive if you do not call bind.
    What "result" is -1?

    writeDatagram() expects either a QByteArray, address, and port OR a char* buffer, size, address, and port. Which do you think you are doing? Are you ignoring a compiler warning?

    Your code starting at line 13 says, "While I have no pending datagrams try to read a datagram." Does that sound right to you?

  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: udpsocket cannot bind,

    Quote Originally Posted by ChrisW67 View Post
    Your code extracts do not call bind(): you will not receive if you do not call bind.

    I added bind(), but still it returns false! I can only bind my own ip address and not the other machines.

    What "result" is -1?

    writeDatagram() expects either a QByteArray, address, and port OR a char* buffer, size, address, and port. Which do you think you are doing? Are you ignoring a compiler warning?

    no I am not missing any compiler warning. I write char* buffer as the first buffer to writeDatagram(), write is successful, the other machine receives packets successfully.


    Your code starting at line 13 says, "While I have no pending datagrams try to read a datagram." Does that sound right to you?
    Yes, you are right, I shall omit ! sign.

    Here is my final code but still I bind returns false and I cannot receive any packets! (my ip is 100.147 and the machine I am connecting is 100.143)

    Qt Code:
    1. #include "UDP.h"
    2.  
    3. UDP::UDP(QObject *parent) :
    4. QObject(parent)
    5. {
    6.  
    7. }
    8. //=======================================
    9. void UDP::UDPInit(int port)
    10. {
    11. socketPort = port;
    12. host = new QHostAddress("192.168.100.143");
    13. myPort = 11000;
    14. udpsocket = new QUdpSocket(this);
    15.  
    16. bool res = udpsocket->bind(QHostAddress("192.168.100.143"),myPort);
    17. connect(udpsocket,SIGNAL(connected()),this,SLOT(socketConnected()));
    18. connect(udpsocket, SIGNAL(readyRead()), this, SLOT(readSocket()));
    19. }
    20. //=======================================
    21. void UDP::socketError( QAbstractSocket::SocketError )
    22. {
    23. qDebug() << "UDPCLIENT ERROR: "<< udpsocket->errorString();
    24. }
    25.  
    26. //========================================
    27. void UDP::socketConnected()
    28. {
    29. qDebug() << "UDPCLIENT : Socket connected!";
    30. emit clientLogMessage(QString("UDPCLIENT : Connected !"));
    31. }
    32.  
    33.  
    34. void UDP::sendCommand(QByteArray data)
    35. {
    36. if(udpsocket->writeDatagram(data.data(),QHostAddress(ip),socketPort)==-1)
    37. emit clientLogMessage(QString("UDPCLIENT : Write problem !"));
    38. else
    39. udpsocket->flush();
    40. }
    41.  
    42. void UDP::readSocket()
    43. {
    44. while (udpsocket->hasPendingDatagrams())
    45. {
    46. QByteArray datagram;
    47. datagram.resize(udpsocket->pendingDatagramSize());
    48. qDebug() << udpsocket->pendingDatagramSize();
    49. udpsocket->readDatagram(datagram.data(), datagram.size(), host, &myPort);
    50. emit dataReceived(datagram);
    51.  
    52.  
    53. qDebug() << datagram.toHex();
    54. }
    55. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: udpsocket cannot bind,

    Your bind() call is asking to receive packets at port 11000 on the other machine. That will be difficult for this machine to do.

    You are still sending writeDatagram() invalid arguments.

  5. #5
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: udpsocket cannot bind,

    Quote Originally Posted by ChrisW67 View Post
    Your bind() call is asking to receive packets at port 11000 on the other machine. That will be difficult for this machine to do.

    You are still sending writeDatagram() invalid arguments.
    Do you mean I shall try other ports to find a suitable one? I also tried 1234 but it failed. I need more explanation is needed please.
    About the datagram the prototype is as following:
    qint64 QUdpSocket::writeDatagram(const QByteArray & datagram, const QHostAddress & host, quint16 port)
    and
    qint64 QUdpSocket::writeDatagram(const char * data, qint64 size, const QHostAddress & address, quint16 port)

    well this time I used used the first prototype instead of the second:

    Qt Code:
    1. if(udpsocket->writeDatagram(data,QHostAddress(ip),myPort)==-1)
    To copy to clipboard, switch view to plain text mode 

    Edit: I tested the program with different port addresses, still I cannot bind() to server, though I can write packet to server successfully, with not packets receipt from the other side.

    listen here please: I change the ip and bind() to my own machine, in this case I can now read from the other machine, but I cannot write to it anymore. I tried to define two sockets, one for writing and one for reading, but still I cannot write to the other machine. I am confused totally, don't know how to solve this.
    Last edited by saman_artorious; 26th October 2013 at 13:11.

  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: udpsocket cannot bind,

    Can you post an update code?

    Check that the address you use for bind is one of your local machine and the one you use for sending is one of the remote machine.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    saman_artorious (27th October 2013)

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: udpsocket cannot bind,

    What is the value of the variable 'ip'? How is that related to the value of variable 'host' or the remote host? Where is it defined and set?

    Your original code was close... One socket can handle the whole conversation.

  9. #8
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: udpsocket cannot bind,

    Quote Originally Posted by anda_skoa View Post
    Can you post an update code?

    Check that the address you use for bind is one of your local machine and the one you use for sending is one of the remote machine.

    Cheers,
    _
    Yes, you are right, I mentioned the server address in bind(), I shoudav mentioned the local machine address instead. And as chris said, we I needed bind() in order to activate the ReadyRead() signal to receive packets.

    Cheers,

Similar Threads

  1. Replies: 3
    Last Post: 4th September 2010, 22:48
  2. Broadcast string with udpsocket
    By wirasto in forum Qt Programming
    Replies: 5
    Last Post: 23rd September 2009, 11:13
  3. UdpSocket bind warning--pls have a look
    By swamyonline in forum Qt Programming
    Replies: 0
    Last Post: 28th August 2008, 11:42
  4. How to destroy a UdpSocket completely
    By ms20020048 in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2006, 19:01

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.