dongtangu6889 2015-07-16 08:49
浏览 55

Php字节数据包

I have a c# library which practically starts listening on a tcpip server an accepts a buffer of a certain size.

I need to send this packet as Byte array from php over the socket in a form of byte array or equivalent.

The packet is constructed for example byte[1] (a flag) is a number from 0 to 255 and byte[6] to byte[11] contains a float number in a string fromat for example: 005.70 which takes 6 bytes representing every character.

I managed to send the flag but when i try to send the float number it does not convert on the other side (C#).

So my question how can i send a byte array to c# using php?

From the C# part the conversion is being handled as follows:

float.Parse(System.Text.Encoding.Default.GetString(Data, 6, 6));
  • 写回答

1条回答 默认 最新

  • dongqiulei1987 2015-07-16 08:59
    关注

    Just after i have posted the question i have dictated my answer. I am not 100% sure if this is the right way but it managed to convert correctly.

    Here is the answer:

    I created an array of characters and escaped the flag (4) to be the actual byte value being (4) but i didn't escape the money value

    $string = array (0=>"\0", 1=>"\4", 2=>"\0", 3=>"\0", 4=>"\0", 5=>"\0", 6=>"5", 7=>".", 8=>"7", 9=>"\0", 10=>"\0");
    

    Imploded all together with nothing as glue:

    $arrByte =  implode("", $string);
    

    and sent over the opened socket:

    $success = @fwrite($this->socket, $arrByte);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值