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
。谢谢你,亲爱的朋友,但我没有收到传感器的任何数据?!!!我用了赫库拉斯,它很管用