Unexpected OnReceive event with datagrams if local target not there

Posted on 2010-01-05
Last Modified: 2013-11-20

So I'm trying to get two applications talking on the same computer using the handy MFC CAsyncSocket class, using UDP datapackets.   I'm using this sample, and have emulated the same behavior in my own application:

I derived my own socket class from CAsyncSocket , and here's the behavior in pseudocode, with some sample comm info filled in:

application 1 init:
instantiate socket
socket.Create(4000, SOCK_DGRAM); // create the local socket on port 4000

application 2 init:
instantiate socket
socket.Create(4001, SOCK_DGRAM); // create the local socket on port 4001

application 1 send:
socket.SendTo("hello", 4001, "")  // say hello to other app's port

application 2 receive:
OnReceive event fires

The weird part is that if application 2 is not running when I send the message from application 1, the OnReceive event of *application 1* fires, with a "Connection reset by peer" error.  So four questions:

a) is this behavior by design, or is something funny going on?
b) if this is by design, what's up with that?  I thought datagrams were supposed to be connectionless, and just send a message without regard (or errors) to whether it was heard
c) would this also occur if they were on different machines, or is this some loopback effect?  (I will eventually try this, but would appreciate an insight)
d) is the capital-R "Right" way to handle this (if I don't care if target is running) simply to filter out anything with that error in my sender's OnReceive event?

Thanks very much for any thoughts and help.

(this Q is the spiritual successor to; but no need to read it)

Question by:riceman0
    LVL 17

    Expert Comment

    UDP doesn't have error check on sequence, that doesn't mean it's completely error free and doesn't have any check.

    It's because you are trying it in loopback interface, otherwise you'll not get OnReceive event fired.

    You cannot filter out anything with that error, if connection get closed/data doesn't reached target, you'll get that error. It's important

    Author Comment


    "It's because you are trying it in loopback interface, otherwise you'll not get OnReceive event fired."

    "You cannot filter out anything with that error, if connection get closed/data doesn't reached target, you'll get that error. It's important"

    These statements seem contradictory -- if this error will not occur except on the same machine, then it's not very important.  

    Is there any error if from one machine to another a datagram doesn't reach target?  (In my application I actually don't care if it gets there, I'm sending periodically anyway.)

    LVL 17

    Expert Comment

    It happens to get that error when data transmission failed because it's in loopback interface.

    But you'll get that error if another connection error also occur in remote.

    There is no error if it doesn't reach target. If you care, you can write a piece of code in otherside to send a return back packet when data reached, so I'll know it's reached, if you don't receive such packet/data you'll know it didn't reached.

    That's all.


    Author Comment


    "it happens... because it's in loopback interface"
    "you'll get that error... in remote"

    these seem contradictory to me again; will I get such an error if the other application is on a different machine, or not?

    "There is no error if it doesn't reach target."

    this seems to contradict my observation; I seem to be getting this error because it didn't reach the target.  Note that if the target is running, no error.

    The counterintuitive part of this to me is that I have a socket created at 4000 calling SendTo to a totally different port: 4001.  Why on earth would this cause the OnReceive event of the 4000 socket to fire?  A large proportion of points will go to whoever can help me understand that a little better.

    LVL 7

    Expert Comment

    think the problem is the handy CASyncSocket class which doesn't handle UDP very well. if you won't accept the behavior use pure winsock in a thread which does both send and receive.

    Author Comment


    I can accept this particular behavior just fine, I can just ignore the error.  But... can you expand on "doesn't handle UDP very well"?  If there will be seven more quirks like this one before I'm done and one is bound to be a showstopper, I should just switch to pure sockets right now.
    LVL 17

    Accepted Solution

    CASyncSocket UDP implemention is not so good, some people experience some problems, there is plenties in internet forums. If you can implement it purely using WinSock API using winsock2.h

    You'll be assured. But don't forget that stuff doesn't have Events, you need to handle it all manually using send, recv, etc. API
    LVL 7

    Assisted Solution

    i personally made a wide bow from CAsyncSocket simply from the fact that the docs don't make clear what happens in the UDP case and some other cases. I had to debug a few times implementations using CAsyncSocket always for the same reason that the programmers using that class were fighting against the defaults which are tcp/ip from a standalone client pc not using a firewall or only connecting to a local server. but as i didn't use it myself my point of view could be biased.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now