指定的参数超出了在c#中从网络获取数据的有效值范围(Specified argument was out of the range of valid value to get data from ne

编程入门 行业动态 更新时间:2024-10-24 17:25:35
指定的参数超出了在c#中从网络获取数据的有效值范围(Specified argument was out of the range of valid value to get data from network in c#)

我正在尝试向传感器发送命令并使用以下代码从中获取数据:

const int PORT_NO = 3000; const string SERVER_IP = "192.168.2.44"; //---listen at the specified IP and port no.--- IPAddress localAdd = IPAddress.Any; TcpListener listener = new TcpListener(localAdd, PORT_NO); Console.WriteLine("Listening..."); listener.Start(); //---incoming client connected--- TcpClient client = listener.AcceptTcpClient(); NetworkStream nwStream = client.GetStream(); //---write back the text to the client--- byte[] buffersend = new byte[client.ReceiveBufferSize]; buffersend = GetBytes("00010002000B0300010004C380"); int bytesSend = nwStream.Read(buffersend, 0, client.ReceiveBufferSize); // Console.WriteLine("Sending back : " + dataReceived); nwStream.Write(buffersend, 0, bytesSend); //---get the incoming data through a network stream--- byte[] buffer = new byte[client.ReceiveBufferSize]; //---read incoming stream--- int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize); //---convert the data received into a string--- string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine("Received : " + dataReceived); client.Close(); listener.Stop(); Console.ReadLine(); }

在这一行中int bytesSend = nwStream.Read(buffersend, 0, client.ReceiveBufferSize); 我收到了这个错误:

Specified argument was out of the range of valid value

buffersend为52, client.ReceiveBufferSize为8192

static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; }

我是c#socket编程的新手

I am trying to send a command to a sensor and get the data from it using this code :

const int PORT_NO = 3000; const string SERVER_IP = "192.168.2.44"; //---listen at the specified IP and port no.--- IPAddress localAdd = IPAddress.Any; TcpListener listener = new TcpListener(localAdd, PORT_NO); Console.WriteLine("Listening..."); listener.Start(); //---incoming client connected--- TcpClient client = listener.AcceptTcpClient(); NetworkStream nwStream = client.GetStream(); //---write back the text to the client--- byte[] buffersend = new byte[client.ReceiveBufferSize]; buffersend = GetBytes("00010002000B0300010004C380"); int bytesSend = nwStream.Read(buffersend, 0, client.ReceiveBufferSize); // Console.WriteLine("Sending back : " + dataReceived); nwStream.Write(buffersend, 0, bytesSend); //---get the incoming data through a network stream--- byte[] buffer = new byte[client.ReceiveBufferSize]; //---read incoming stream--- int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize); //---convert the data received into a string--- string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.WriteLine("Received : " + dataReceived); client.Close(); listener.Stop(); Console.ReadLine(); }

in this line int bytesSend = nwStream.Read(buffersend, 0, client.ReceiveBufferSize); i got this error :

Specified argument was out of the range of valid value

the buffersend is 52 and the client.ReceiveBufferSize is 8192

static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; }

I am so new in c# socket programming

最满意答案

而不是这个

byte[] buffersend = new byte[client.ReceiveBufferSize]; buffersend = GetBytes("00010002000B0300010004C380"); int bytesSend = nwStream.Read(buffersend, 0, client.ReceiveBufferSize); nwStream.Write(buffersend, 0, bytesSend);

我想你只想要这个。

byte[] buffersend = GetBytes("00010002000B0300010004C380"); nwStream.Write(buffersend, 0, buffersend.Length);

没有必要新建一个数组只是为了用GetBytes的结果替换它。 另外我不认为你想读入buffersend ,你只想写它。 然后你的后一个代码将读入buffer 。

您获得异常的原因是, buffersend NetworkStream.Read的文档, buffersend的长度小于client.ReceiveBufferSize

Instead of this

byte[] buffersend = new byte[client.ReceiveBufferSize]; buffersend = GetBytes("00010002000B0300010004C380"); int bytesSend = nwStream.Read(buffersend, 0, client.ReceiveBufferSize); nwStream.Write(buffersend, 0, bytesSend);

I think you just want this.

byte[] buffersend = GetBytes("00010002000B0300010004C380"); nwStream.Write(buffersend, 0, buffersend.Length);

There is no need to new up an array just to replace it with the results of GetBytes. Also I do not think you want to read into buffersend, you just want to write it. Your latter code will then read into buffer.

The reason you are getting the exception is that buffersend has a length that is less than client.ReceiveBufferSize per the documentation for NetworkStream.Read

更多推荐

本文发布于:2023-07-22 19:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222749.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有效值   参数   数据   网络   超出了

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!