?
Solved

Unexpected OnReceive event with datagrams if local target not there

Posted on 2010-01-05
8
Medium Priority
?
412 Views
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: http://support.microsoft.com/kb/214396

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, "127.0.0.1")  // 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 http://www.experts-exchange.com/Programming/Languages/CPP/Q_25015367.html; but no need to read it)



0
Comment
Question by:riceman0
  • 3
  • 3
  • 2
8 Comments
 
LVL 17

Expert Comment

by:CSecurity
ID: 26183603
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
0
 

Author Comment

by:riceman0
ID: 26184674

"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.)

0
 
LVL 17

Expert Comment

by:CSecurity
ID: 26185497
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.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:riceman0
ID: 26185769

"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.

0
 
LVL 7

Expert Comment

by:js-profi
ID: 26189365
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.
0
 

Author Comment

by:riceman0
ID: 26190796

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.
0
 
LVL 17

Accepted Solution

by:
CSecurity earned 1000 total points
ID: 26191130
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
0
 
LVL 7

Assisted Solution

by:js-profi
js-profi earned 1000 total points
ID: 26191295
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.
Suggested Courses

850 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