473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 6183
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.