By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

Asynchronous file transfer from server to client and vice versa

P: 7
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
Share this Question
Share on Google+
5 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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

P: 7
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
Expert Mod 2.5K+
P: 4,677
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

P: 7
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

P: 2
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

Post your reply

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