473,406 Members | 2,343 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,406 software developers and data experts.

asynchronous networkstream to filestream (vice versa)

Hi all, I have a client/server app. They both use asynchronous calls when receiving data. its build with TCP and is meant, primarily for sending files.

A command is sent along a socket which is then 'converted' into and action with a simple switch case. If the client send the command "SENDFILE" I want the server to be able to enter a case which calls a function which then handles any further data along that socket and combines it into a file.

This is OnDataReceive callback function and switch case on the server side:

Expand|Select|Wrap|Line Numbers
  1.  public void OnDataReceived(IAsyncResult asyn)
  2.         {
  3.             SocketData socketData = (SocketData)asyn.AsyncState;
  4.             try
  5.             {
  6.  
  7.                 // Complete the BeginReceive() asynchronous call by EndReceive() method
  8.                 // which will return the number of characters written to the stream 
  9.                 // by the client
  10.                 socketData.m_currentSocket.EndReceive(asyn);
  11.  
  12.                 //Get packet dtata
  13.                 Packet returnPacket = Helper.ByteArrayToPacket(socketData.dataBuffer);
  14.  
  15.                 switch (returnPacket.command)
  16.                 {
  17.                     case Command.SENDREQUEST: //Get ready for an incoming file

Here the class that reads from a network stream asynchronously and write to a filestream synchronously:
Is that right?

Expand|Select|Wrap|Line Numbers
  1.         public static void NetToFile(NetworkStream net, FileStream file)
  2.         {
  3.             var copier = new AsyncStreamCopier(net, file);
  4.             copier.Start();
  5.         }
  6.  
  7. public class AsyncStreamCopier
  8.     {
  9.         public event EventHandler Completed;
  10.  
  11.         private readonly Stream input;
  12.         private readonly Stream output;
  13.  
  14.         private byte[] buffer = new byte[Settings.BufferSize];
  15.  
  16.         public AsyncStreamCopier(Stream input, Stream output)
  17.         {
  18.             this.input = input;
  19.             this.output = output;
  20.         }
  21.  
  22.         public void Start()
  23.         {
  24.             GetNextChunk();
  25.         }
  26.  
  27.         private void GetNextChunk()
  28.         {
  29.             input.BeginRead(buffer, 0, buffer.Length, InputReadComplete, null);
  30.         }
  31.  
  32.         private void InputReadComplete(IAsyncResult ar)
  33.         {
  34.             // input read asynchronously completed
  35.             int bytesRead = input.EndRead(ar);
  36.  
  37.             if (bytesRead == 0)
  38.             {
  39.                 RaiseCompleted();
  40.                 return;
  41.             }
  42.  
  43.             // write synchronously
  44.             output.Write(buffer, 0, bytesRead);
  45.  
  46.             // get next
  47.             GetNextChunk();
  48.         }
  49.  
  50.         private void RaiseCompleted()
  51.         {
  52.             if (Completed != null)
  53.             {
  54.                 Completed(this, EventArgs.Empty);
  55.             }
  56.         }
  57.     }
The problem im having is doing the opposite, reading from a filestream to a networkstream, should I read from the filestream asynchronously and write synchronously to the network stream?

also because with the above example, the first time Start() is called and the function ends it goes back to the server switch case and any further (file)data is then hitting the
Expand|Select|Wrap|Line Numbers
  1. Packet returnPacket = Helper.ByteArrayToPacket(socketData.dataBuffer);
and erroring :(

I previously had a synchronous method of doing it using while loops but that takes up a lot of CPU and im trying to prevent that.

Could someone point me in the right direction ? there isn't much on the net from what i can see.
Jan 11 '11 #1
0 1204

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Valentina Boycheva | last post by:
Thanks for the reply. I already have "Learning Python" from Mark Lutz and David Ascher, which covers 2.3 (I am in 2.4). However, it seems like heavy artillery to me. What I want is, for instance,...
1
by: Dharmendra Singh | last post by:
Hi I'm using .Net(C#) and working on the form(Screen) which have text boxes for both arabic and english data to store. So i want to change the language at run time from arabic to english and...
4
by: Chris Gordon-Smith | last post by:
I am tying to call a Pascal function from C++, and vice versa. Does anyone know how to do this, or where detailed information on this topic can be found? For the C++ to Pascal call I have...
5
by: Protoman | last post by:
How do I call assembly code from C++ code and vice versa? Thanks for the help!!!
1
by: Mr. x | last post by:
.... The second time I am sending this email ... .... I want a good solution, please. .... Thanks :) What I need is moving xml data from client to server & vice versa, but not using xml file....
2
by: Steve - DND | last post by:
Just wondering if anyone out there has any code to convert a plural word to it's singular form and vice versa. Most of our database tables are named in a plural fashion. When we go to create...
9
by: james | last post by:
I have a FileStream retrieved from FileOpenDialog. I have a Byte which I intend to store for later use. What is the most efficient way of getting the File into my Byte and tehn back out to a new...
2
by: Urs Vogel | last post by:
Hi What's sthe best approach to convert char or char* strings to a BSTR and vice versa? I would like to omit OLE2A macros and allocate the memory myself, or is the the only way? Any hints? ...
5
by: Ney André de Mello Zunino | last post by:
Hello. I am developing a project in managed C++ for the first time. Things were going fine up until the point I had the need to mix managed and non-managed code. More specifically, what I am...
1
by: Maric Michaud | last post by:
Le Tuesday 24 June 2008 07:08:46 swapna mudavath, vous avez écrit : This is not valid xml, there is no commas in attribute list in xml. You could try with minidom if your xml stream isn't too...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.