4

I'm editing a legacy MFC application, and I have to add some basic network functionalities. The operating side has to receive a simple instruction (numbers 1,2,3,4...) and do something based on that. The clients wants the latency to be as fast as possible, so naturally I decided to use datagrams (UDP).

But reading all sorts of resources left me bugged. I cannot listen to UDP sockets (CAsyncSocket) in MFC, it's only possible to call Receive which blocks and waits. Blocking the UI isn't really a smart. So I guess I could use some threading technique, but since I'm not all that experienced with MFC how should that be implemented?

The other part of the question is should I do this, or revert to TCP, considering reliability and implementation issues. I know that UDP is unreliable, but just how unreliable is it really? I read that it is up to 50% faster, which is a lot for me.

References I used: http://msdn.microsoft.com/en-us/library/09dd1ycd(v=vs.80).aspx

5
  • latency of what? of sending a command or of receiving a response to the command?
    – Serge
    Oct 20, 2012 at 8:52
  • Sending a command. The remote application should start working as soon as it can after issuing the command. Oct 20, 2012 at 9:07
  • Is the order of commands relevant?
    – Serge
    Oct 20, 2012 at 9:10
  • 1
    You may find this thread useful "When is it appropriate to use UDP instead of TCP?" -stackoverflow.com/questions/1099672/…
    – SChepurin
    Oct 20, 2012 at 9:28
  • The order of commands will not crash the application, and they will not be in short periods appart so they can't mix up. However, if a command is lost, the next can get the application in a state that is unwanted at the moment (states are something like play, stop and pause). Still, how to implement UDP listening if I choose that? Oct 20, 2012 at 14:06

3 Answers 3

2

The majority of "latency" in TCP is the handshaking required to set up the initial connection.

If your client app is going to request lots of commands from your MFC app, then TCP is a no brainer. The client opens a TCP connection and keeps it open.

If your MFC app is going to receieve lots of ad-hoc commands from different clients, then UDP may be appropriate, but you have to weigh up whether saving the slight cost of a TCP handshake is worth the possibility that a command may never happen at all due to UDP throwing it away.

4
  • The commands will originate from one place. But the "server" will send them to more clients, at the same time. How slight is the cost? Oct 20, 2012 at 14:12
  • 1
    If you do use TCP, don't forget to disable Nagle's algorithm (via setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, ...) or you'll get ~200mS of delay for each send() call. Oct 20, 2012 at 16:58
  • @JeremyFriesner - Disabling Nagle is important when the server doesn't generate a reply for each send(). In situations where the server is replying to each incoming message it won't make any difference.
    – Roddy
    Nov 19, 2012 at 11:50
  • @Roddy Nagle will make a difference in all cases. Try it for yourself and see. The server's behavior doesn't make a difference because by the time the data gets the server, the client's Nagle algorithm has already added 200mS delay. Nov 29, 2012 at 16:03
0

Thankyou everyone. From everything above, I decided to try with UDP. As for my implementation question, I found an answer myself which helped me a lot, and it looks like it's going to get my communication in order. For someone reading in the future or asking the same, two simple CAsyncSocket wrapper classes:

http://www.codeproject.com/Articles/16581/Sending-Receiving-UDP-Datagrams-with-MFC-s-CAsyncS

0

Why can't you listen to UDP sockets in MFC using CAsyncSocket? I have code that does that. Call Create() with the port number, SOCK_DGRAM, and FD_READ as parameters. OnReceive() should get called automatically. If it doesn't, you can prod it by calling AsyncSelect() with an FD_READ parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.