Microsoft .NET Framework >> Looking for good c# sample NetworkStream BeginRead/EndRead...

by Kyle Jedrusiak » Thu, 01 Jul 2004 01:19:33 GMT

Need the correct usage pattern for something like a chat program.

Don't want any threading issues or stack problems.

Kyle!



Microsoft .NET Framework >> Looking for good c# sample NetworkStream BeginRead/EndRead...

by Supra » Thu, 01 Jul 2004 03:44:24 GMT


www.vbip.com

Similar Threads

1. NetworkStream / BeginRead / EndRead / Dataavailable: can't understand MSDN code sample

Hi,

I need to asynchronously read from a network (TCP) stream, and I am
having trouble with retrieving whole blocks; I get a break in the data
block every 1460 bytes which relates to network packet sizes I guess.

To get this fixed, I am trying to follow an example in the MSDN, which
pretty much resembles what I want to do. Since the code (I can't do
this in a static class for example) is not 100% what I need, I
certainly would like to understand what I am doing, but I guess I am
lacking some fundamentals of asynchronous programming since I simply
do not understand the code snippet. Here it is:


public static void myReadCallBack(IAsyncResult ar ){

    NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState;
    byte[] myReadBuffer = new byte[1024];
    String myCompleteMessage = "";
    int numberOfBytesRead;

    numberOfBytesRead = myNetworkStream.EndRead(ar);
    myCompleteMessage = 
        String.Concat(myCompleteMessage,
Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
    
    // message received may be larger than buffer size so loop through
until you have it all.
    while(myNetworkStream.DataAvailable){
        
        myNetworkStream.BeginRead(myReadBuffer, 0,
myReadBuffer.Length,
                                                   new
AsyncCallback(NetworkStream_ASync_Send_Receive.myReadCallBack),
                                                   myNetworkStream);  

    }

    // Print out the received message to the console.
    Console.WriteLine("You received the following message : " +
                                myCompleteMessage);
}


I wonder, how myCompleteMessage can ever add up to the complete
message, since it will be reinitialized at every callback, won't it?

If somebody can help me to understand this (what happens
"while(myNetworkStream.DataAvailable){...}") , I would be most
grateful.

Best regards

DC

2. asynchronous programming model with BeginRead...EndRead - CSharp/C#

3. networkstream.beginread, socket error, no callback

Hi,

i was testing a network application that i've been working on which use .net
socket components. To read from a socket i use the networkstream beginread.
If the other side dies, beginread calls the callback delegate. Today i was
testing this in my old laptop (celeron 553MHz) and verify an interesting
thing. Callback is not called and this exception is captured by visual
studio.

An unhandled exception of type 'System.NullReferenceException' occurred in
system.dll

Additional information: Object reference not set to an instance of an
object.


Unhandled Exception: System.NullReferenceException: Object reference not set
to an instance of an object.
   at System.Net.OSSOCK.WSAGetOverlappedResult(IntPtr socketHandle, IntPtr
overlapped, UInt32& bytesTransferred, Boolean wait, IntPtr ignored)
   at System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback(UInt32
errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)

Therefore, i assume there is a bug in beginread, usually hidden by the cpu
speed or so.

Who knows the why and suggest me the how?

Manuel


4. NetworkStream.BeginRead and Forms threading - CSharp/C#

5. NetworkStream EndRead SocketException OperationAborted

I have a .NET 2.0 application that acts as a TCP/IP server, and Ie 
discovered an issue with NetworkStream.  Ie accepted a client socket and 
called steam.BeginRead( and my AsyncCallback has been called, so it calls 
EndRead.  EndRead occasionally throws an IoException that has an 
InnerException of SocketException that has a SocketErrorCode of 
OperationAborted.  The MSDN documentation says that OperationAborted means 
the socket is closed, and the socket IsConnected does indeed indicate false.

But I handle the exception and call stream.BeginRead(..) again and it works 
fine and I continue to have a conversation with my client like nothing ever 
happened.  The client sends me a message every 15 seconds, and I reply OK.  
Additionally, I have a 1 second timer and I can continue to send unsolicited 
messages to the client every second.
 
Running in the debugger, I notice that I always get an Output Window message 
of he thread 0x1234 has exited with code 0 (0x0).just before I get the 
exception with the OperationAborted.  I not terminating any threads in my 
code.

Why does EndRead throw that exception that indicates OperationAborted?
How should I handle this exception?  I could close the socket, but that 
basically means that my application isn servicing clients.
Why is the NetworkStream and the Socket still connected and working?

Thanks,
John

6. NetworkStream.BeginRead Blocking problem

7. NetworkStream.BeginRead() wierdness

I have a class that wraps a TcpClient object and manages all the async
reading of the socket.  It works really nice, and I use it all over the
place.  But there's this ONE INSTANCE where I create one of these things and
it WON'T read data.  If I set a breakpoint in my EndRead callback, it never
goes off.  NOTHING is different from anywhere else I use this class, its
just this one place.

Now, if I create a second constructor for my class that DOESN'T start off
the async reading process (basically, never calls BeginRead()), subsequent
Read()s will receive data.  Additionally, if I do a loop like this:

while(!networkStream.DataAvailable)
   Sleep(250);

networkStream.BeginRead(...)

Then the class works like it does everywhere else.  Its like, in this one
instance (and this instance isn't any different than anywhere else I use
this class), I can't just do a BeginRead() and wait for data to come in.  I
have to loop and check DataAvailable before calling BeginRead().

Any suggestions?


8. NetworkStream BeginRead & callback invocation - .Net Framework