views:

290

answers:

2

Hello All.

As we know .Net has UdpClient for simple Socket usage for UDP.

Blockquote From MSDN. If you are writing a relatively simple application and do not require maximum performance, consider using TcpClient, TcpListener, and UdpClient. These classes provide a simpler and more user-friendly interface to Socket communications. Blockquote

I am wondering how much performance differences between Raw Socket and UdpClient? I know UdpClient is a wrapper of socket for Udp and it does not have asynchron read/write.

Anything else?

Thanks

A: 

UDP is a connectionless protocol which has zero error-checking, it is that, is the trade-off with TCP, it is faster than TCP. Think of UDP as a fire-and-forget protocol in comparison to TCP, where integrity and checksum is the additional overhead of the TCP over UDP.

RawSocket has no bearing or differences regardless of the protocol in use. A RawSocket is just that, raw, you have to put in the relevant headers such as the source/destination IP address, and to ensure that the headers conform to the relevant protocol. It has nothing to do with the speed of the socket - that will of course depend on the protocol you choose.

Hope this helps, Best regards, Tom.

tommieb75
@tommieb75: the raw socket I mean is like Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.UDP);If I use the statement above to use UDP instead of using Udpclient, what's the performance differences???.Net said there is
Jack
+1  A: 

As the docs say, UdpClient/TcpClient are a thin wrapper on top of Socket class. If all you want to do is send/receive blobs of data, then these classes are good. For this scenario, there is no difference in performance between Socket and UdpClient/TcpClient.

However, Socket does provide a faster way to do IO, in the form of XXXAsync() methods. These methods allow you to do very fast I/O, and are not exposed in TcpClient/UdpClient. That is what the docs mean by "perf difference" - it is that for faster perf, you will have to dig into Socket class and use these methods (XXXAsync).

For an example of the perf differences, see http://ferozedaud.blogspot.com/2009/12/socketsendfile-implementing-fast-file.html which compares the difference in perf between Socket.SendFile and a different implementation using Socket.Send(). Socket.SendFile() is another API that is not exposed in TcpClient.

feroze