472,992 Members | 3,415 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,992 software developers and data experts.

Asynchronous file transfer from server to client and vice versa

Hi all,

Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the common process of connecting and of the server and client...

here is the code i am trying to implement. this code snippet is for message transfer only for multiple clients.

/////////////////////////////file receive method when sent from client/////////////
Expand|Select|Wrap|Line Numbers
  1.  public void OnReceive(IAsyncResult ar)
  2.             {
  3.             String content = String.Empty;
  4.  
  5.             // Retrieve the state object and the handler socket
  6.             // from the asynchronous state object.
  7.             StateObject state = (StateObject)ar.AsyncState;
  8.             Socket handler = state.workSocket;
  9.             int bytesRead;
  10.  
  11.             if (handler.Connected)
  12.             {
  13.  
  14.             // Read data from the client socket. 
  15.             try
  16.             {
  17.             bytesRead = handler.EndReceive(ar);
  18.             if (bytesRead > 0)
  19.             {
  20.  
  21.                 state.fst.Write(state.buffer, 0, bytesRead);
  22.                 state.fst.Flush();
  23.  
  24.              //There  might be more data, so store the data received so far.
  25.             //    state.sb.Remove(0, state.sb.Length);
  26.             //    state.sb.Append(Encoding.ASCII.GetString(
  27.             //                 state.buffer, 0, bytesRead));
  28.  
  29.             //// Display Text in Rich Text Box
  30.             //    content = state.sb.ToString();
  31.                 //SetText(content);
  32.  
  33.             handler.BeginReceive(state.buffer, 0, StateObject.BUFFER_SIZE, 0,
  34.             new AsyncCallback(OnReceive), state);
  35.  
  36.             }
  37.             }
  38.  
  39.             catch (SocketException socketException)
  40.             {
  41.             //the other side closed impolitely
  42.             if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053)))
  43.             {
  44.             // Complete the disconnect request.
  45.             String remoteIP = ((IPEndPoint)handler.RemoteEndPoint).Address.ToString();
  46.             String remotePort = ((IPEndPoint)handler.RemoteEndPoint).Port.ToString();
  47.             // this.owner.DisconnectClient(remoteIP, remotePort);
  48.  
  49.             handler.Close();
  50.             handler = null;
  51.  
  52.             }
  53.             }
  54.  
  55.  
  56.             catch (Exception exception)
  57.             {
  58.             MessageBox.Show(exception.Message + "\n" + exception.StackTrace);
  59.  
  60.             }
  61.             }
  62.             }
  63.  
  64.             }
  65.  
  66. //////////class which handles connected clients on server side for file transfer for sending and receiving////////////////////////////////// 
  67. public class StateObject
  68.     {
  69.         // Client  socket.
  70.         public Socket workSocket = null;
  71.         // Size of receive buffer.
  72.         public const int BUFFER_SIZE = 1024;
  73.         // Receive buffer.
  74.         public byte[] buffer = new byte[BUFFER_SIZE];
  75.         // Received file stored in static/fixed location.
  76.         public FileStream fst=new FileStream(@"c:\\sample1.txt",FileMode.Create,FileAccess.Write);
  77.         //public StringBuilder sb = new StringBuilder();
  78.  
  79.     }
Jun 24 '07 #1
5 8131
sicarie
4,677 Expert Mod 4TB
raghubr-

Are you looking for a program that can do this, or are you attempting to write your own? If you are attempting to write your own, what language is this in?
Jun 25 '07 #2
raghubr-

Are you looking for a program that can do this, or are you attempting to write your own? If you are attempting to write your own, what language is this in?
i am implementing it and the language is c#.Net.
Jun 25 '07 #3
sicarie
4,677 Expert Mod 4TB
i am implementing it and the language is c#.Net.
I have moved your thread to the .Net forum where someone should better be able to answer your question.
Jun 25 '07 #4
I have moved your thread to the .Net forum where someone should better be able to answer your question.

Thank you once again for transferring the thread to .Net forum, but since 6 days i havn't got any solution for this query so please can you help me out in getting the solution because it project specific and i have already exceeded the deadline..i think you got.
Jun 30 '07 #5
Here for Asynchronous File transfer you need to create two threads one for reading thread and Writing thread in both client and server.
when server accept the client socket it must assist the client with these two threads.
i am a java developer so i will show u some example in java itself.

public static void main(String args[])
{
ServerSocket soc=null;
try
{
soc=new ServerSocket(6600);
System.out.println("FTP Server Started on Port Number 6600");
}
catch(Exception e)
{
}
while(true)
{
try
{
System.out.println("Waiting for Connection ...");
Handleclient t=new Handleclient(soc.accept());
}
catch(Exception e)
{
}
}
when server accepted the socket it calls handleclient class it extends the threads class.
from the class handle client we should call read and write thread.
Sep 23 '08 #6

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

Similar topics

9
by: Michael Lindsey | last post by:
I need to write a server app to send images to client GUIs that are outside of the server's domain. The client will have the file system path to the image but can not access the file system. I am...
5
by: Paul de Goede | last post by:
I set the Response.Filter in my aspnet application but I have noticed that if you do a Server.Transfer that the filter doesn't get called. And in actual fact the response is mostly empty. It seems...
4
by: john | last post by:
I have an app that uses Server.Transfer from page1 to page2. page2 needs to be able to read all the values from page1's form. The problem is, if the user clicks the back button on page2 after a...
2
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control...
3
by: rodchar | last post by:
hey all, i'm having trouble understanding the connection (if there is one beween) client-side scripting and server side scripting. For example can a variable that is defined in the code-behind...
8
by: William Meitzen | last post by:
Does someone know how I can get two projects to share a common class? Something like this: solution WholeShmeer project1 code1 CommonClass project2 code2 CommonClass
4
by: =?Utf-8?B?S2F1c2hhbCBNZWh0YQ==?= | last post by:
Hi, I was reading about asynchronous transfer of control in Real Time Java Specification. I am wondering if there is any way we can do similar thing with Web Services in Service Oriented...
1
by: dipalichavan82 | last post by:
I am developing .net application (.net remoting) where i am sending (currently) whole database from client to server n vice versa, so that updation made at client can be updated to server. and also...
5
by: ranajui | last post by:
design a protocol where the communication between a client and a server is established by means of socket connection in java.
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
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.