File Transfer

G

Guest

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.FILETYPE = fs.Name.Substring((int)fs.Name.Length - 3, 3);

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

XmlSerializer fileSerializer = new XmlSerializer(typeof(FileDetails));
MemoryStream stream = new MemoryStream();

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

Console.WriteLine("Sending file details...");

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

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

receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

// Convert and display data
Console.WriteLine("----File received...Saving...");

// Create temp file from received file extension
fs = new FileStream("temp." + fileDet.FILETYPE, FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Write(receiveBytes, 0, receiveBytes.Length);

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

Process.Start(fs.Name); // Opens file with associated program
}
catch (Exception e)
{
Console.WriteLine(e.ToString ());
}
finally
{
//fs.Close();
receivingUdpClient.Close();
}
}
plz give solution for this

thanks for advance
lkr
 
I

Ignacio Machin \( .NET/ C# MVP \)

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/WriteStringToNetwork ) 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
WriteStringToNetwork( fstream.Length.ToString() );
//writer.WriteLine( fstream.Length);
//writer.Flush();
while( (readed=fstream.Read( buff, 0, 2048))>0 )
networkstream.Write( buff, 0, readed);
fstream.Close();
}
catch(Exception e)
{
throw new Exception("\n==>Method: NetAccess.SendFile, sending this file
:"+ filename +" :" + e.Message );
}
}
public void ReceiveFile( string filename)
{
try
{
int size= Convert.ToInt32(ReadStringFromNetwork());
FileStream fs = new FileStream( filename, FileMode.Create);
byte[] buff = new Byte[ size>40048?40048:size];
int readed=0;
int readedt=0;
int toread= size>40048?40048:size;
while( (readedt=networkstream.Read( buff, 0, toread))>0)
{
readed+= readedt;
fs.Write( buff, 0, readedt);
toread=(size-readed)>40048?40048:size-readed;
if ( toread == 0 ) break;
}
fs.Close();

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






public void WriteStringToNetwork(string towrite)
{
try
{
//char[] chars = towrite.ToCharArray();
foreach( char c in towrite.ToCharArray())
networkstream.WriteByte( Convert.ToByte( c));
networkstream.WriteByte( 13);
networkstream.WriteByte( 10);
//Now we have to convert the chars to byte
//writer.WriteLine( towrite);
//writer.Flush();
}
catch(Exception e)
{
throw new Exception("\n==>Method: NetAccess.WriteStringToNetwork,
writing this string " + towrite+ " :" + e.Message );

}
}
public string ReadStringFromNetwork( )
{
StringBuilder buff;
try
{
buff = new StringBuilder( 20);
int ch;
while( (ch=networkstream.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.ReadStringFromNetwork,
reading string :" + e.Message );

}
return buff.ToString();
}


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


lkr said:
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.FILETYPE = fs.Name.Substring((int)fs.Name.Length - 3, 3);

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

XmlSerializer fileSerializer = new XmlSerializer(typeof(FileDetails));
MemoryStream stream = new MemoryStream();

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

Console.WriteLine("Sending file details...");

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

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

receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

// Convert and display data
Console.WriteLine("----File received...Saving...");

// Create temp file from received file extension
fs = new FileStream("temp." + fileDet.FILETYPE, FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Write(receiveBytes, 0, receiveBytes.Length);

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

Process.Start(fs.Name); // Opens file with associated program
}
catch (Exception e)
{
Console.WriteLine(e.ToString ());
}
finally
{
//fs.Close();
receivingUdpClient.Close();
}
}
plz give solution for this

thanks for advance
lkr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top