File Transfer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lkr

    File Transfer

    hi
    i got one file transfer program using serialization which has a limitation
    that i can send only 8192 bytes(8KB). i want to send more than that wht can i
    do. how can i divide the file into mutiple segment and send the file and
    receive it. here is the program below

    public static void SendFileInfo()
    {
    // Get file type or extension
    fileDet.FILETYP E = fs.Name.Substri ng((int)fs.Name .Length - 3, 3);

    // Get file length (Future purpose)
    fileDet.FILESIZ E = fs.Length;

    XmlSerializer fileSerializer = new XmlSerializer(t ypeof(FileDetai ls));
    MemoryStream stream = new MemoryStream();

    // Serialize object
    fileSerializer. Serialize(strea m, fileDet);
    // Stream to byte
    stream.Position = 0;
    byte[] bytes = new byte[stream.Length];
    stream.Read(byt es, 0, Convert.ToInt32 (stream.Length) );

    Console.WriteLi ne("Sending file details...");

    // Send file details
    sender.Send(byt es, bytes.Length, endPoint);
    stream.Close();
    }
    and in the receiving end

    public static void ReceiveFile()
    {
    try
    {
    Console.WriteLi ne(
    "-----------*******Waiting to get File!!*******-----------");
    // Receive file

    receiveBytes = receivingUdpCli ent.Receive(ref RemoteIpEndPoin t);

    // Convert and display data
    Console.WriteLi ne("----File received...Savi ng...");

    // Create temp file from received file extension
    fs = new FileStream("tem p." + fileDet.FILETYP E, FileMode.Create ,
    FileAccess.Read Write, FileShare.ReadW rite);
    fs.Write(receiv eBytes, 0, receiveBytes.Le ngth);

    Console.WriteLi ne("----File Saved...");
    Console.WriteLi ne("-------Opening file with associated program------");

    Process.Start(f s.Name); // Opens file with associated program
    }
    catch (Exception e)
    {
    Console.WriteLi ne(e.ToString ());
    }
    finally
    {
    //fs.Close();
    receivingUdpCli ent.Close();
    }
    }
    plz give solution for this

    thanks for advance
    lkr
  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: File Transfer

    Hi,


    Basically what you want is a Send file / Receive file set of methods right?

    IF so find below the code for those, I also include two help methods (
    Read/WriteStringToNe twork ) that they use.

    Also notice the small buffers cause these are meant to run on a PocketPC

    Cheers,


    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation


    //*************** *************** *************** *************** ************

    public void SendFile( string filename )
    {
    try
    {
    int readed=0;
    byte[] buff = new Byte[2048];
    FileStream fstream = new FileStream( filename, FileMode.Open);
    //send the file length
    WriteStringToNe twork( fstream.Length. ToString() );
    //writer.WriteLin e( fstream.Length) ;
    //writer.Flush();
    while( (readed=fstream .Read( buff, 0, 2048))>0 )
    networkstream.W rite( buff, 0, readed);
    fstream.Close() ;
    }
    catch(Exception e)
    {
    throw new Exception("\n== >Method: NetAccess.SendF ile, sending this file
    :"+ filename +" :" + e.Message );
    }
    }
    public void ReceiveFile( string filename)
    {
    try
    {
    int size= Convert.ToInt32 (ReadStringFrom Network());
    FileStream fs = new FileStream( filename, FileMode.Create );
    byte[] buff = new Byte[ size>40048?4004 8:size];
    int readed=0;
    int readedt=0;
    int toread= size>40048?4004 8:size;
    while( (readedt=networ kstream.Read( buff, 0, toread))>0)
    {
    readed+= readedt;
    fs.Write( buff, 0, readedt);
    toread=(size-readed)>40048?4 0048:size-readed;
    if ( toread == 0 ) break;
    }
    fs.Close();

    }
    catch(Exception e)
    {
    throw new Exception("\n== >Method: NetAccess.Recei veFile, receiving this
    file :"+ filename +" :" + e.Message );
    }
    }






    public void WriteStringToNe twork(string towrite)
    {
    try
    {
    //char[] chars = towrite.ToCharA rray();
    foreach( char c in towrite.ToCharA rray())
    networkstream.W riteByte( Convert.ToByte( c));
    networkstream.W riteByte( 13);
    networkstream.W riteByte( 10);
    //Now we have to convert the chars to byte
    //writer.WriteLin e( towrite);
    //writer.Flush();
    }
    catch(Exception e)
    {
    throw new Exception("\n== >Method: NetAccess.Write StringToNetwork ,
    writing this string " + towrite+ " :" + e.Message );

    }
    }
    public string ReadStringFromN etwork( )
    {
    StringBuilder buff;
    try
    {
    buff = new StringBuilder( 20);
    int ch;
    while( (ch=networkstre am.ReadByte())! = -1)
    {
    if (ch == 13) continue;
    if ( ch==10) return buff.ToString() ;
    buff.Append( Convert.ToChar( ch));

    }
    }
    catch(Exception e)
    {
    throw new Exception("\n== >Method: NetAccess.ReadS tringFromNetwor k,
    reading string :" + e.Message );

    }
    return buff.ToString() ;
    }


    //*************** *************** *************** *************** *************


    "lkr" <lkr@discussion s.microsoft.com > wrote in message
    news:BB1C124E-DC5A-472A-874B-826EA00C45E5@mi crosoft.com...[color=blue]
    > hi
    > i got one file transfer program using serialization which has a limitation
    > that i can send only 8192 bytes(8KB). i want to send more than that wht
    > can i
    > do. how can i divide the file into mutiple segment and send the file and
    > receive it. here is the program below
    >
    > public static void SendFileInfo()
    > {
    > // Get file type or extension
    > fileDet.FILETYP E = fs.Name.Substri ng((int)fs.Name .Length - 3, 3);
    >
    > // Get file length (Future purpose)
    > fileDet.FILESIZ E = fs.Length;
    >
    > XmlSerializer fileSerializer = new XmlSerializer(t ypeof(FileDetai ls));
    > MemoryStream stream = new MemoryStream();
    >
    > // Serialize object
    > fileSerializer. Serialize(strea m, fileDet);
    > // Stream to byte
    > stream.Position = 0;
    > byte[] bytes = new byte[stream.Length];
    > stream.Read(byt es, 0, Convert.ToInt32 (stream.Length) );
    >
    > Console.WriteLi ne("Sending file details...");
    >
    > // Send file details
    > sender.Send(byt es, bytes.Length, endPoint);
    > stream.Close();
    > }
    > and in the receiving end
    >
    > public static void ReceiveFile()
    > {
    > try
    > {
    > Console.WriteLi ne(
    > "-----------*******Waiting to get File!!*******-----------");
    > // Receive file
    >
    > receiveBytes = receivingUdpCli ent.Receive(ref RemoteIpEndPoin t);
    >
    > // Convert and display data
    > Console.WriteLi ne("----File received...Savi ng...");
    >
    > // Create temp file from received file extension
    > fs = new FileStream("tem p." + fileDet.FILETYP E, FileMode.Create ,
    > FileAccess.Read Write, FileShare.ReadW rite);
    > fs.Write(receiv eBytes, 0, receiveBytes.Le ngth);
    >
    > Console.WriteLi ne("----File Saved...");
    > Console.WriteLi ne("-------Opening file with associated program------");
    >
    > Process.Start(f s.Name); // Opens file with associated program
    > }
    > catch (Exception e)
    > {
    > Console.WriteLi ne(e.ToString ());
    > }
    > finally
    > {
    > //fs.Close();
    > receivingUdpCli ent.Close();
    > }
    > }
    > plz give solution for this
    >
    > thanks for advance
    > lkr[/color]


    Comment

    Working...