Results 1 to 5 of 5

Thread: cannot connect to UDP socket

  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 cannot connect to UDP socket

    I wrote this code to connect to the ip address of my own machine. bind returns true, though connect wouldn't prompt anything. I also run another program, using same ip and port but these two do not connect.

    Qt Code:
    1. void UDP::UDPtInit()
    2. {
    3. socketPort = PORT;
    4. udpsocket = new QUdpSocket(this);
    5. if(!udpsocket->bind(QHostAddress("192.168.111.1"),socketPort))
    6. {
    7. qDebug() << "true";
    8. }
    9. else
    10. {
    11. qDebug() << "false";
    12. }
    13. connect(udpsocket,SIGNAL(readyRead()),this,SLOT(readState()));
    14.  
    15. connect(udpsocket,SIGNAL(connected()),this,SLOT(socketConnected()));
    16.  
    17. connect(udpsocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socketError(QAbstractSocket::SocketError)));
    18.  
    19. emit clientActive(true);
    20. }
    21.  
    22.  
    23. void UDP::socketError( QAbstractSocket::SocketError )
    24. {
    25. qDebug() << "UDPCLIENT ERROR: "<< udpsocket->errorString();
    26. }
    27.  
    28.  
    29. void UDP::socketConnected()
    30. {
    31. qDebug() << "UDPCLIENT : Socket connected!";
    32. emit clientLogMessage(QString("UDPCLIENT : Connected !"));
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: cannot connect to UDP socket

    UDP is a connectionless protocol.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  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: cannot connect to UDP socket

    Server runs sending packets successfully, But Client fails at:
    while(udpsocket->hasPendingDatagrams())
    I have given them same IP and PORT address.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: cannot connect to UDP socket

    UDP has no notion of "client" and "server", only "peers". That aside, peers can't use the same socket, you have to give them different port numbers if they are to use the same IP.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    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: cannot connect to UDP socket

    If the client and server are on different machines then:
    I have given them same IP and PORT address.
    makes no sense. If two machines have the same IP address then your network is misconfigured. If you have two processes on the same machine, say talking through the loop back interface, then they typically would not bind the same UDP port.

    The IP address in bind() is not the address of the other end of a connection, its the address of an interface on the machine running the code. The IP address of the receiver is specified when you call writeDatagram().

Similar Threads

  1. Wait for socket to connect without application hang?
    By hakermania in forum Qt Programming
    Replies: 5
    Last Post: 9th October 2011, 11:03
  2. Replies: 1
    Last Post: 30th September 2011, 15:49
  3. Replies: 2
    Last Post: 22nd May 2011, 21:31
  4. Replies: 2
    Last Post: 9th May 2011, 10:38
  5. Replies: 4
    Last Post: 10th November 2006, 15:38

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.