Hi. For the first time since I started working with .NET, I need to write some TCP/IP code. While I only need to connect to a server socket, send one handshake packet and wait for incoming data, I started having very dim thoughts about high-performance communication servers in .NET .
First there's the problem of packing binary data into a byte[] ready to be sent to the network. I want to take a 32 bit integer and put it in offest 50 of a byte[] array (which is my outgoing message). Naturally, I can do this: BitConvert.GetBytes(IPAddress.HostToNetworkOrder(myInt)).CopyTo(msg, 50); What this does (unless I'm missing something) is allocate a 4-byte array, put the number in it, copy the 4 bytes to my outgoing message buffer and, when it's time for garbage collection, free the 4-byte array. This isn't the most efficient way to accomplish this, and I'm being polite. If BitConvert offered a GetBytes(int, byte[], offset) method, it could have worked a lot faster (and I do mean A LOT). This is the smaller problem of the two. I can write an unsafe version of BitConvert that will spare the extra memory allocation and copy. The bigger problem I encountered was waiting for incoming socket events. My application waits for two kinds of events - socket events and another WaitHandle that is set when there's some other work to do. To make everything work, I call BeginReceive/BeginConnect/BeginSomething on my socket, and in the main loop I WaitHandle.WaitAny(...) on my handles (event and last socket operation handle). This works, but generates memory allocations inside BeginReceive. I also need to allocate my WaitHandle[] array every time, because sometimes I wait for 2 handles and sometimes just for 1. I could allocate two arrays in advance and use the appropriate one, but it will turn ugly if the number of handles I wait on varies... While the performance impact on my application is negligible, this sort of overhead can be a killer for a high-performance server application. Do server developers need to resort to Windows API to use IOCPs and control when memory is allocated using the unmanaged heap (basically saying C++ is the way to go and not C#)? Thanks, Itay. <html><body><center><hr><b><font face=arial size=2>Visit the Tel Aviv Stock Exchange's Website<a href=http://www.tase.co.il> www.tase.co.il</a></b></body></html> =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com
