How to send big file use socket

Z

zy

I want to send big file from one PC to remote server.I use C# and socket
develop a program,when the client and server run in one computer,there is
not problem. But when trans data throught internet(client and server run in
two computer),server always receive mutch more data than client send.How can
I receive data rightly?


//code:
//Client
string fileName = openFileDialog1.FileName;
byte[] buf = new byte[bufLen];
TcpClient tc = new TcpClient();
tc.Connect(IPAddress.Parse(sIp), int.Parse(tbRPort.Text));
FileStream fs = File.OpenRead(fileName);
//´«ËÍÎļþÃû
byte[] fileNameBuf =
System.Text.Encoding.ASCII.GetBytes(fs.Name.ToCharArray());
fileNameBuf.CopyTo(buf, 0);
tc.GetStream().Write(buf, 0, bufLen);
//GetShortFileName(fileName);
//´«ËÍÎļþ´óС
tc.GetStream().Write(BitConverter.GetBytes(fs.Length),0,sizeof(long));
Log("FileLen:" + fs.Length);
long sectionCount, lastSectionLength;
sectionCount = fs.Length / bufLen;
lastSectionLength = fs.Length % bufLen;
Log("SectionCount:" + sectionCount);
Log("LastSectionLength:" + lastSectionLength);
if (fs.Length < bufLen)
{
sectionCount = 0;
lastSectionLength = fs.Length;
}
Log("Begion send");
for (int i = 0; i < sectionCount; i++)
{
fs.Read(buf, 0, bufLen);
//fs.Flush();
tc.GetStream().Write(buf, 0, bufLen);
//tc.GetStream().Flush();
Log("Section:" + i);
}
fs.Read(buf, 0, (int)lastSectionLength);
fs.Flush();
tc.GetStream().Write(buf, 0, (int)lastSectionLength);
tc.GetStream().Flush();
Log("LastSection");
tc.Close();
MessageBox.Show("Ok,send complete.");
fs.Close();
//server

NetworkStream ns = new NetworkStream(socket);
byte[] buf = new byte[bufLen];
//¶ÁÈ¡ÎļþÃû
ns.Read(buf, 0, bufLen);
string fileName = System.Text.Encoding.ASCII.GetString(buf);
fileName = path + GetShortFileName(fileName);
fileName = fileName.Replace("\0", "");
//MessageBox.Show(fileName);
System.IO.FileStream fs = System.IO.File.Open(fileName,
FileMode.OpenOrCreate);
//¶ÁÈ¡Îļþ´óС
ns.Read(buf, 0, sizeof(Int64));
ns.Flush();
Int64 fileLen = BitConverter.ToInt64(buf, 0);
Logr("FileLen:" + fileLen);
//MessageBox.Show(fileLen.ToString());
Int64 sectionCount, lastSectionLength;
sectionCount = fileLen / bufLen;
lastSectionLength = fileLen % bufLen;
if (fileLen < bufLen)
{
sectionCount = 0;
lastSectionLength = fileLen;
}
Logr("SectionCount:" + sectionCount);
Log("LastSectionLength:" + lastSectionLength);
//MessageBox.Show("SectionCount" + sectionCount + "
LastSectionLength" + lastSectionLength.ToString());
for (int i = 0; i < sectionCount; i++)
{
ns.Read(buf, 0, bufLen);
//ns.Flush();
fs.Write(buf, 0, bufLen);
//fs.Flush();
Logr("Section:" + i);
}
//ns.Read(buf, 0, (int)lastSectionLength);
//fs.Write(buf, 0, (int)lastSectionLength);
int lastRead = ns.Read(buf, 0, bufLen);
fs.Write(buf, 0, lastRead);
Logr("LastSection:" + lastRead);
fs.Flush();
fs.Close();
ns.Close();
ns = null;
fs = null;
 
I

Ignacio Machin ( .NET/ C# MVP )

I want to send big file from one PC to remote server.I use C# and socket
develop a program,when the client and server run in one computer,there is
not problem. But when trans data throught internet(client and server run in
two computer),server always receive mutch more data than client send.How can
I receive data rightly?

I honestly did not read your entire code, but a very common error is
to always send the full buffer even when the buffer contains less
info, let's say that your buffer is 50bytes if you send a file of 75
bytes the second time you should be prepared to read 25 and not 50.
You should send first the file size and then keep a readed counter,
somwthing like:

int totalSize = getTotalSize
int bufferSize = ......
int readed = 0;
while ( readed<totalSize ){
//get the correct qty to read
int toRead = (totalSize-readed)<bufferSize > totalSize-readed ?
bufferSize ;
readed+= stream.Read( ...., toRead );
}
 

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