472,801 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,801 software developers and data experts.

BufferedStream class - asynchronous read is blocking ?

Hello,

When using the asynchronous read method in the BufferedStream class, [
i.e. BufferedStream.BeginRead() ], it seems to me that it blocks like
the normal synchronous read method. Why is it so? Why does the
BufferedStream.BeginRead() not behave similar to the
NetworkStream.BeginRead() ??

The following short program illustrates my problem.:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

class BufferedStreamTest
{
private const int BUFFER_SIZE = 64;
private static byte[] buffer = new byte[BUFFER_SIZE];

[STAThread]
static void Main()
{
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Any, 8888));
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(OnAccept), socket);

TcpClient client = new TcpClient();
client.Connect("127.0.0.1", 8888);
// Create a buffered network stream
Stream s = new BufferedStream(client.GetStream());

// Begin the asynchronous read operation
s.BeginRead(buffer, 0, BUFFER_SIZE,
new AsyncCallback(OnRead), s);

Console.WriteLine("Started asynchronous read op..");

/// Just to keep the program running..
Console.ReadLine();
}

public static void OnRead(IAsyncResult result)
{
Stream s = result.AsyncState as Stream;
int bytesRead = s.EndRead(result);
}
public static void OnAccept(IAsyncResult result)
{
Console.WriteLine("Socket connected..");
Socket s = result.AsyncState as Socket;
Socket c = s.EndAccept(result);
}
}
This program blocks when executing the s.BeginRead(..) statement.
However, if I remove the use of the BufferedStream, the s.BeginRead(...)
completes asynchronously..?

Can you please explain to me why this is so ??

thank you,

Niels Johansen
Nov 16 '05 #1
1 6115
ok,

Think I found the answer myself:

http://www.libertyassociates.com/pag...rogCS_edit.htm

quote:
-------
..... Note that the BeginRead and BeginWrite on the Stream class are not
currently asynchronous..
... (FileStream and NetworkStream obviously override these methods and
provide true async behavior, but MemoryStream & CryptoStream do not.) ...
-------

I guess that also applies to the BufferedStream..:)

/niels
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Corne Oosthuizen | last post by:
I'm writing a Telnet Server application using Asynchronous sockets. I spawn a listener thread to handel incomming connections and create a separate client socket for each new connection. I...
3
by: Matthew King | last post by:
Hi all I've written a asynchronous socket client class, but i've found that in order to consume it I have to use events, and cannot simply for example SocketClient client = new...
0
by: Richard | last post by:
Hi, I'm suffering a socket race condition - I think. The code works fine at full speed on a single CPU machine. However when I run full speed on a 2 Zeon machine the socket drops data on an...
1
by: sfoxover | last post by:
Hi, Could someone please give me some suggestions on how to make this class robust. I need to be able to handle around 20 similtanious requests to this class which causes a web browser to...
1
by: foreman | last post by:
Hi there, Hello everybody. I am a newbie to dot net framework class lib. I am confused about those classes such as all of the stream classes and those XXXReader XXXWriter. In fact, I have tried...
2
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
3
by: curt.bathras | last post by:
I am trying to open and read a file using the following: BufferedStream stream = new BufferedStream(File.OpenRead(aFilename)); If the file specified by aFilename is being used by another...
8
by: panko | last post by:
Hello, I can't manage with asynchronous socket communication. :( I wrote a class CSocket.cs. This class is taking care of sending strings to LED display. This display is actually communicating...
6
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.