Ok I want to connect to a Socket and read a network stream using the System.Net.Sockets.NetworkStream class. This is what I have so far:
NetworkStream myNetworkStream;
Socket socket;
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
ProtocolType.IPv4);
socket.Connect(IPAddress.Parse("8.8.8.8"), 8888);
myNetworkStream = new NetworkStream(socket);
byte[] buffer = new byte[1024];
int offset = 0;
int count = 1024;
myNetworkStream.BeginRead(buffer, offset, count, ??, ??);
Now I need an AsyncCallback and an Object state to complete my BeginRead method but I’m not even sure if this is going to work. I’m a bit lost at this point! Where do I need to go from here?
Basically, when you call the
Begin*method on an asynchronous operation, there needs to be a call to a correspondingEnd*statement (for more detailed information, see the Asynchronous Programming Overview on MSDN, specifically the section titled “Ending an Asynchronous Operation”).That said, you generally want to pass a method/anonymous method/lambda expression which will do one or two things:
1) Call the corresponding
End*method, in this case,Stream.EndRead. This call will not block when called because thecallbackwill not be called until the operation is complete (note that if an exception occurred during the async call then this exception will be thrown when theEnd*method is called).2) Possibly start a new asynchronous call. In this case, it’s assumed you’ll want to read more data, so you should start a new call to
Stream.BeginReadAssuming you want to do #2, you can do the following:
However, if you are using .NET 4.0, this becomes much easier using the
FromAsyncmethod on theTaskFactoryclass.