BeginRead() EndRead() methods with NetworkStream class
-
Hi, iv got a program which has a networkstream for exchangin messages between a server and client...... The problem i had was that the client needs to keep checking the data stream for new messages sent from the server......as im no expert i originally implemented a endless while loop which checked for data in the incoming data buffer but obviously this caused my computer to crash with the number of checks it was doing.......... so i found out about the BeginRead() method which allowed me to create a seperate execution thread using a delegate and callback method......so then i could re call the beginRead() with in the thread so it continously read in formation from the stream............ Now, i dont think i understand how to implement them, as it seems to work one time round and then crashes, or reads data into the buffer that looks like \0\0\0\0\0\0\0\0\0\0\0 (what eva that means)..........i think i need some one to shed some light on these methods so i can get it to work?? cheers sorry for the long message.... mike
-
Hi, iv got a program which has a networkstream for exchangin messages between a server and client...... The problem i had was that the client needs to keep checking the data stream for new messages sent from the server......as im no expert i originally implemented a endless while loop which checked for data in the incoming data buffer but obviously this caused my computer to crash with the number of checks it was doing.......... so i found out about the BeginRead() method which allowed me to create a seperate execution thread using a delegate and callback method......so then i could re call the beginRead() with in the thread so it continously read in formation from the stream............ Now, i dont think i understand how to implement them, as it seems to work one time round and then crashes, or reads data into the buffer that looks like \0\0\0\0\0\0\0\0\0\0\0 (what eva that means)..........i think i need some one to shed some light on these methods so i can get it to work?? cheers sorry for the long message.... mike
mikeyhardingboyo wrote: i originally implemented a endless while loop which checked for data in the incoming data buffer but obviously this caused my computer to crash with the number of checks it was doing That's hardly obvious. I've done a lot of checks in programs and they've never crashed the app or the computer. So have many other developers. It's what applications do. Understand that when you use
BeginRead
a read is started on another thread. The read itself is a blocking call. It does not return until there's data to actually read. If you keep callingBeginRead
and there's never any data, then you've got many threads piling up, wasting memory, and doing nothing. Now all of a sudden when data arrives you've got a race condition. Never use asynchronousBegin*
methods without ever calling their correspondingEnd*
functions. The mess you got into is the mess that will happen when you don't use asynchronous methods correctly. If you want to know if data is available for aNetworkStream
, check theNetworkStream.DataAvailable
property. If you had checked the documentation - especially important if you're new to .NET development - you should've noticed this obviously named method. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
mikeyhardingboyo wrote: i originally implemented a endless while loop which checked for data in the incoming data buffer but obviously this caused my computer to crash with the number of checks it was doing That's hardly obvious. I've done a lot of checks in programs and they've never crashed the app or the computer. So have many other developers. It's what applications do. Understand that when you use
BeginRead
a read is started on another thread. The read itself is a blocking call. It does not return until there's data to actually read. If you keep callingBeginRead
and there's never any data, then you've got many threads piling up, wasting memory, and doing nothing. Now all of a sudden when data arrives you've got a race condition. Never use asynchronousBegin*
methods without ever calling their correspondingEnd*
functions. The mess you got into is the mess that will happen when you don't use asynchronous methods correctly. If you want to know if data is available for aNetworkStream
, check theNetworkStream.DataAvailable
property. If you had checked the documentation - especially important if you're new to .NET development - you should've noticed this obviously named method. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]I do use the DataAvailable property. My point is that when i initially call BeginRead() from my main thread, there is always data to read....thus is returns and allows the main thread to continue.... Thus in the seperate thread that the initial BeginRead call created I want it to block in that thread if there is no data to read, until data becomes available..... Thus I have to have a form of iteration where the BeginRead() is called in the seperate thread so it can continue to check for data as long as the program is running. As the seperate thread is executed i do call the EndRead() on each result of a BeginRead() call. The method that the seperate thread executes looks like this...so are you telling me this is not possible? public void myReadCallBack(IAsyncResult result) { NetworkStream str = (NetworkStream) result.AsyncState; byte[] myReadBuffer = new byte[2048]; int numberOfBytesRead = 0; numberOfBytesRead = str.EndRead(result); myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); TokenizeCommands(myCompleteMessage); myCompleteMessage = ""; stream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream); } mike
-
I do use the DataAvailable property. My point is that when i initially call BeginRead() from my main thread, there is always data to read....thus is returns and allows the main thread to continue.... Thus in the seperate thread that the initial BeginRead call created I want it to block in that thread if there is no data to read, until data becomes available..... Thus I have to have a form of iteration where the BeginRead() is called in the seperate thread so it can continue to check for data as long as the program is running. As the seperate thread is executed i do call the EndRead() on each result of a BeginRead() call. The method that the seperate thread executes looks like this...so are you telling me this is not possible? public void myReadCallBack(IAsyncResult result) { NetworkStream str = (NetworkStream) result.AsyncState; byte[] myReadBuffer = new byte[2048]; int numberOfBytesRead = 0; numberOfBytesRead = str.EndRead(result); myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); TokenizeCommands(myCompleteMessage); myCompleteMessage = ""; stream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream); } mike
I'm not sure I follow you completely. All you need to do is use
BeginRead
once while your main thread of execution continues (to update an animated "wait" graphics or something). In yourAsyncCallback
you callEndRead
as you're doing and you've call the data. Take a look in the .NET Framework SDK documentation at theNetworkStream.BeginRead
andEndRead
methods. The two together form a complete solution (i.e., a callback is created in the docs forBeginRead
that is defined in the example forEndRead
). What you have is similar, but the examples - and the documentation itself - may help you discover what the problem is. Otherwise, please post your complete solution (minus any code that actually acts on the data or updates a UI, etc., for brevity's sake). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
I'm not sure I follow you completely. All you need to do is use
BeginRead
once while your main thread of execution continues (to update an animated "wait" graphics or something). In yourAsyncCallback
you callEndRead
as you're doing and you've call the data. Take a look in the .NET Framework SDK documentation at theNetworkStream.BeginRead
andEndRead
methods. The two together form a complete solution (i.e., a callback is created in the docs forBeginRead
that is defined in the example forEndRead
). What you have is similar, but the examples - and the documentation itself - may help you discover what the problem is. Otherwise, please post your complete solution (minus any code that actually acts on the data or updates a UI, etc., for brevity's sake). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]O, you have helped me understand these methods better. But to clear things up in what im trying to achieve..........The bottom line is I want to be able to read data as it is sent down the stream over time.....Thus my program needs to be able to constantly monitor the stream in-order to react as it were when data(messages) are there to be read off. So in my meain theard after the TCP connection has been set up I call a method which contains the following: if(stream.CanRead) { byte[] myReadBuffer = new byte[2048]; stream.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream); } After this is called the callback method is called (were the seperate thread executes) The code in there looks like this: public void myReadCallBack(IAsyncResult result) { NetworkStream str = (NetworkStream) result.AsyncState; byte[] myReadBuffer = new byte[2048]; int numberOfBytesRead = 0; numberOfBytesRead = str.EndRead(result); myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); while(str.DataAvailable) { str.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream); } if(stream.DataAvailable == false) { TokenizeCommands(myCompleteMessage); myCompleteMessage = ""; str.BeginRead(myReadBuffer, 0, myReadBuffer.Length, new AsyncCallback(myReadCallBack),stream); } } } Thats the code. So im trying to achieve structure that will monitor data when it becomes available on the stream but in an iterative way so the main thread can continue to do other things but react when messages become available on the stream. thanks for ya help, its difficult for me to explain as im a student to this, but im getting there :) cheers mIKE