locked
Synchronous socket vs. asynchronous socket - performance RRS feed

  • Question

  • I have made a small application that listens for udp data on a specific port. The incoming datagrams are streamed from two different machines at a rate of 500 kB per sec each summing to a total of 1 MB per sec.

    I process the data into different files depending on which host IP the datagram was sent from.

    The actual monitoring of the socket is done in a thread, that constantly checks for data in synchronous mode, blocking the thread until data i received.

    My problem is that the program uses up quite alot of CPU, around 30% on a modern PC. I cannot reason out why it uses this much. I mean, I have seen torrent clients in Java performing much better with much higher speeds.

    I speculate that asynchronous is better suited, but I have no arguments to substatiate this. I mean, an event triggered  when a udp packet is received, is also running in a thread right?

    Is it at all possible to trigger an event on packet received?
    Monday, December 8, 2008 6:42 AM

Answers

  • Socket has quite a number of possible async methods to use. However, for your situation, sync should be fine.

    The only way you might get better socket performance is to use the AsyncCallback form of BeginReceiveMessageFrom; and that's assuming that the Socket implementation will not create an IAsyncResult and will use I/O Completion Ports.

    I would suspect your high CPU usage may be due to other factors. Have you tried timing/profiling other operations by your code?

           -Steve

    • Proposed as answer by Stephen ClearyMVP Wednesday, December 31, 2008 2:57 PM
    • Marked as answer by Hobz Wednesday, February 4, 2009 8:38 AM
    Saturday, December 13, 2008 4:13 AM
  • I found a significant improvement in performance when using asynchronous sockets.

    The result was that I could easily handle 1500 kB/s (3x500 kB of data every second) from three different sources, storing the data in six different files, and displaying data from all three sources in a graph every 100 ms, on a single core processor 1.6 GHz. The max load on CPU was 13% with an average of about half.

    • Marked as answer by Hobz Wednesday, February 4, 2009 8:38 AM
    Wednesday, February 4, 2009 8:37 AM

All replies

  • Socket has quite a number of possible async methods to use. However, for your situation, sync should be fine.

    The only way you might get better socket performance is to use the AsyncCallback form of BeginReceiveMessageFrom; and that's assuming that the Socket implementation will not create an IAsyncResult and will use I/O Completion Ports.

    I would suspect your high CPU usage may be due to other factors. Have you tried timing/profiling other operations by your code?

           -Steve

    • Proposed as answer by Stephen ClearyMVP Wednesday, December 31, 2008 2:57 PM
    • Marked as answer by Hobz Wednesday, February 4, 2009 8:38 AM
    Saturday, December 13, 2008 4:13 AM
  • Thanks for replying.

    I haven't tried that. Is there a profiler in visual studio?
    Monday, December 15, 2008 1:44 PM
  • I found a significant improvement in performance when using asynchronous sockets.

    The result was that I could easily handle 1500 kB/s (3x500 kB of data every second) from three different sources, storing the data in six different files, and displaying data from all three sources in a graph every 100 ms, on a single core processor 1.6 GHz. The max load on CPU was 13% with an average of about half.

    • Marked as answer by Hobz Wednesday, February 4, 2009 8:38 AM
    Wednesday, February 4, 2009 8:37 AM