dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: sendto sending UDP packets twice

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Location
    Edinburgh
    Posts
    20

    sendto sending UDP packets twice

    I have a Windows 7 application that uses sendto to send UDP packets to a DSP.

    Fairly often, the first packet or the first two packets being send out are duplicated. I have confirmed that for each pair of packets,
    I am executing a single sento call.

    WireShark shows them being sent out twice, the only differences being the header and UDP checksums:
    26 14.006331000 192.168.0.4 192.168.0.10 UDP 62 Yes Source port: 9 Destination port: 9
    27 14.006637000 192.168.0.4 192.168.0.10 UDP 62 Yes Source port: 9 Destination port: 9
    28 14.011313000 192.168.0.4 192.168.0.10 UDP 94 Yes Source port: 9 Destination port: 9
    29 14.011392000 192.168.0.4 192.168.0.10 UDP 94 Yes Source port: 9 Destination port: 9

    The short time intervals between the original packet and its copy suggest that this is happening within Windows.

    Any ideas what may be happening here?

  2. #2
    Join Date
    Nov 2012
    Location
    Edinburgh
    Posts
    20

    Re: sendto sending UDP packets twice

    Additional information:

    I have now got it into a state where every call to sendto results in two identical packets being issued (except the final one in the set where extra zeros are added to the ehnd).

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: sendto sending UDP packets twice

    this isn't unusual for UDP since UDP has no flow control.

    you can get duplicate packes for several reasons.
    - the origin is multihomed and a packet is sent out from each source address on the origin (this indicates a flaw in your routing, though it's possible it's unsolvable, or may even be intended)
    - depending on setup, you could be getting a packet over IPV4 and another over IPV6 (this indicates a flaw in your setup)
    - there may be multiple paths from source to target, indicating loops in your routing, again, this is potentially a flaw, or intended.
    - ...

  4. #4
    Join Date
    Nov 2012
    Location
    Edinburgh
    Posts
    20

    Re: sendto sending UDP packets twice

    Thank you for your reply, but I don't see what flow control has to do with it.

    I am sending the packets, and I send them only once each. The connection to the DSP is direct; there are no routers in the way.
    There is only one ARP mapping between the IP address being used and the device's MAC address.

  5. #5
    Join Date
    Mar 2001
    Posts
    2,527

    Re: sendto sending UDP packets twice

    How do you *know* the data being sampled and sent just has simply not changed yet?
    ahoodin
    To keep the plot moving, that's why.

  6. #6
    Join Date
    Nov 2012
    Location
    Edinburgh
    Posts
    20

    Re: sendto sending UDP packets twice

    Quote Originally Posted by ahoodin View Post
    How do you *know* the data being sampled and sent just has simply not changed yet?
    First, no data are being sampled; the contents of the packets being sent come from a file on the PC of fixed and known contents.
    Second, I add a sequence number to each packet being sent; Wireshark shows that number going up in every other packet leaving the PC.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: sendto sending UDP packets twice

    As I said, UDP has no flow control. so on the reciving end you have to be prepared to accept duplicates, handle missing packets and packets arriving out of order.

    This is why TCP was developed, it's basically UDP with flow control, making sure that from the connection POV, you get a continuous, ordered stream. But that comes at a price ofc, that flow control takes CPU effort so TCP is slower.

    there are many reasons for duplicate packets, particularly in UDP unicast. This is how UDP works, nothing much you can do, it even happens in a perfecly correctly configured network, and can happen a lot for various badly configured network issues.

    https://wiki.wireshark.org/DuplicatePackets

  8. #8
    Join Date
    Nov 2012
    Location
    Edinburgh
    Posts
    20

    Re: sendto sending UDP packets twice

    Quote Originally Posted by OReubens View Post
    As I said, UDP has no flow control. so on the reciving end you have to be prepared to accept duplicates, handle missing packets and packets arriving out of order.
    I find it hard to believe that on a network of two processors, directly connected with neither end doing anything except the process in hand, a single call to sendto can validly send the same packet twice.

  9. #9
    Join Date
    Mar 2001
    Posts
    2,527

    Re: sendto sending UDP packets twice

    Just throw out the second packet, you said you have a sequence number, so you know that packet was already processed. Its either that or better yet, you can implement modbus over UDP, or add some other protocol overtop like SCTP over UDP. OReubens is right about this. UDP is the wild wild west of uncontrolled network communications, raw speed and throughput like a wild mustang. Great if you want a fast stream of data such as a live video feed, but not so great if you want flow control and other features, since it is up to you to implement them. UDP definitely has its uses, but without work, precision isn't one of them.

    Have a look at SCTP, there is stuff already out there that you can bolt on. Link to your project and run with it.
    Last edited by ahoodin; August 7th, 2015 at 11:13 AM. Reason: encourgement
    ahoodin
    To keep the plot moving, that's why.

  10. #10
    Join Date
    Jul 2019
    Posts
    2

    Re: sendto sending UDP packets twice

    I'm encountering exactly the same problem in a very similar setup.
    A point to point ethernet cable from the laptop to the embedded platform (Zynq 7030).
    Python is being use to send/receive message on the PC, wireshark on the PC shows all messages from PC --> Embedded platform are duplicated (identical payload, between 2us and 15us difference in time stamps).
    As the packet rate and size goes in, this is going to be a problem for given the data rates, so I'd rather not just 'discard' the unwanted duplicates.

    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.bind(('', 5704))
    while(more_to_send == TRUE)
    <code to populate bytes_to_send>
    s.sendto(bytes_to_send, (HOST, UDP_PORT))
    data = s.recv(1024)

    43204 80.146000 192.168.1.20 192.168.1.10 UDP 566 5704 → 5604 Len=524 (payload)
    43205 80.146003 192.168.1.20 192.168.1.10 UDP 566 5704 → 5604 Len=524 (duplicate at payload time + 3us)
    43206 80.149112 192.168.1.10 192.168.1.20 UDP 48 5604 → 5704 Len=6 (ack)
    43207 80.149306 192.168.1.20 192.168.1.10 UDP 566 5704 → 5604 Len=524 (payload)
    43208 80.149311 192.168.1.20 192.168.1.10 UDP 566 5704 → 5604 Len=524 (duplicate at payload time +5us)

    Was there an conclusion from Peter Robertson?

    Andy

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,923

    Re: sendto sending UDP packets twice

    Yes - See OReubens posts #3, #7. Your UDP receive code has to allow for duplicate and missing packets. This happens all the time with UDP. if this causes a problem, then you'll need to use TCP.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  12. #12
    Join Date
    Jul 2019
    Posts
    2

    Re: [RESOLVED] Receiving duplicate udp messages

    Thanks 2kaud. I guess I'm trying to understand why python would send two identical packets back to back, down the point to point link to the embedded board. The concern is not the ability to cope with it (apart from the used bandwidth), but understanding why it should happen. I'm erring towards thinking it is a python bug.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)