Sendig file using TCP/IP

L

Lars

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(client);

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

FileInfo info = new FileInfo("c:\\test.doc");
int len = (int)info.Length;
Console.WriteLine(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.WriteLine(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.WriteLine("Klient: " + i);
}
 
P

pei_world

http://www.csharphelp.com/archives2/archive335.html


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(client);

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

FileInfo info = new FileInfo("c:\\test.doc");
int len = (int)info.Length;
Console.WriteLine(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.WriteLine(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.WriteLine("Klient: " + i);
}
 
R

Rich Blum

Lars said:
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(client);

FileStream file1 = new FileStream("test.txt",
FileMode.Open, FileAccess.Read);
FileInfo info = new FileInfo("test.txt");
int len = (int)info.Length;
byte[] bLen = BitConverter.GetBytes(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.WriteLine(" 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(iep);
NetworkStream netStream = new NetworkStream(sock);
byte[] data = new byte[1024];
int recv = netStream.Read(data, 0, 4);
int len = BitConverter.ToInt32(data, 0);
FileStream file1 = new FileStream("test.txt", FileMode.Create);
int count = 0, total = 0;
while(total != len)
{
count = netStream.Read(data, 0, 1024);
file1.Write(data, 0, count);
total += count;
Console.WriteLine(" 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/WileyTitle/productCd-0471433012.html
 

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