R
Roger Down
Hi all...
I'm in the process of coding up some "high-speed" asynchronous server. The
server must handle many simultaneous connections. The amount of data to be
transported on each connection is not very big, typically 1000 bytes, but
the server must process the data using the fastest techniques available.
I've had a look on the "Asynchronous Server Socket Example" located here:
http://msdn2.microsoft.com/en-us/library/fx6588te(VS.80).aspx
1) Is this a good starting point regarding how to implement a good async tcp
server ? Anything you would have changed on the connection parts ?
Since the data to be transported is an array of various bytes, I have
changed the StateObject to something like this:
------------------------------------
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Total buffer.
public MemoryStream ms = new MemoryStream();
}
------------------------------------
So in the receive part of the code, I plan to write the bytes read from the
buffer to the memory stream. Something like this:
------------------------------------
state.ms.Write(state.buffer,0,bytesRead);
state.workerSocket.BeginReceive(state.buffer, 0, state.buffer.Length, 0,
receiveCallback, state);
------------------------------------
2 ) Is this a good approach for dynamically add buffer data ?
The total amount of bytes to be received will be made available in the first
part of the byte array, so another possibility would be to allocate a second
buffer. This would make the state object look like this:
------------------------------------
* * *
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Total buffer. Unknown upon class creation.
public byte[] totalbuffer;
}
------------------------------------
So at some point during the first receive, I allocate the total buffer to
the correct size, and uses Buffer.BlockCopy to move data between those two
arrays.
3 ) Could this be an alternative ?
At some point the received data is going to be converted to a byte array
anyway, so perhaps the last suggestion is the best ?
Best of regards...
I'm in the process of coding up some "high-speed" asynchronous server. The
server must handle many simultaneous connections. The amount of data to be
transported on each connection is not very big, typically 1000 bytes, but
the server must process the data using the fastest techniques available.
I've had a look on the "Asynchronous Server Socket Example" located here:
http://msdn2.microsoft.com/en-us/library/fx6588te(VS.80).aspx
1) Is this a good starting point regarding how to implement a good async tcp
server ? Anything you would have changed on the connection parts ?
Since the data to be transported is an array of various bytes, I have
changed the StateObject to something like this:
------------------------------------
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Total buffer.
public MemoryStream ms = new MemoryStream();
}
------------------------------------
So in the receive part of the code, I plan to write the bytes read from the
buffer to the memory stream. Something like this:
------------------------------------
state.ms.Write(state.buffer,0,bytesRead);
state.workerSocket.BeginReceive(state.buffer, 0, state.buffer.Length, 0,
receiveCallback, state);
------------------------------------
2 ) Is this a good approach for dynamically add buffer data ?
The total amount of bytes to be received will be made available in the first
part of the byte array, so another possibility would be to allocate a second
buffer. This would make the state object look like this:
------------------------------------
* * *
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Total buffer. Unknown upon class creation.
public byte[] totalbuffer;
}
------------------------------------
So at some point during the first receive, I allocate the total buffer to
the correct size, and uses Buffer.BlockCopy to move data between those two
arrays.
3 ) Could this be an alternative ?
At some point the received data is going to be converted to a byte array
anyway, so perhaps the last suggestion is the best ?
Best of regards...