Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 指定的参数超出了从c中的网络获取数据的有效值范围#_C#_Sockets_Buffer_Streamwriter - Fatal编程技术网

C# 指定的参数超出了从c中的网络获取数据的有效值范围#

C# 指定的参数超出了从c中的网络获取数据的有效值范围#,c#,sockets,buffer,streamwriter,C#,Sockets,Buffer,Streamwriter,我正在尝试向传感器发送命令,并使用以下代码从传感器获取数据: 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

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

 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编程非常陌生,而不是这个

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
,您只想编写它。您的后一个代码将被读入
缓冲区

出现异常的原因是
buffersend
的长度小于
client.ReceiveBufferSize
,而不是此

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
,您只想编写它。您的后一个代码将被读入
缓冲区


出现异常的原因是
buffersend
的长度小于
client.ReceiveBufferSize
,请参见
GetBytes
的文档?您可能需要将
客户端.ReceiveBufferSize
更改为
缓冲发送.Length
,以确保您请求的字节数不会超过缓冲区可以容纳的字节数。@juharr我更新了question@juharr它将字符串转换为字节,但不应
buffersend
仅用于
写入
?在用字节填充后,为什么要尝试将字节读入其中?我只是在猜测,但我想你可以先完全删除
Write
GetBytes
做什么?您可能需要将
客户端.ReceiveBufferSize
更改为
缓冲发送.Length
,以确保您请求的字节数不会超过缓冲区可以容纳的字节数。@juharr我更新了question@juharr它将字符串转换为字节,但不应
buffersend
仅用于
写入
?在用字节填充后,为什么要尝试将字节读入其中?我只是猜测,但我想你可以先完全删除
Write
。我应该用什么来代替tessend?@EhsanAkbar用
buffersend.Length
。谢谢你亲爱的朋友,但我没有收到传感器的任何数据?!!!我用了herculas,它能用什么来代替Tessend?@EhsanAkbar用
buffersend.Length
。谢谢你,亲爱的朋友,但我没有收到传感器的任何数据?!!!我用了赫库拉斯,它很管用