• <bdo id='MwLzQ'></bdo><ul id='MwLzQ'></ul>

    <small id='MwLzQ'></small><noframes id='MwLzQ'>

    <legend id='MwLzQ'><style id='MwLzQ'><dir id='MwLzQ'><q id='MwLzQ'></q></dir></style></legend>

      <i id='MwLzQ'><tr id='MwLzQ'><dt id='MwLzQ'><q id='MwLzQ'><span id='MwLzQ'><b id='MwLzQ'><form id='MwLzQ'><ins id='MwLzQ'></ins><ul id='MwLzQ'></ul><sub id='MwLzQ'></sub></form><legend id='MwLzQ'></legend><bdo id='MwLzQ'><pre id='MwLzQ'><center id='MwLzQ'></center></pre></bdo></b><th id='MwLzQ'></th></span></q></dt></tr></i><div id='MwLzQ'><tfoot id='MwLzQ'></tfoot><dl id='MwLzQ'><fieldset id='MwLzQ'></fieldset></dl></div>

      1. <tfoot id='MwLzQ'></tfoot>

        指定的参数超出了有效值的范围.参数名称:size &amp;串口通讯

        Specified argument was out of the range of valid values. Parameter name: size amp; Serial Port Communication(指定的参数超出了有效值的范围.参数名称:size amp;串口通讯)
          <tbody id='8Tj7T'></tbody>

            <small id='8Tj7T'></small><noframes id='8Tj7T'>

            <legend id='8Tj7T'><style id='8Tj7T'><dir id='8Tj7T'><q id='8Tj7T'></q></dir></style></legend>
            • <bdo id='8Tj7T'></bdo><ul id='8Tj7T'></ul>
              <tfoot id='8Tj7T'></tfoot>

                <i id='8Tj7T'><tr id='8Tj7T'><dt id='8Tj7T'><q id='8Tj7T'><span id='8Tj7T'><b id='8Tj7T'><form id='8Tj7T'><ins id='8Tj7T'></ins><ul id='8Tj7T'></ul><sub id='8Tj7T'></sub></form><legend id='8Tj7T'></legend><bdo id='8Tj7T'><pre id='8Tj7T'><center id='8Tj7T'></center></pre></bdo></b><th id='8Tj7T'></th></span></q></dt></tr></i><div id='8Tj7T'><tfoot id='8Tj7T'></tfoot><dl id='8Tj7T'><fieldset id='8Tj7T'></fieldset></dl></div>

                1. 本文介绍了指定的参数超出了有效值的范围.参数名称:size &amp;串口通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要创建一个应用程序,该应用程序需要使用 TCP/IP 与现有软件进行通信,其中我的应用程序和其他应用程序都将使用下面指定的端口号.

                  I need to create an application which requires communicating to an existent software using TCP/IP, where both mine and the other application will be using the port number specified below.

                  private void frmScan_Load(object sender, EventArgs e)
                  {
                      clientSocket.Connect("100.100.100.30", 76545);
                  }
                  
                  public void msg(string mesg)
                  {
                      textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
                  }
                  
                  private void cmdSCANok_Click(object sender, EventArgs e)
                  {
                  
                      msg("Client Started");
                      NetworkStream serverStream = clientSocket.GetStream();
                      byte[] outStream = Encoding.ASCII.GetBytes("PCK|SCAN|5025066840471");
                  
                      serverStream.Write(outStream, 0, outStream.Length);
                      serverStream.Flush();
                  
                      byte[] inStream = new byte[10025];
                      serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
                      string returndata = Encoding.ASCII.GetString(inStream, 0, inStream.Length);
                  
                  
                      msg("Data from Server : " + returndata);
                  } 
                  

                  发生的情况是,我正在与之通信的程序有一些内置语言,它可以理解我发送的代码,并根据我发送的代码返回数据.所以在上面的代码中,我发送了三位代码: ("PCK|SCAN|5025066840471") 它将在数据库中找到一个特定的项目.当它运行时,我得到一个错误:

                  What happens is, the program I am communicating with has some in-built language where it will understand the code that I send, and it will return data according to the code that I have sent. So in the code above, I sent three bits of code: ("PCK|SCAN|5025066840471") which will find a specific item in the database. When it runs, I get an error on the line:

                  serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
                  

                  错误显示如下:指定的参数超出了有效值的范围.参数名称:大小"

                  the error shows the following: "Specified argument was out of the range of valid values. Parameter name: size"

                  我按照我在这个网站上看到的教程进行操作:http://csharp.net-informations.com/communications/csharp-client-socket.htm - 但我的做法略有不同.所以不要放

                  I followed the tutorial I saw on this website: http://csharp.net-informations.com/communications/csharp-client-socket.htm - But I did slightly different. So instead of putting

                  string returndata = Encoding.ASCII.GetString(inStream);
                  

                  我写道:

                  string returndata = Encoding.ASCII.GetString(inStream, 0, inStream.Length); 
                  

                  我对为什么会遇到这些问题感到非常困惑,老实说,我不太了解代码在做什么,我只是有一个粗略的想法,但不足以解决这个问题.有人可以帮忙吗?

                  I am extremely confused on why I am getting those problems, and to be honest I am not understanding much of what the code is doing, I just have a rough idea, but not enough to troubleshoot this. Can someone help please?

                  非常感谢!

                  PS:我正在 Visual Studio 2010 上为 Windows CE(便携式设备)编程.

                  PS: I am programming for Windows CE (portable device) on Visual Studio 2010.

                  推荐答案

                  您的代码是如何不进行 TCP 通信的一个很好的例子.我已经多次看到这段代码被复制了很多次,我很乐意为您指出一个关于 TCP 的好教程 - 可惜我还没有看到一个:)

                  Your code is a great example of how not to do TCP communication. I've seen this code copied over and over many times, and I'd be very happy to point you to a good tutorial on TCP - too bad I haven't seen one yet :)

                  让我先指出一些错误:

                  • TCP 不保证数据包以一串字节的形式到达.因此(理论上)Write 操作可能会导致拆分,需要在另一侧进行两次读取.通过 TCP 发送没有标头的数据是一个非常糟糕的主意 - 接收方不知道它必须读取多少.所以你有两个选择——要么在数据本身之前写入整组数据的长度,要么使用控制字符来结束数据包"
                  • 第一点也应该澄清你的阅读也是错误的.读取整个命令"可能需要不止一次读取操作,或者一次读取操作可能会同时给您两个命令!
                  • 您正在将 ReceiveBufferSize 字节读入 10025 长缓冲区.ReceiveBufferSize 可能比您的缓冲区大.不要这样做 - 读取 inStream.Length 的最大计数.如果您使用 C++ 进行编码,这将是一个很好的缓冲区溢出示例.
                  • 当您将数据转换为字符串时,您预计整个缓冲区已满.很可能并非如此.相反,您必须存储 read 调用的返回值——它告诉您实际读取了多少字节.否则,您正在读取垃圾,并且基本上有 另一个 缓冲区溢出.
                  • TCP doesn't guarantee you the packet arrives as one bunch of bytes. So (theoretically) the Write operation could result in a split, requiring two reads on the other side. Sending data without headers over TCP is a very bad idea - the receiving side has no idea how much it has to read. So you've got two options - either write the length of the whole bunch of data before the data itself, or use a control character to end the "packet"
                  • The first point should also clarify that your reading is wrong as well. It may take more than a single read operation to read the whole "command", or a single read operation might give you two commands at once!
                  • You're reading ReceiveBufferSize bytes into a 10025 long buffer. ReceiveBufferSize might be bigger than your buffer. Don't do that - read a max count of inStream.Length. If you were coding in C++, this would be a great example of a buffer overflow.
                  • As you're converting the data to a string, you're expecting the whole buffer is full. That's most likely not the case. Instead, you have to store the return value of the read call - it tells you how many bytes were actually read. Otherwise, you're reading garbage, and basically having another buffer overflow.

                  因此,一个更好(尽管仍远非完美)的实现应该是这样的:

                  So a much better (though still far from perfect) implementation would be like this:

                  NetworkStream serverStream = clientSocket.GetStream();
                  byte[] outStream = Encoding.ASCII.GetBytes("PCK|SCAN|5025066840471");
                  
                  // It would be much nicer to send a terminator or data length first, 
                  // but if your server doesn't expect that, you're out of luck.
                  serverStream.Write(outStream, 0, outStream.Length);
                  
                  // When using magic numbers, at least use nice ones :)
                  byte[] inStream = new byte[4096];
                  
                  // This will read at most inStream.Length bytes - it can be less, and it
                  // doesn't tell us how much data there is left for reading.
                  int bytesRead = serverStream.Read(inStream, 0, inStream.Length);
                  
                  // Only convert bytesRead bytes - the rest is garbage
                  string returndata = Encoding.ASCII.GetString(inStream, 0, bytesRead);
                  

                  哦,我不得不推荐 这篇关于 TCP 协议设计的文章.

                  它谈到了关于 TCP 的许多误解,最重要的是请参阅消息框架部分.

                  It talks about many of the misconceptions about TCP, most importantly see the Message Framing part.

                  这篇关于指定的参数超出了有效值的范围.参数名称:size &amp;串口通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                2. <small id='foxw9'></small><noframes id='foxw9'>

                    <legend id='foxw9'><style id='foxw9'><dir id='foxw9'><q id='foxw9'></q></dir></style></legend>
                      <i id='foxw9'><tr id='foxw9'><dt id='foxw9'><q id='foxw9'><span id='foxw9'><b id='foxw9'><form id='foxw9'><ins id='foxw9'></ins><ul id='foxw9'></ul><sub id='foxw9'></sub></form><legend id='foxw9'></legend><bdo id='foxw9'><pre id='foxw9'><center id='foxw9'></center></pre></bdo></b><th id='foxw9'></th></span></q></dt></tr></i><div id='foxw9'><tfoot id='foxw9'></tfoot><dl id='foxw9'><fieldset id='foxw9'></fieldset></dl></div>
                          <tbody id='foxw9'></tbody>
                        <tfoot id='foxw9'></tfoot>

                          <bdo id='foxw9'></bdo><ul id='foxw9'></ul>