473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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
Nov 17 '05 #1
1 6812
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" <lk*@discussion s.microsoft.com > wrote in message
news:BB******** *************** ***********@mic rosoft.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.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

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8648
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 int array byte for example: byte=0xEFBB byte=0xBF0D
11
6616
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 transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
11
32565
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 progress, the filesize, as indicated in Windows Explorer or derived with the above two lines of code, will be the size of the file once the copy operation has completed. Is there a way to
8
7573
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: streamWriter = new StreamWriter(@startupPath+"\\"+filename, false); streamWriter.WriteLine(data); I am not specifying any file Encoding. When I try to open the file
15
4744
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 button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is not validate based on some given rules. I also have a custom error handling page to show the...
1
8088
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! The transfert is stoped randomly (sometimes at 25%, sometimes at 75%,...). And I don't understand why?! :/
10
10612
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 example to follow for using a tcp socket to transfer files between client/server, server/client. Both server and client are my program so I'm not looking for how to implement an FTP client, or how to download a file from a web server via http...
2
3876
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 file transfer?????? -Ted
0
2068
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 developers who are creating .NET applications, the File Transfer .NET component offers a comprehensive interface for uploading and downloading files. The Catalyst File Transfer .NET component is a managed code component that is fully compatible with...
6
2774
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 but the user would have to select the file and initiate the transfer. I would like the transfer to start under program control and not require the user's interaction. In the past I would simply do a GET or POST transfer of a collection of
0
8175
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8625
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8482
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.