Results 1 to 14 of 14

Thread: QUdpSocket - Send and receive data

  1. #1
    Join Date
    Apr 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QUdpSocket - Send and receive data

    Send a socket to server and this response me immediately (This in php). But in QT, the socket not work...

    Qt Code:
    1. #include "sendreceive.h"
    2. #include "ui_sendreceive.h"
    3. #include "QUdpSocket"
    4. #include "QMessageBox"
    5. #include "QDebug"
    6.  
    7. SendReceive::SendReceive(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::SendReceive)
    10. {
    11. ui->setupUi(this);
    12. }
    13.  
    14. SendReceive::~SendReceive()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void SendReceive::on_enviarsocket_clicked()
    20. {
    21. QMessageBox *mensaje = new QMessageBox(this);
    22. mensaje->setWindowTitle(QString("Socket send"));
    23. mensaje->setText(QString("Socket Send, wait..."));
    24. mensaje->addButton(QString("Ok"), QMessageBox::YesRole);
    25. mensaje->exec();
    26.  
    27.  
    28. QByteArray Data; // Message for send
    29. Data += "SAMP";
    30. Data += QString( 93 );
    31. Data += QString( 119 );
    32. Data += QString( 26 );
    33. Data += QString( 214 );
    34. Data += QString(7777 & 0xFF);
    35. Data += QString(7777 >> 8 & 0xFF);
    36. Data.append("i");
    37.  
    38. socket = new QUdpSocket(this);
    39. int sebindeo = socket->bind(7777);
    40. int envio = socket->writeDatagram(Data, QHostAddress("93.119.26.214"), 7777);
    41.  
    42. qDebug() << "Bind: " << sebindeo << "; Send bytes: " << envio << "; Message send: " << Data;
    43. connect(socket, SIGNAL(readyRead()), this, SLOT(readyReade()));
    44.  
    45.  
    46. }
    47. void SendReceive::readyReade()
    48. {
    49. while (socket->hasPendingDatagrams()) {
    50. QByteArray datagram;
    51. datagram.resize(socket->pendingDatagramSize());
    52. socket->readDatagram(datagram.data(), datagram.size());
    53. qDebug() << "Message receive: " << datagram.data();
    54. }
    55. }
    To copy to clipboard, switch view to plain text mode 

    When press "Ok" i send the socket and the debug: Bind: 1 ; Send bytes: 12 ; Message send: "SAMP]wÖai"

    The server should send me a message: SAMP^5zÙi–+City of Angels RP [Happy Hours do 10 level]CoA RP v11.5.2 Los Santos

    But, the server not send me anything...

    --


    What we seek to do is send a message to a server and immediately read the data this server returns me.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket - Send and receive data

    Do you have any control on the server? Can you confirm that the server receives your datagram and sends back another datagram?

    What does the rest of your program do? Does control return to the event loop after SendReceive::on_enviarsocket_clicked() has finished executing?

    Be careful with memory allocations: your code allocates a new QUdpSocket every time SendReceive::on_enviarsocket_clicked() is executed, and only deallocates them when the SendReceive object itself is destroyed.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,480
    Thanked 275 Times in 270 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket - Send and receive data

    From QUdpSocket::writeDatagram doc : Warning: Calling this function on a connected UDP socket may result in an error and no packet being sent. If you are using a connected socket, use write() to send datagrams. Maybe this is a problem ?

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket - Send and receive data

    Quote Originally Posted by Lesiok View Post
    From QUdpSocket::writeDatagram doc : Warning: Calling this function on a connected UDP socket may result in an error and no packet being sent. If you are using a connected socket, use write() to send datagrams. Maybe this is a problem ?
    I do not think so, because the socket is only bound, not connected.

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    866
    Thanks
    69
    Thanked 58 Times in 56 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket - Send and receive data

    Hi,

    Connect the signal to slot before writing the datagram. Also you can use Wireshark to inspect the network traffic to see if the server is writing the response.

    Also you can resuse the same Udp socket instead of creating it on every send.
    Òscar Llarch i Galán

  6. #6
    Join Date
    Apr 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket - Send and receive data

    Quote Originally Posted by yeye_olive View Post
    Do you have any control on the server? Can you confirm that the server receives your datagram and sends back another datagram?
    I don't have control of the server, but i can use my localhost (127.0.0.1). The server is a game, when this receive a udp socket, this response some info.

    Quote Originally Posted by yeye_olive View Post
    What does the rest of your program do? Does control return to the event loop after SendReceive::on_enviarsocket_clicked() has finished executing?
    This is the rest.
    sendreceive.h
    Qt Code:
    1. #ifndef SENDRECEIVE_H
    2. #define SENDRECEIVE_H
    3.  
    4. #include <QMainWindow>
    5. #include "QUdpSocket"
    6.  
    7. namespace Ui {
    8. class SendReceive;
    9. }
    10.  
    11. class SendReceive : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit SendReceive(QWidget *parent = 0);
    17. ~SendReceive();
    18.  
    19. private slots:
    20. void on_enviarsocket_clicked();
    21. void readyReade();
    22.  
    23. private:
    24. Ui::SendReceive *ui;
    25. QUdpSocket *socket;
    26. };
    27.  
    28. #endif // SENDRECEIVE_H
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "sendreceive.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. SendReceive w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by yeye_olive View Post
    Be careful with memory allocations: your code allocates a new QUdpSocket every time SendReceive::on_enviarsocket_clicked() is executed, and only deallocates them when the SendReceive object itself is destroyed.
    How do i repair this?
    -------

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    Connect the signal to slot before writing the datagram. Also you can use Wireshark to inspect the network traffic to see if the server is writing the response.

    Also you can resuse the same Udp socket instead of creating it on every send.
    I connect the signal before and still same. I don't know wireshark (I go to view this app).

    How can i reuse the socket?

  7. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    866
    Thanks
    69
    Thanked 58 Times in 56 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket - Send and receive data

    Hi

    I don't know wireshark
    Wireshark is a network sniffer that can help you to see the traffic of the network so you can see If the server is sending you anything.
    You also can use the debugger to inspect if "readyReade()" is called.

    How can i reuse the socket?
    You have to create it only once(maybe in the constructor) and then you can call it's methods.
    Òscar Llarch i Galán

  8. #8
    Join Date
    Apr 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket - Send and receive data

    udp.zipHi.

    This is the new sendreceive.cpp. The readyReade() isn't called.

    Qt Code:
    1. #include "sendreceive.h"
    2. #include "ui_sendreceive.h"
    3. #include "QUdpSocket"
    4. #include "QMessageBox"
    5. #include "QDebug"
    6.  
    7. SendReceive::SendReceive(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::SendReceive)
    10. {
    11. ui->setupUi(this);
    12. socket = new QUdpSocket(this);
    13. }
    14.  
    15. SendReceive::~SendReceive()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void SendReceive::on_enviarsocket_clicked()
    21. {
    22. qDebug() << "on_enviarsocket_clicked Call";
    23. QMessageBox *mensaje = new QMessageBox(this);
    24. mensaje->setWindowTitle(QString("Socket send"));
    25. mensaje->setText(QString("Socket Send, wait..."));
    26. mensaje->addButton(QString("Ok"), QMessageBox::YesRole);
    27. mensaje->exec();
    28.  
    29.  
    30. QByteArray Data; // Message for send
    31. Data += "SAMP";
    32. Data += QString( 93 );
    33. Data += QString( 119 );
    34. Data += QString( 26 );
    35. Data += QString( 214 );
    36. Data += QString(7777 & 0xFF);
    37. Data += QString(7777 >> 8 & 0xFF);
    38. Data.append("i");
    39.  
    40. int sebindeo = socket->bind(7777);
    41. connect(socket, SIGNAL(readyRead()), this, SLOT(readyReade()));
    42. int envio = socket->writeDatagram(Data, QHostAddress("93.119.26.214"), 7777);
    43.  
    44. qDebug() << "Bind: " << sebindeo << "; Send bytes: " << envio << "; Message send: " << Data;
    45.  
    46.  
    47. }
    48. void SendReceive::readyReade()
    49. {
    50. qDebug() << "readyReade Call";
    51. while (socket->hasPendingDatagrams()) {
    52. QByteArray datagram;
    53. datagram.resize(socket->pendingDatagramSize());
    54. socket->readDatagram(datagram.data(), datagram.size());
    55. qDebug() << "Message receive: " << datagram.data();
    56. }
    57. }
    To copy to clipboard, switch view to plain text mode 

    Wireshark show the udp with destination and port. udp.zip is the capture of wirseshark

  9. #9
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QUdpSocket - Send and receive data

    readyRead() is never called because your network trace shows that 93.119.26.114 never sends a response back to 192.168.1.2.

    Edit: What is the value of envio after your call to writeDatagram?
    Last edited by jefftee; 15th April 2015 at 05:16.

  10. #10
    Join Date
    Oct 2009
    Posts
    483
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket - Send and receive data

    Although this has no impact on your problem, you should move the connect(socket, SIGNAL(readyRead()), this, SLOT(readyReade())); statement to the end of the constructor.

  11. #11
    Join Date
    Apr 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket - Send and receive data

    The value of envio after call writeDatagram is 12. Is wireshark the message is send, but never response.

    I move the connect and still the same.

    This is the new senderreceive.cpp

    Qt Code:
    1. #include "sendreceive.h"
    2. #include "ui_sendreceive.h"
    3. #include "QUdpSocket"
    4. #include "QMessageBox"
    5. #include "QDebug"
    6.  
    7. int sebindeo;
    8.  
    9. SendReceive::SendReceive(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::SendReceive)
    12. {
    13. ui->setupUi(this);
    14.  
    15. socket = new QUdpSocket(this);
    16. sebindeo = socket->bind(7777);
    17. connect(socket, SIGNAL(readyRead()), this, SLOT(readyReade()));
    18. }
    19.  
    20. SendReceive::~SendReceive()
    21. {
    22. delete ui;
    23. }
    24.  
    25. void SendReceive::on_enviarsocket_clicked()
    26. {
    27. qDebug() << "on_enviarsocket_clicked Call";
    28. /*QMessageBox *mensaje = new QMessageBox(this);
    29.   mensaje->setWindowTitle(QString("Socket send"));
    30.   mensaje->setText(QString("Socket Send, wait..."));
    31.   mensaje->addButton(QString("Ok"), QMessageBox::YesRole);
    32.   mensaje->exec();*/
    33.  
    34.  
    35. QByteArray Data; // Message for send
    36. Data += "SAMP";
    37. Data += QChar( 93 );
    38. Data += QChar( 119 );
    39. Data += QChar( 26 );
    40. Data += QChar( 214 );
    41. Data += QChar(7777 & 0xFF);
    42. Data += QChar(7777 >> 8 & 0xFF);
    43. Data += 'i';
    44.  
    45. int envio = socket->writeDatagram(Data, QHostAddress("93.119.26.214"), 7777);
    46.  
    47. qDebug() << "Bind: " << sebindeo << "; writeDatagram: " << envio << "; Data: " << Data;
    48.  
    49.  
    50. }
    51. void SendReceive::readyReade()
    52. {
    53. qDebug() << "readyReade Call";
    54. while (socket->hasPendingDatagrams()) {
    55. QByteArray datagram;
    56. datagram.resize(socket->pendingDatagramSize());
    57. socket->readDatagram(datagram.data(), datagram.size());
    58. qDebug() << "Message receive: " << datagram.data();
    59. }
    60. }
    To copy to clipboard, switch view to plain text mode 

    In php, with the same message and ip:port the server response.

    this is the php code:

    Qt Code:
    1. <?php
    2. /*
    3.  * Let's generate the string needed for the packet.
    4.  */
    5. $sIPAddr = "93.119.26.214"; // IP address of the server
    6. $iPort = 7777; // Server port.
    7. $sPacket = ""; // Blank string for packet.
    8.  
    9. $aIPAddr = explode('.', $sIPAddr); // Exploding the IP addr.
    10.  
    11. $sPacket .= "SAMP"; // Telling the server it is a SA-MP packet.
    12.  
    13. $sPacket .= chr($aIPAddr[0]); //
    14. $sPacket .= chr($aIPAddr[1]); //
    15. $sPacket .= chr($aIPAddr[2]); //
    16. $sPacket .= chr($aIPAddr[3]); // Sending off the server IP,
    17.  
    18. $sPacket .= chr($iPort & 0xFF); //
    19. $sPacket .= chr($iPort >> 8 & 0xFF); // Sending off the server port.
    20.  
    21. $sPacket .= 'i'; // The opcode that you want to send.
    22. // You can now send this to the server.
    23.  
    24. /**
    25.   * Let's connect now to the server.
    26.   */
    27. $rSocket = fsockopen('udp://'.$sIPAddr, $iPort, $iError, $sError, 2); // Create an active socket.
    28. fwrite($rSocket, $sPacket); //Send data
    29. print_r(fread($rSocket, 1024));
    30. /* fread($rSocket, 11);
    31.  
    32. $aDetails['password'] = ord(fread($rSocket, 1));
    33.  
    34. $aDetails['players'] = ord(fread($rSocket, 2));
    35.  
    36. $aDetails['maxplayers'] = ord(fread($rSocket, 2));
    37.  
    38. $iStrlen = ord(fread($rSocket, 4));
    39.  
    40. $aDetails['hostname'] = fread($rSocket, $iStrlen);
    41.  
    42. $iStrlen = ord(fread($rSocket, 4));
    43. $aDetails['gamemode'] = fread($rSocket, $iStrlen);
    44.  
    45. $iStrlen = ord(fread($rSocket, 4));
    46. $aDetails['mapname'] = fread($rSocket, $iStrlen);
    47.  
    48.  
    49.  
    50.   print_r($aDetails); */ // Get the output from the server
    51.  
    52. fclose($rSocket); // Close the connection
    53. ?>
    To copy to clipboard, switch view to plain text mode 

    the ip is convert to ascii characters
    Attached Images Attached Images

  12. #12
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QUdpSocket - Send and receive data

    Quote Originally Posted by TheMasterNico View Post
    First problem I see is that you're not sending the exact same data to your server. In your PHP example, you send 11 bytes of data. In your QUdpSocket code, you wind up sending 12 bytes of data, because QChar is a 16-bit unicode type and when you add the QChar for 214, it winds up appending two bytes to your QByteArray instead of one.

    I used the code below and got a response from your server. The Wireshark capture for the exchange is shown below as well.

    Qt Code:
    1. QByteArray Data; // Message for send
    2. Data.append("SAMP");
    3. Data.append(93);
    4. Data.append(119);
    5. Data.append(26);
    6. Data.append(214);
    7. Data.append(7777 & 0xFF);
    8. Data.append(7777 >> 8 & 0xFF);
    9. Data.append('i');
    10. int bytes_written = s.writeDatagram(Data, QHostAddress("93.119.26.214"), 7777);
    To copy to clipboard, switch view to plain text mode 



    No. Time Source Destination Protocol Length Info
    1 0.000000000 172.20.28.17 93.119.26.214 UDP 43 Source port: 7777 Destination port: 7777

    Frame 1: 43 bytes on wire (344 bits), 43 bytes captured (344 bits) on interface 2
    Null/Loopback
    Internet Protocol Version 4, Src: 172.20.28.17 (172.20.28.17), Dst: 93.119.26.214 (93.119.26.214)
    User Datagram Protocol, Src Port: 7777 (7777), Dst Port: 7777 (7777)
    Data (11 bytes)

    0000 53 41 4d 50 5d 77 1a d6 61 1e 69 SAMP]w..a.i
    Data: 53414d505d771ad6611e69
    [Length: 11]

    No. Time Source Destination Protocol Length Info
    2 0.194495000 93.119.26.214 172.20.28.17 UDP 138 Source port: 7777 Destination port: 7777

    Frame 2: 138 bytes on wire (1104 bits), 138 bytes captured (1104 bits) on interface 2
    Null/Loopback
    Internet Protocol Version 4, Src: 93.119.26.214 (93.119.26.214), Dst: 172.20.28.17 (172.20.28.17)
    User Datagram Protocol, Src Port: 7777 (7777), Dst Port: 7777 (7777)
    Data (106 bytes)

    0000 53 41 4d 50 5d 77 1a d6 61 1e 69 00 04 00 94 00 SAMP]w..a.i.....
    0010 32 00 00 00 50 72 6f 66 65 73 73 69 6f 6e 61 6c 2...Professional
    0020 20 52 6f 6c 65 70 6c 61 79 20 7c 20 70 72 6f 2d Roleplay | pro-
    0030 72 6f 6c 65 70 6c 61 79 2e 72 6f 20 5b 30 2e 33 roleplay.ro [0.3
    0040 2e 37 20 52 33 5d 12 00 00 00 52 6f 6c 65 70 6c .7 R3]....Rolepl
    0050 61 79 20 31 39 2e 30 2e 33 2e 37 69 0a 00 00 00 ay 19.0.3.7i....
    0060 4c 6f 73 20 53 61 6e 74 6f 73 Los Santos
    Data: 53414d505d771ad6611e6900040094003200000050726f66.. .
    [Length: 106]


    And the attachment here if you prefer:

    pcap.txt

  13. The following user says thank you to jefftee for this useful post:

    TheMasterNico (16th April 2015)

  14. #13
    Join Date
    Sep 2014
    Posts
    94
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QUdpSocket - Send and receive data

    plz send me source it is useful to my application ...

  15. #14
    Join Date
    Apr 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket - Send and receive data

    Quote Originally Posted by mouni View Post
    plz send me source it is useful to my application ...
    Look the previous comments.

Similar Threads

  1. QUdpsocket send and receive broadcast in linux
    By danics in forum Qt Programming
    Replies: 5
    Last Post: 4th February 2015, 11:26
  2. Replies: 1
    Last Post: 13th March 2013, 08:44
  3. QUdpSocket::writeDatagram claims to send but does not
    By oliverg in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2011, 18:00
  4. Replies: 6
    Last Post: 5th February 2011, 20:18
  5. How to send and receive Email ??
    By wincry in forum Newbie
    Replies: 4
    Last Post: 18th October 2009, 17:51

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.