Results 1 to 3 of 3

Thread: Remake udp (SOCK_DGRAM) socket onto QUdpSocket

  1. #1
    Join Date
    Oct 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Remake udp (SOCK_DGRAM) socket onto QUdpSocket

    I have this code in my C program.
    Now I'm coding on Qt and want to remake this to QUdpSocket.
    Is it really?

    algorythm are here:
    we have ip and port of udp server,
    make data and send it to ip and port of server,
    and waiting answer...
    i can't use bind(ip,port), i don't know recv port, its generates the socket subsystem, and server get it from incoming datagramm.
    i know Qudpsocket needs to be bind on fixed port.
    Qudpsocket is unusable at my use?

    Qt Code:
    1. unsigned char sendbuf[11] = "";
    2. unsigned char recvbuf[DEFAULT_BUFLEN] = "";
    3.  
    4. SOCKET S;
    5. sockaddr_in SA;
    6. int SA_Size = sizeof(SA);
    7.  
    8. int iResult;
    9. int recvbuflen = DEFAULT_BUFLEN;
    10.  
    11. WSAData WSData;
    12. WSAStartup(0x101,&WSData);
    13.  
    14. S = socket(AF_INET,SOCK_DGRAM,0);
    15.  
    16. SA.sin_family=AF_INET;
    17. SA.sin_port=htons(2323);
    18. SA.sin_addr.S_un.S_addr=inet_addr(_ip.c_str());
    19.  
    20. //filling sendbuf here..
    21.  
    22. //sending sendbuf
    23. sendto(S, (char*)sendbuf,sizeof(sendbuf), 0,(struct sockaddr *)&SA, sizeof(SA))
    24.  
    25. //receiving answer
    26. fd_set rfds;
    27. struct timeval tv;
    28. tv.tv_sec = _timeout;
    29. tv.tv_usec = 0;
    30. FD_ZERO(&rfds);
    31. FD_SET(S,&rfds);
    32. if (select((int)S+1, &rfds, NULL, NULL, &tv) > 0)
    33. {
    34. //getting data from
    35. iResult=recvfrom(S, (char*)recvbuf, recvbuflen, 0, (struct sockaddr *)&SA, &SA_Size);
    36. closesocket(S);
    37. WSACleanup();
    38. }
    39. ...
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Remake udp (SOCK_DGRAM) socket onto QUdpSocket

    Have you read the Broadcast Receiver and Broadcast Sender examples?

    What have you tried?
    Last edited by ChrisW67; 2nd November 2010 at 05:27. Reason: Added afterthought

  3. #3
    Join Date
    Oct 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Remake udp (SOCK_DGRAM) socket onto QUdpSocket

    these samples based on binding of fixed port 45454.
    sender and receiver connected to each other by knowing fixed port. how about if another receiver will connect from the same computer? the port is already binded. that is. second receiver doesn't receive anything.


    Added after 1 18 minutes:


    i did it!

    ingredients:

    m_pUdpSocket = new QUdpSocket(this);
    m_pUdpSocket->connectToHost(strHost, nPort);

    method write:

    QByteArray arrBlock;
    QDataStream out(&arrBlock, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_7);
    out << (qint8)0x4C << (qint8)0x4D << (qint8)0x43 << (qint8)0x50 << (qint8)0x00 << (qint8)0x01 << (qint8)0xAA << (qint8)0xBB << (qint8)0x01 << (qint8)0x00 << (qint8)0x00;
    m_pUdpSocket->write(arrBlock);

    and slotReadyRead reacted by m_pUdpSocket, SIGNAL(readyRead()) has received Server answer on generic socket port.

    I watched all exchage in WireShark sniffer.
    Last edited by pethead; 2nd November 2010 at 07:26.

Similar Threads

  1. QUdpSocket: sending and receiving on the same socket?
    By mhoover in forum Qt Programming
    Replies: 16
    Last Post: 17th June 2009, 03:28
  2. UDP - QUdpSocket
    By denwelzie in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2008, 09:02
  3. Elite remake ported to Qt
    By zelko in forum Qt-based Software
    Replies: 0
    Last Post: 17th March 2008, 00:49
  4. QUdpSocket binding
    By db in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2008, 11:24
  5. Socket Programming Port53 QTcpSocket/QUdpSocket
    By patrik08 in forum General Programming
    Replies: 5
    Last Post: 10th May 2007, 16:59

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
  •  
Qt is a trademark of The Qt Company.