tcp客户端服务器响应消息 [英] tcp client server response message

查看:156
本文介绍了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客户端服务器响应消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆