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;
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;