473,749 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sendig file using TCP/IP

Hi! I´m trying to send a file over a network with tcp/ip
so that the client can download it and save it. I going to
send the size of the file in a textmessage but I have not
implemented that part yet. But am I getting in right?
This is the server (not all).

Socket client = server.Accept() ;
NetworkStream netStream = new NetworkStream(c lient);

FileStream file = new FileStream
("c:\\test.doc" ,FileMode.Open, FileAccess.Read );

FileInfo info = new FileInfo("c:\\t est.doc");
int len = (int)info.Lengt h;
Console.WriteLi ne(len);
byte [] b = new byte[50 * 1024];

int count = 0;
while(count != len)
{
count = file.Read(b, 0, len);
netStream.Write (b, 0, len);
Console.WriteLi ne(count);
}

And the client:

FileStream file = new FileStream("c:\ \test2.doc",
FileMode.Create );

byte [] b = new byte[50 * 1024];
int len = 19456; //Size of file, should be sent.
int count = 0;

for(count = 0; count <= len; count++)
{
int i = netStream.Read( b, 0, len);
file.Write(b, 0, len);
Console.WriteLi ne("Klient: " + i);
}


Nov 15 '05 #1
3 13458
http://www.csharphelp.com/archives2/archive335.html
"Lars" <an*******@disc ussions.microso ft.com> wrote in message
news:06******** *************** *****@phx.gbl.. .
Hi! I´m trying to send a file over a network with tcp/ip
so that the client can download it and save it. I going to
send the size of the file in a textmessage but I have not
implemented that part yet. But am I getting in right?
This is the server (not all).

Socket client = server.Accept() ;
NetworkStream netStream = new NetworkStream(c lient);

FileStream file = new FileStream
("c:\\test.doc" ,FileMode.Open, FileAccess.Read );

FileInfo info = new FileInfo("c:\\t est.doc");
int len = (int)info.Lengt h;
Console.WriteLi ne(len);
byte [] b = new byte[50 * 1024];

int count = 0;
while(count != len)
{
count = file.Read(b, 0, len);
netStream.Write (b, 0, len);
Console.WriteLi ne(count);
}

And the client:

FileStream file = new FileStream("c:\ \test2.doc",
FileMode.Create );

byte [] b = new byte[50 * 1024];
int len = 19456; //Size of file, should be sent.
int count = 0;

for(count = 0; count <= len; count++)
{
int i = netStream.Read( b, 0, len);
file.Write(b, 0, len);
Console.WriteLi ne("Klient: " + i);
}

Nov 15 '05 #2
Well, I´m a beginner at C# and it doesn't help me much.
-----Original Message-----
http://www.csharphelp.com/archives2/archive335.html
"Lars" <an*******@disc ussions.microso ft.com> wrote in messagenews:06******* *************** ******@phx.gbl. ..
Hi! I´m trying to send a file over a network with tcp/ip
so that the client can download it and save it. I going to
send the size of the file in a textmessage but I have not
implemented that part yet. But am I getting in right?
This is the server (not all).

Socket client = server.Accept() ;
NetworkStrea m netStream = new NetworkStream(c lient);

FileStream file = new FileStream
("c:\\test.doc ",FileMode.Open , FileAccess.Read );

FileInfo info = new FileInfo("c:\\t est.doc");
int len = (int)info.Lengt h;
Console.WriteL ine(len);
byte [] b = new byte[50 * 1024];

int count = 0;
while(count != len)
{
count = file.Read(b, 0, len);
netStream.Write (b, 0, len);
Console.WriteLi ne(count);
}

And the client:

FileStream file = new FileStream("c:\ \test2.doc",
FileMode.Creat e);

byte [] b = new byte[50 * 1024];
int len = 19456; //Size of file, should be sent.
int count = 0;

for(count = 0; count <= len; count++)
{
int i = netStream.Read( b, 0, len);
file.Write(b, 0, len);
Console.WriteLi ne("Klient: " + i);
}

.

Nov 15 '05 #3
"Lars" <an*******@disc ussions.microso ft.com> wrote in message news:<06******* *************** ******@phx.gbl> ...
Hi! I m trying to send a file over a network with tcp/ip
so that the client can download it and save it. I going to
send the size of the file in a textmessage but I have not
implemented that part yet. But am I getting in right?

Lars -

You had the right idea, but had a couple of your counters off. The
main idea is for the server to loop through the file until it knows it
sent the whole thing. Then the receiver can loop through reading from
the network until it knows it received the whole thing. Of course the
tricky part is knowing what "the whole thing" is.

Here's some code for the server side:

Socket client = sock.Accept();
NetworkStream netStream = new NetworkStream(c lient);

FileStream file1 = new FileStream("tes t.txt",
FileMode.Open, FileAccess.Read );
FileInfo info = new FileInfo("test. txt");
int len = (int)info.Lengt h;
byte[] bLen = BitConverter.Ge tBytes(len);
netStream.Write (bLen, 0, 4);
netStream.Flush ();

byte[] data = new byte[1024];
int count = 0, total = 0;
while(total != len)
{
count = file1.Read(data , 0, 1024);
netStream.Write (data, 0, count);
total += count;
Console.WriteLi ne(" sent {0} bytes", count);
}
netStream.Flush ();

This code first finds the file size, converts it to a byte array,
and sends it out on the network. After sending the size of the file,
the server loops through reading the blocks of data from the file and
sending it out on the network. Now for the receiver:

sock.Connect(ie p);
NetworkStream netStream = new NetworkStream(s ock);
byte[] data = new byte[1024];
int recv = netStream.Read( data, 0, 4);
int len = BitConverter.To Int32(data, 0);
FileStream file1 = new FileStream("tes t.txt", FileMode.Create );
int count = 0, total = 0;
while(total != len)
{
count = netStream.Read( data, 0, 1024);
file1.Write(dat a, 0, count);
total += count;
Console.WriteLi ne(" wrote {0} total bytes", total);
}

The receiver knows the first 4 bytes received from the server are
the file size, so it converts them into an Int32 value, which tells it
the file size to expect. After that the receiver loops through reading
data from the network until all of the bytes have been written.

Hope this helps you out some on your project. Good luck.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html
Nov 15 '05 #4

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

Similar topics

8
4063
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had problems getting my algorithm to work and in order to help me find the solution I decided to print each line to screen as I read them. Then, to my surprise, I noticed that there was a space between every character as I outputted the lines to the...
3
26267
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
9
8277
by: ALI-R | last post by:
Hi,, I have two questions : 1) Is it mandatory that config file of a desktop application must be App.config 2) Is it possible to update config file in your code?? thanks for your help. ALI
13
4319
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming that this is suppossed to end up as a component for others to use, and therefore I do NOT have access to their global.cs::Session_End() how do I cleanup files that were uploaded -- but obviously left stranded when the users aborted/gave up writting...
15
4777
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
19951
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access it, it throws an exception. How to solave this problem? So second process can wait until first process completes its processing on the file. Thanks in advance
1
1879
by: Jerry John | last post by:
I am working in ASP.NET with C#. I have a text file which contains datas with delimiters. For example:- MSH|^~\$|DISCHARGE|CLAY COUNTY MEMORIAL|||200502110939| I also have an XML file created with predefined tags. Some of the tags contain child element. I need to pass the data from the text file i.e the value within the delimiters should be passed to the corresponding tags within the XML file. I have done this through hard code. But i...
17
8029
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: I am trying to use the SaveFileDialog class to get a filename, which is subsequently opened for writing (write access, read sharing, but using read/write sharing doesn't make the problem go away anyway). Sometimes, on the statement where I...
3
8296
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and then decrypting, the resulting decrypted file under .NET 2.0 is garbage, the .NET 1.1 build works...
0
9568
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
9389
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...
1
9335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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
8257
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
4709
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...
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
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.