Tcpclient-Tcpserver 接收到的帧的第二个字节数组变为负值 responseBytes Length = 347713
Second byte array of frame received by Tcpclient-Tcpserver goes negative responseBytes Length = 347713
请帮助我理解为什么当通过网络发送一些帧转换字节数组时第一帧(字节数组)正确到达,
而到达的第二个字节数组(第二帧)的大小错误甚至为负数。
这是发送帧的client-side代码:
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());
}
}
这是接收帧的 server-side 代码,不幸的是我得到的是正确的,
具有正确尺寸的框架只有第一个而不是第二个框架
注意:一些旧指令被描述而不是被删除
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
这是使用 HEADER 写入客户端发送方字节数组帧的大小然后我读取服务器端接收方的长度的课程
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 属性
- 响应字节长度 = 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).
对于接收到的第二帧字节数组中发生的情况,您有什么建议吗?*
您是否认为这是由服务器引起的,它在没有 ASYNC 方法的情况下以同步模式接收字节?
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);
这条消息是假的。那不是 "packet" 的大小。 TCP 不公开数据包。数组大小为 50,从 Read 读取的 return 值可能不同于 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();
}
请帮助我理解为什么当通过网络发送一些帧转换字节数组时第一帧(字节数组)正确到达, 而到达的第二个字节数组(第二帧)的大小错误甚至为负数。
这是发送帧的client-side代码:
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());
}
}
这是接收帧的 server-side 代码,不幸的是我得到的是正确的, 具有正确尺寸的框架只有第一个而不是第二个框架 注意:一些旧指令被描述而不是被删除
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
这是使用 HEADER 写入客户端发送方字节数组帧的大小然后我读取服务器端接收方的长度的课程
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 属性
- 响应字节长度 = 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).
对于接收到的第二帧字节数组中发生的情况,您有什么建议吗?* 您是否认为这是由服务器引起的,它在没有 ASYNC 方法的情况下以同步模式接收字节?
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);
这条消息是假的。那不是 "packet" 的大小。 TCP 不公开数据包。数组大小为 50,从 Read 读取的 return 值可能不同于 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();
}