473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 6789
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lingyun Yang | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Dear all, I have a file it's binary data viewed in UltraEdit is EF BB BF 0D 0A 3C ....... I want to read them into a int or long...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
11
by: Stephan Steiner | last post by:
Hi Generally, FileInfo fi = new FileInfo(path); long size = fi.Length; gets you the length of a file in bytes. However, when copying files, even while the copy operation is still in...
8
by: Xarky | last post by:
Hi, I am downloading a GIF file(as a mail attachement) with this file format, Content-Transfer-Encoding: base64; Now I am writing the downloaded data to a file with this technique: ...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
1
by: Alex | last post by:
Hello, I'm trying to write a little php script to transfert some files from a server to clients (web/http). It's working fin with small files. But transfering big files (try on 1Gb) failed!...
10
by: David | last post by:
I have googled to no avail on getting specifically what I'm looking for. I have found plenty of full blown apps that implement some type of file transfer but what I'm specifcally looking for is an...
2
by: tedpottel | last post by:
Hi, My program has the following code to transfer a binary file f = open(pathanme+filename,'rb') print "start transfer" self.fthHandle.storbinary('STOR '+filename, f) How can I do an ASCII...
0
by: fiona | last post by:
Yucca Valley, CA, - October 2007: Catalyst Development Corporation, publisher of SocketTools, SocketWrench and LogicGem, today announced the release of Catalyst File Transfer .NET V5.0. For...
6
by: Thom Little | last post by:
I need to transfer an XML file between an application on the client and the server. This would in fact be a copy of the application's .config file. HTML <input form=...would transfer the file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.