以代表大小的4个字节发送消息头 [英] Sending message header as 4 bytes that represent size

查看:570
本文介绍了以代表大小的4个字节发送消息头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有小型C#服务器

I have small c# server

while (clientSocket.Connected)
            {
                byte[] streamBuffer = new byte[4];
                int streamLenght = this.clientStream.Read(streamBuffer, 0, 4);
                if (streamLenght == 0) break;
                int sizeofnewdata = BitConverter.ToInt32(streamBuffer, 0);
                streamBuffer = new byte[sizeofnewdata];
                streamLenght = this.clientStream.Read(streamBuffer, 0, sizeofnewdata);
                if (streamLenght == 0) break;
            }



想法是使用前4个字节作为要到达的新数据的大小标头.
问题是,客户端是用C ++编写的.

什么是在c ++中使用send()仅发送第一个4字节的最简单方法,以便我可以通过C#中的BitConverter.ToInt32对其进行解码?


问题是如何将sizeof()转换为无符号字符,然后通过套接字发送该无符号字符,以便我可以将其解码为Int32

另外,客户端未使用.NET Framework.

预先谢谢你
Filip



Idea is to use first 4 bytes as an size header for new data that will arrive.
Problem is, client is being written in C++.

What would be easiest way to send first just 4 bytes using send() in c++ so I can decode them via BitConverter.ToInt32 in C#?


Question is how to convent sizeof() to unsigned char and then send that unsigned char over socket so i can decode it as Int32

Also, client is not using .NET framework.

Thank you in advance
Filip

推荐答案

newzion写道:
newzion wrote:

首先发送最简单的方法是在c ++中使用send()4个字节

What would be easiest way to send first just 4 bytes using send() in c++


使用发送 [


Using the send[^] function, of course.
:)


这篇关于以代表大小的4个字节发送消息头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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