从 TCP 侦听器中的网络流读取数据
本文关键字:读取 数据 网络 TCP 侦听器 | 更新日期: 2023-09-27 18:34:50
我有一个网络流,其中包含文件名和文件数据。我正在以一些块发送文件,每个块都带有文件名以便于识别。您能否帮助我正确读取网络流并将所有数据写入文件流。当我从网络流写入数据时,我似乎错过了几个字节。例如,文件名长度将在第 4 个索引中,文件名将从第 4 个索引开始。
Client:
int NoOfPackets = Convert.ToInt32
(Math.Ceiling((Convert.ToDouble(Fs.Length))/ Convert.ToDouble(BufferSize)));
int TotalLength = (NoOfPackets *4+fileNameByte.Length) +(int)Fs.Length, CurrentPacketLength, counter = 0;
netstream1 = client.GetStream();
for (int i = 0; i < NoOfPackets+1 ; i++)
{
if (TotalLength > BufferSize)
{
CurrentPacketLength = BufferSize;
TotalLength = TotalLength - CurrentPacketLength;
}
else
CurrentPacketLength = TotalLength;
SendingBuffer = new byte[CurrentPacketLength];
fileNameLength.CopyTo(SendingBuffer, 0);
fileNameByte.CopyTo(SendingBuffer, 4);
Fs.Read(SendingBuffer, 4 + fileNameByte.Length, CurrentPacketLength - (4 + fileNameByte.Length));
netstream1.Write(SendingBuffer, 0, SendingBuffer.Length);
netstream1.Flush();
}
侦听器代码:
client = Listener.AcceptTcpClient();
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
//client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, true);
client.Client.LingerState = new LingerOption(true, 300);
client.Client.SendTimeout = 300000;
client.Client.ReceiveTimeout = 300000;
client.NoDelay = true;
NetworkStream netstream = client.GetStream();
MemoryStream ms = new MemoryStream();
bool first = true;
string fullPath = "";
byte[] RecData = new byte[512000];
while ((RecBytes = netstream.Read
(RecData, 0, RecData.Length)) > 0)
{
int fileNameLen = RecData[0];
if (fileNameLen > 0 && first)
{
string name1 = "", name2 = "";
first = false;
name1 = Encoding.UTF8.GetString(RecData, 4, fileNameLen);
name2 = name1;
string folderName = "";
if (name2.Contains("''"))
{
folderName = name2.Substring(0, name2.LastIndexOf("''"));
if (!Directory.Exists("D:''123''" + folderName))
Directory.CreateDirectory("D:''123''" + folderName);
}
if (folderName != "")
fullPath = "D:''123''" + folderName + "''" + name2.Substring(name2.LastIndexOf("''") + 1);
else
fullPath = "D:''123''" + name2.Substring(name2.LastIndexOf("''") + 1);
}
if (!File.Exists(fullPath))
{
//file = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Write);
while (true)
{
try
{
using (FileStream file = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Write))
{
if (RecBytes - (4 + fileNameLen) > 0)
file.Write(RecData, 4 + fileNameLen, RecBytes - (4 + fileNameLen));
break;
}
}
catch (IOException)
{
Thread.Sleep(20);
}
}
//using (file = File.Create(fullPath))
//{
// file.Write(data, 4 + fileNameLen, (int)data.Length - (4 + fileNameLen));
//}
}
else
{
while (true)
{
try
{
using (FileStream file = new FileStream(fullPath, FileMode.Append, FileAccess.Write))
{
if (RecBytes - (4 + fileNameLen) > 0)
file.Write(RecData, 4 + fileNameLen, RecBytes - (4 + fileNameLen));
break;
}
}
catch (IOException)
{
Thread.Sleep(20);
}
}
}
}
ms.Close();
netstream.Close();
client.Close();
即使你和任何人一样模糊,我仍然建议你创建一个结构,以便数据的序列化和反序列化可以以已知格式完成!! 并且可以避免数据丢失!!同样,如果您的方法已知,这将对回答您的问题有很大帮助。