Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. BeginRead() EndRead() methods with NetworkStream class

BeginRead() EndRead() methods with NetworkStream class

Scheduled Pinned Locked Moved C#
sysadminhelptutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikeyhardingboyo
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • M mikeyhardingboyo

      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

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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 calling BeginRead 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 asynchronous Begin* methods without ever calling their corresponding End* 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 a NetworkStream, check the NetworkStream.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]

      M 1 Reply Last reply
      0
      • H Heath Stewart

        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 calling BeginRead 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 asynchronous Begin* methods without ever calling their corresponding End* 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 a NetworkStream, check the NetworkStream.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]

        M Offline
        M Offline
        mikeyhardingboyo
        wrote on last edited by
        #3

        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

        H 1 Reply Last reply
        0
        • M mikeyhardingboyo

          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

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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 your AsyncCallback you call EndRead as you're doing and you've call the data. Take a look in the .NET Framework SDK documentation at the NetworkStream.BeginRead and EndRead methods. The two together form a complete solution (i.e., a callback is created in the docs for BeginRead that is defined in the example for EndRead). 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]

          M 1 Reply Last reply
          0
          • H Heath Stewart

            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 your AsyncCallback you call EndRead as you're doing and you've call the data. Take a look in the .NET Framework SDK documentation at the NetworkStream.BeginRead and EndRead methods. The two together form a complete solution (i.e., a callback is created in the docs for BeginRead that is defined in the example for EndRead). 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]

            M Offline
            M Offline
            mikeyhardingboyo
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups