473,320 Members | 1,902 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,320 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 8185
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.