Tcpclient-Tcpserver 接收的帧的第二个字节数组变为负响应字节长度 = 347713
本文关键字:字节 响应 347713 数组 第二个 Tcpclient-Tcpserver 字节数 | 更新日期: 2023-09-27 18:19:18
请帮助我理解为什么当通过网络发送一些帧转换字节数组时,第一帧(字节数组(正确到达,而到达的第二个字节数组(第二帧(的大小错误甚至为负数。
这是发送帧的客户端代码:
public static void invia(byte[] bytetosend)
{
byte[] compressed;
compressed = Compressor.Compressor.Compress(bytetosend);
byte[] bytes = new byte[1024];
try
{
IPAddress ipAddressremoto = IPAddress.Parse("192.168.1.191");
IPEndPoint remoteEP = new IPEndPoint(ipAddressremoto, 8585);
try
{
if (Form1.z == 0)
{
Form1.client = new TcpClient();
Form1.client.Connect(remoteEP);
Form1.streamclient = client.GetStream();
Form1.z = 1;
}
Form1.streamclient.Flush();
byte[] sizePacket = new byte[50];
sizePacket = BitConverter.GetBytes(compressed.Length);
System.Console.WriteLine("size of packet = " + sizePacket.Length + " compressed.Length = " + compressed.Length + " " + "size inside packet " + BitConverter.ToInt32(sizePacket, 0));
byte[] requestWithHeader = new byte[sizePacket.Length + compressed.Length];
sizePacket.CopyTo(requestWithHeader, 0);
bytetosend.CopyTo(requestWithHeader, sizePacket.Length);
streamclient.Write(requestWithHeader, 0, requestWithHeader.Length);
}
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
相反,这是接收帧的服务器端代码,不幸的是我得到正确,使用合适尺寸的框架,只有第一个框架,而不是第二个框架注意:一些旧的指令被描述而不是删除
public WindowsFormsServer()
{
var frameTask = Task.Factory.StartNew(() =>
{
try
{
while (true)
{
if (WindowsFormsServer.y == 0)
{
WindowsFormsServer.server = WindowsFormsServer.Idea_server();
// WindowsFormsServer.server.ReceiveTimeout = 50000;
// Console.WriteLine("Waiting for a connection...");
WindowsFormsServer.streamserver = server.GetStream();
}
//streamserver.Flush();
byte[] responseBytes = null;
//if (streamserver.CanRead)
//{
var responseStream = new System.IO.MemoryStream();
byte[] sizePacket = new byte[50];
streamserver.Read(sizePacket, 0, sizePacket.Length);
int responseLength = BitConverter.ToInt32(sizePacket, 0);
System.Console.WriteLine("size of packet = " + sizePacket.Length + "size inside packet = " + responseLength);
byte[] response = new byte[responseLength];
int bytesReceived = 0;
while (bytesReceived < responseLength)
{
int bytesRead = streamserver.Read(response, bytesReceived, responseLength - bytesReceived);
bytesReceived += bytesRead;
}
System.Console.WriteLine("frameTask attivo 'r'n");
responseStream.Write(response, 0, responseLength);
//System.Threading.Thread.Sleep(1); //added this line
responseBytes = responseStream.ToArray();
Console.WriteLine("responseBytes Length = " + responseBytes.Length);
//}
}
}
catch (NullReferenceException ex)
{
System.Console.WriteLine("Errore: " + ex.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
try
{
//MessageData.Picture2 = (Bitmap)byteArrayToImage(Compressor.Compressor.Decompress(responseBytes));
MessageData.Picture2 = (Bitmap)byteArrayToImage(responseBytes);
CamImageBox.Image = MessageData.Picture2;
}
catch (NullReferenceException ex)
{
System.Console.WriteLine("Errore: " + ex.ToString());
}
catch (Exception ex)
{
System.Console.WriteLine("Errore: " + ex.ToString());
}
});
//frameTask.Start();
InitializeComponent();
}
这是服务器端的错误,它将帧作为字节数组接收
'WindowsFormsServer.vshost.exe' (CLR v4.0.30319: WindowsFormsServer.vshost.exe): caricamento di 'C:'Windows'Microsoft.Net'assembly'GAC_MSIL'System.Configuration'v4.0_4.0.0.0__b03f5f7f11d50a3a'System.Configuration.dll' completato. Caricamento dei simboli ignorato. Il modulo è ottimizzato e l'opzione del debugger 'Solo codice utente' è abilitata.
lunghezza = 5
这是使用标头写入客户端发送方字节数组帧的大小,然后我读取服务器端接收器的长度
inviocomandoTask attivo
- 数据包大小 = 50
- 数据包内的大小 = 347713
收到的第一帧的长度,这是正确的,因为它与发送方发送的相同。
'WindowsFormsServer.vshost.exe' (CLR v4.0.30319: WindowsFormsServer.vshost.exe): caricamento di 'C:'Windows'Microsoft.Net'assembly'GAC_MSIL'mscorlib.resources'v4.0_4.0.0.0_it_b77a5c561934e089'mscorlib.resources.dll' completato. Modulo compilato senza simboli.
frameTask attivo
- 响应字节长度 = 347713
- 数据包大小 = 50
- 数据包内的大小 = -383988050
收到的第二帧的长度,这是错误的
Eccezione generata: 'System.OverflowException' in WindowsFormsServer.exe Eccezione generata: 'System.ArgumentNullException' in mscorlib.dll System.OverflowException: Le dimensioni della matrice hanno superato l'intervallo supportato. The size of the array exceeded the supported range. in WindowsFormsServer.WindowsFormsServer.<.ctor>b__12_0() in C:'Users'vmtest'Documents'CamDisplay4'WindowsFormsServer'Form1.cs:riga 70 Errore: System.ArgumentNullException: Il buffer non può essere null. Nome parametro: buffer in System.IO.MemoryStream..ctor(Byte[] buffer, Boolean writable) in WindowsFormsServer.WindowsFormsServer.byteArrayToImage(Byte[] bytesArr) in C:'Users'vmtest'Documents'CamDisplay4'WindowsFormsServer'Form1.cs:riga 170 in WindowsFormsServer.WindowsFormsServer.<.ctor>b__12_0() in C:'Users'vmtest'Documents'CamDisplay4'WindowsFormsServer'Form1.cs:riga 98 Il thread 0x19ec è terminato con il codice 0 (0x0). Il thread 0x19d4 è terminato con il codice 0 (0x0). Il programma '[1352] WindowsFormsServer.vshost.exe' è terminato con il codice 0 (0x0).
您对收到的第二个帧字节数组中发生的情况有什么建议吗?您是否认为这是由服务器引起的,它在没有异步方法的情况下以同步模式接收字节?
byte[] sizePacket = new byte[50];
streamserver.Read(sizePacket, 0, sizePacket.Length);
在这里,您读取 50 个字节,但仅使用前 4 个字节。其余的被扔掉。
我会这样做:
var br = new BinaryReader(someStream);
var length = br.ReadUInt32();
var contents = br.ReadBytes(length);
这就是你真正需要的。
byte[] sizePacket = new byte[50];
sizePacket = BitConverter.GetBytes(compressed.Length);
为什么总是 50 字节??这是没有道理的。首先使用正确的长度。在这里,您无论如何都会覆盖该变量,因此这应该是:
byte[] sizePacket = BitConverter.GetBytes(compressed.Length);
System.Console.WriteLine("size of packet = " + sizePacket.Length + "size inside packet = " + responseLength);
这个消息是错误的。这不是"数据包"的大小。TCP 不会公开数据包。数组大小为 50,从 Read 读取返回值,该值可能与 50 不同。
现在我已经按照您所说的代码进行了更改:但我认为这是不正确的,可以即兴创作,事实上在接收器部分,我需要在收到帧之前创建字节数组时设置帧大小,所以我这样做了:
var br = new BinaryReader(streamserver);
var length = br.ReadUInt32();
var responseLength = br.ReadBytes(4);
int responseIntLength = BitConverter.ToInt32(responseLength, 0);
byte[] response = new byte[responseIntLength];
这是新的全服务器接收器TCP端
public WindowsFormsServer()
{
var frameTask = Task.Factory.StartNew(() =>
{
try
{
while (true)
{
if (WindowsFormsServer.y == 0)
{
WindowsFormsServer.server = WindowsFormsServer.Idea_server();
// WindowsFormsServer.server.ReceiveTimeout = 50000;
// Console.WriteLine("Waiting for a connection...");
WindowsFormsServer.streamserver = server.GetStream();
}
//streamserver.Flush();
byte[] responseBytes = null;
//if (streamserver.CanRead)
//{
var responseStream = new System.IO.MemoryStream();
byte[] sizePacket = new byte[4];
var br = new BinaryReader(streamserver);
var length = br.ReadUInt32();
var responseLength = br.ReadBytes(4);
int responseIntLength = BitConverter.ToInt32(responseLength, 0);
byte[] response = new byte[responseIntLength];
System.Console.WriteLine("size inside responseIntLength = " + responseLength);
int bytesReceived = 0;
while (bytesReceived < responseIntLength)
{
int bytesRead = streamserver.Read(response, bytesReceived, responseIntLength - bytesReceived);
bytesReceived += bytesRead;
}
System.Console.WriteLine("frameTask attivo 'r'n");
responseStream.Write(response, 0, responseIntLength);
//System.Threading.Thread.Sleep(1); //added this line
responseBytes = responseStream.ToArray();
Console.WriteLine("responseBytes Length = " + responseBytes.Length);
//}
}
}
catch (NullReferenceException ex)
{
System.Console.WriteLine("Errore: " + ex.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
try
{
//MessageData.Picture2 = (Bitmap)byteArrayToImage(Compressor.Compressor.Decompress(responseBytes));
MessageData.Picture2 = (Bitmap)byteArrayToImage(responseBytes);
CamImageBox.Image = MessageData.Picture2;
}
catch (NullReferenceException ex)
{
System.Console.WriteLine("Errore: " + ex.ToString());
}
catch (Exception ex)
{
System.Console.WriteLine("Errore: " + ex.ToString());
}
});
//frameTask.Start();
InitializeComponent();
}