通过套接字接收C结构 [英] receiving a C struct via sockets

查看:65
本文介绍了通过套接字接收C结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过套接字将C结构从C app发送到C#服务器。我使用

BinaryReader类读取C char数组时偶尔会出现
错误。错误是:


"输出字符缓冲区太小而不能包含已解码的字符


读取C字符串的代码看起来像这样(br是二进制读取器)

私有字符串ReadString(int maxLen)

{

char [] str = br.ReadChars( maxLen);

int i = 0;

while(str [i]!= 0)

i ++;

返回新字符串(str,0,i);

}


我很确定这里发生的事情是我有垃圾

在0 NULL终结符之后的C char数组和C#试图对这些垃圾值进行编码




我的问题是我阅读这40个字节的最佳方式是什么?

只将其转换为C#字符串,只有NULL(0)字节位于C
$ b $中b char数组?

谢谢!

解决方案

将BinaryReader的编码设置为AsciiEncoding会修复此问题吗?

2008年9月8日星期一21:51:48 -0700,Mike Margerum< ju ** @ mail.comwrote:


将BinaryReader的编码设置为AsciiEncoding会解决这个问题吗?



我对此表示怀疑。但是对你来说这很简单,我不确定

为什么你在这里发布了这部分问题。


那个说,如果数据的编码是ASCII,那么你应该

肯定是设置适合的编码。如果我没记错的话,默认值是

UTF-8,由于编码重叠的方式,它也会处理ASCII数据。

。但恕我直言,这是一个更好的做法

来适当地设置编码。如果没有别的,它会使得b / b
损坏的数据更容易被检测到,因为编码器会知道没有
期待任何_but_有效的ASCII。


现在,就实际情况而言,如果你的字符串是可变的

长度并由空终止符分隔,那么你将不得不寻找

转换前读取的_bytes_中的null终止符,然后

然后只将已知为字符串一部分的字节传递给编码器,以便将
转换为C#字符( UTF-16)。


当然,如果字符串是恒定长度,那么只需读取该长度。

:)


Pete


Mike Margerum写道:


我从C发送一个C结构应用程序通过套接字到C#服务器。我使用

BinaryReader类读取C char数组时偶尔会出现
错误。错误是:


"输出字符缓冲区太小而不能包含已解码的字符


读取C字符串的代码看起来像这样(br是二进制读取器)

私有字符串ReadString(int maxLen)

{

char [] str = br.ReadChars( maxLen);

int i = 0;

while(str [i]!= 0)

i ++;

返回新字符串(str,0,i);

}


我很确定这里发生的事情是我有垃圾

在0 NULL终止符之后的C char数组和C#试图对这些垃圾值进行编码




我会编写一个扩展方法,或写一个

BinaryReader的后代,直接从BaseStream读取字节,直到0字节,

并从中进行转换;或者简单转换为ASCII,

或者通过缓存在MemoryStream中,List< byteor类似,然后通过编码获得



我还要确保BaseStream是一个BufferedStream,以避免调用例如
的性能成本一个NetworkStream寻找单个

字节。


- Barry


-
http://barrkel.blogspot.com/

I am sending a C struct from a C app via sockets to a C# server. I
occasionally get an error when reading a C char array using the
BinaryReader Class. the error is:

"The output char buffer is too small to contain the decoded characters"

The code that reads a C string looks like this (br is a binaryreader)
private string ReadString(int maxLen)
{
char[] str = br.ReadChars(maxLen);
int i = 0;
while (str[i] != 0)
i++;
return new string(str, 0, i);
}

I''m pretty sure what is happening here is I have garbage in the end of
the C char array after the 0 NULL terminator and C# is trying to encode
these garbage values.

My question is what is the best way for me to read this 40 bytes in and
converting it to a C# string only to where the NULL(0) byte is in the C
char array?
Thanks!

解决方案

Would setting the Encoding of the BinaryReader to AsciiEncoding fix this?


On Mon, 08 Sep 2008 21:51:48 -0700, Mike Margerum <ju**@mail.comwrote:

Would setting the Encoding of the BinaryReader to AsciiEncoding fix this?

I doubt it. But it''s so simple for you to check yourself, I''m not sure
why you posted that part of the question here.

That said, if the encoding for the data is ASCII, then you should
definitely be setting the encoding appropriate to that. The default is
UTF-8 if I recall correctly, and that will deal with ASCII data too just
because of the way the encoding overlap. But IMHO it''s a better practice
to set the encoding appropriately. If nothing else, it will make
corrupted data easier to detect, because the encoder will know to not
expect anything _but_ valid ASCII.

Now, as far as the actual situation goes, if your string is variable
length and delimited by a null terminator, then you will have to look for
the null terminator in the _bytes_ that were read before converting, and
then pass only the bytes known to be part of the string to an encoder for
conversion to C# characters (UTF-16).

Of course, if the string is constant length, then just read that length.
:)

Pete


Mike Margerum wrote:

I am sending a C struct from a C app via sockets to a C# server. I
occasionally get an error when reading a C char array using the
BinaryReader Class. the error is:

"The output char buffer is too small to contain the decoded characters"

The code that reads a C string looks like this (br is a binaryreader)
private string ReadString(int maxLen)
{
char[] str = br.ReadChars(maxLen);
int i = 0;
while (str[i] != 0)
i++;
return new string(str, 0, i);
}

I''m pretty sure what is happening here is I have garbage in the end of
the C char array after the 0 NULL terminator and C# is trying to encode
these garbage values.

I would write an extension method, or write a descendant of
BinaryReader, that directly read bytes from BaseStream until the 0 byte,
and do the conversion from that; either a simple conversion for ASCII,
or by buffering in a MemoryStream, List<byteor similar and thence
through an Encoding.

I''d also make sure the BaseStream is a BufferedStream to avoid
performance costs of calling e.g. a NetworkStream looking for single
bytes.

-- Barry

--
http://barrkel.blogspot.com/


这篇关于通过套接字接收C结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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