Asynchronous file transfer from server to client and vice versa

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raghubr
    New Member
    • Jun 2007
    • 7

    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/////////////
    [code=c]
    public void OnReceive(IAsyn cResult ar)
    {
    String content = String.Empty;

    // Retrieve the state object and the handler socket
    // from the asynchronous state object.
    StateObject state = (StateObject)ar .AsyncState;
    Socket handler = state.workSocke t;
    int bytesRead;

    if (handler.Connec ted)
    {

    // Read data from the client socket.
    try
    {
    bytesRead = handler.EndRece ive(ar);
    if (bytesRead > 0)
    {

    state.fst.Write (state.buffer, 0, bytesRead);
    state.fst.Flush ();

    //There might be more data, so store the data received so far.
    // state.sb.Remove (0, state.sb.Length );
    // state.sb.Append (Encoding.ASCII .GetString(
    // state.buffer, 0, bytesRead));

    //// Display Text in Rich Text Box
    // content = state.sb.ToStri ng();
    //SetText(content );

    handler.BeginRe ceive(state.buf fer, 0, StateObject.BUF FER_SIZE, 0,
    new AsyncCallback(O nReceive), state);

    }
    }

    catch (SocketExceptio n socketException )
    {
    //the other side closed impolitely
    if (socketExceptio n.ErrorCode == 10054 || ((socketExcepti on.ErrorCode != 10004) && (socketExceptio n.ErrorCode != 10053)))
    {
    // Complete the disconnect request.
    String remoteIP = ((IPEndPoint)ha ndler.RemoteEnd Point).Address. ToString();
    String remotePort = ((IPEndPoint)ha ndler.RemoteEnd Point).Port.ToS tring();
    // this.owner.Disc onnectClient(re moteIP, remotePort);

    handler.Close() ;
    handler = null;

    }
    }


    catch (Exception exception)
    {
    MessageBox.Show (exception.Mess age + "\n" + exception.Stack Trace);

    }
    }
    }

    }

    //////////class which handles connected clients on server side for file transfer for sending and receiving//////////////////////////////////
    public class StateObject
    {
    // Client socket.
    public Socket workSocket = null;
    // Size of receive buffer.
    public const int BUFFER_SIZE = 1024;
    // Receive buffer.
    public byte[] buffer = new byte[BUFFER_SIZE];
    // Received file stored in static/fixed location.
    public FileStream fst=new FileStream(@"c: \\sample1.txt", FileMode.Create ,FileAccess.Wri te);
    //public StringBuilder sb = new StringBuilder() ;

    }[/code]
    Last edited by sicarie; Jun 25 '07, 01:32 PM. Reason: Please use [code=c] and [/code] tags around your code. Thanks!
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #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?

    Comment

    • raghubr
      New Member
      • Jun 2007
      • 7

      #3
      Originally posted by sicarie
      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.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by raghubr
        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.

        Comment

        • raghubr
          New Member
          • Jun 2007
          • 7

          #5
          Originally posted by sicarie
          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.

          Comment

          • sivakrish85
            New Member
            • Sep 2008
            • 2

            #6
            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(66 00);
            System.out.prin tln("FTP Server Started on Port Number 6600");
            }
            catch(Exception e)
            {
            }
            while(true)
            {
            try
            {
            System.out.prin tln("Waiting for Connection ...");
            Handleclient t=new Handleclient(so c.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.

            Comment

            Working...