tcp客户端服务器响应消息

编程入门 行业动态 更新时间:2024-10-23 11:29:38
本文介绍了tcp客户端服务器响应消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

byte[] inStream = new byte[10025]; serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize); string returndata = System.Text.Encoding.ASCII.GetString(inStream); MessageBox.Show(returndata);

我写这段代码snipet用于客户端监听然而,服务器响应, System.dll中发生了'System.ArgumentOutOfRangeException'类型的未处理异常 我该如何修复tihs?

I write this code snipet for client listen to server response however, "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.dll" How can i fix tihs?

推荐答案

这不是很明显吗?你将一些数组大小硬编码为10025.为什么?你永远不应该这样做。难怪,如果你试图读取一些其他大小的数据,比如(int)clientSocket.ReceiveBufferSize ,进入你的缓冲区,那个缓冲区可能不够大。 显然,如果你根据所需尺寸进行硬编码就可以写出: int size Isn't that obvious? You hard-code some array size to 10025. Why? You should never do it. No wonder, if you try to read some amount of data of some other size, such as (int)clientSocket.ReceiveBufferSize, into your buffer, that buffer may be not big enough. Apparently, if will be just write if you create it without hard-coding based on required size: int size int size = (int)clientSocket.ReceiveBufferSize; byte[] inStream = new byte[size]; serverStream.Read(inStream, 0, size); // ...

祝你好运。

-SA

更多推荐

tcp客户端服务器响应消息

本文发布于:2023-11-25 09:06:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629155.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   消息   服务器   tcp

发布评论

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

>www.elefans.com

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