469,270 Members | 1,583 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

File Transfer

lkr
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
Nov 17 '05 #1
1 6433
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" <lk*@discussions.microsoft.com> wrote in message
news:BB**********************************@microsof t.com...
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

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Lingyun Yang | last post: by
11 posts views Thread by Abhishek | last post: by
11 posts views Thread by Stephan Steiner | last post: by
8 posts views Thread by Xarky | last post: by
1 post views Thread by Alex | last post: by
10 posts views Thread by David | last post: by
2 posts views Thread by tedpottel | last post: by
6 posts views Thread by Thom Little | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.