- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,这是我用来向我的程序发送 UDP 数据包进行测试的 php 脚本。
<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$pack = pack('C*', 4, 3, 1, 0);
socket_sendto($sock, $pack, strlen($pack), 0, '127.0.0.1', 1337);
socket_close($sock);
super 简单,pack一些二进制文件并通过网络将其发送到我的程序。一切都是已知的唯一值,因此我们可以确保将信息读入结构中的正确位置。
#![allow(non_snake_case)]
use std::net::UdpSocket;
use std::mem::size_of;
#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
struct ISP_TINY {
Size: u8, // Always 4
Type: u8, // Always ISP_TINY = 3
ReqI: u8, // 0 Unless return of a request for information.
SubT: u8, // Sub Type from the TINY_ enumeration.
}
fn main() -> std::io::Result<()>
{
// Some output so that we know the program has actally started.
println!("Running ...");
// Here we bind to the UDP socket, but the question mark allows us to return early if there is an error and end the program then and there.
let socket = UdpSocket::bind("0.0.0.0:1337")?;
// We'll read into this buffer space, but it has to be as big as or bigger than the largest packet your program can receive, otherwise it will cut off the rest of the data.
let mut buf = [0; 256];
loop
{
// recv_from blocks, and will wait (in sleep) until we get a packet to our bound socket.
let (bytes, socketAddress) = socket.recv_from(&mut buf)?;
// Once we get a packet, read from our buffer upto the packet length in bytes.
let packet = &mut buf[..bytes];
// Check that it's of the size we understand and want.
if bytes != size_of::<ISP_TINY>()
{
println!("Got packet of size {} need packet of size {}", bytes, size_of::<ISP_TINY>());
// Here we implicitly discard a packet we don't like and contiune the loop.
continue;
}
// When we get a packet we want, we print it's contents, it's size and where it came from.
println!("Packet Recv {:#?} of size {} from {}", packet, bytes, socketAddress);
}
}
为了让 Rust 满意,我已经进行了大量的实现。在过去的 24 小时里,我想我已经完成了大约 10 种不同的设计。他们似乎都不满意我提出的解决方案。
这看起来不像是一个难题。我有数据,我可以切入它吗?对吧?
let tiny = ISP_TINY {
Size: packet[0 .. 1],
Type: packet[1 .. 2],
ReqI: packet[2 .. 3],
SubT: packet[3 .. 4]
};
错
error[E0308]: mismatched types
--> src/main.rs:42:19
|
42 | Size: packet[0 .. 1],
| ^^^^^^^^^^^^^^ expected `u8`, found slice `[u8]`
error[E0308]: mismatched types
--> src/main.rs:43:19
|
43 | Type: packet[1 .. 2],
| ^^^^^^^^^^^^^^ expected `u8`, found slice `[u8]`
error[E0308]: mismatched types
--> src/main.rs:44:19
|
44 | ReqI: packet[2 .. 3],
| ^^^^^^^^^^^^^^ expected `u8`, found slice `[u8]`
error[E0308]: mismatched types
--> src/main.rs:45:19
|
45 | SubT: packet[3 .. 4]
| ^^^^^^^^^^^^^^ expected `u8`, found slice `[u8]`
error: aborting due to 4 previous errors
所以我对自己说,也许我需要创建一个实现......
impl ISP_TINY
{
fn from(&self, packet: &[u8])
{
self.Size: packet[0 .. 1],
self.Type: packet[1 .. 2],
self.ReqI: packet[2 .. 3],
self.SubT: packet[3 .. 4]
}
}
它也不喜欢那样。
error: expected one of `!`, `(`, `::`, `;`, `<`, or `}`, found `[`
--> src/main.rs:19:26
|
19 | self.Size: packet[0 .. 1],
| - ^ expected one of `!`, `(`, `::`, `;`, `<`, or `}`
| |
| tried to parse a type due to this
error: aborting due to previous error
这是我认为合理的 10 个解决方案中的两个。但它们不起作用,我不确定如何在 Rust 语言中解决这个问题。如何将数据包中的原始二进制数据读取到结构中!?
最佳答案
只需按整数索引即可获得单个字节,例如packet[0]
而不是 packet[0..1]
。这将为您提供一个 u8
而不是长度为 1 的切片。
用于将字节数组转换为更大的整数,例如u16
、i64
等,可以使用from_le_bytes等函数、from_be_bytes
和 from_ne_bytes
在 native 整数上定义。
关于rust - 将数据包读入 Rust 中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61280818/
使用 Python 的 rtmplib 绑定(bind)并遇到一些问题。 首先, 我有这样的东西: import librtmp conn = librtmp.RTMP(...) conn.con
基本上,我是在查看 Motorstorm 排行榜时在 PS3 上窃听数据包。排行榜以 XML 格式发送到我的 ps3,但只有在我获得授权后。那么有人可以告诉我这三个数据包之间发生了什么,以及我如何在浏
我正在努力了解 TCP,但解析大量 RFC 并没有帮助。我相信我了解连接和关闭握手,但我似乎无法找到任何总结实际数据流的内容。 在连接和关闭握手之间 TCP 数据包看起来像什么? (特别是标题) 最佳
我正在尝试通过 RCON 端口与我的 Minecraft 服务器通信。 虽然我不知道如何使用套接字和流的东西。四处寻找,我发现他们都有一些共同点。套接字、输入流和输出流。 我在我的代码中试过了,但返回
我正在 UDP 之上设计一个简单的协议(protocol),现在我意识到其他人可以将数据包发送到我正在监听的端口。这样的数据包对于我的应用程序来说显然是不正确的(我现在不担心安全问题) 是否有过滤这些
我目前有一个具有可自定义滴答率的游戏服务器,但在本示例中,我们建议服务器每秒仅滴答一次或 1hz。我想知道如果客户端发送速率比服务器快,因为我当前的设置似乎不起作用,那么处理传入数据包的最佳方法是什么
我无法理解网络字节顺序以及通过 UDP 发送和接收数据的顺序。我正在使用 C#。 我有一个结构保持: message.start_id = 0x7777CCCC; message.me
我正在为 USB 设备编写代码。假设 USB 主机开始控制读取传输以从设备读取一些数据,并且请求的数据量(设置数据包中的 wLength)是端点 0 最大数据包大小的倍数。那么在主机接收到所有数据后(
我有一台 Windows PC、Marvell 交换机、Netgear 交换机和一台 Ubuntu 机器连接在一起(通过 Netgear 交换机)。 我最近从 Windows PC 向 Marvell
在查看数据包字节码时,您将如何识别 dns 数据包。 IP header 的协议(protocol)字段会告诉后面有一个 UDP 帧,但是在 UDP 帧内没有协议(protocol)字段来指定接下来会
我有一个通过 udf 的 802.11 (wifi) 上各种类型的流量的 pcap。由于 MTU,udp(或更准确地说是 IP)对 wifi 数据包进行分段。我目前正在使用 SharpPcap 读取并
我正在开发的 Core Audio 应用程序上有此崩溃日志。我目前正在调试它,所以我的问题不是关于崩溃本身,而是关于 的含义“k”包 . 这是什么意思 ? 我已阅读 this , 和 this (关于
我在一台 VM Ubuntu 16.04 机器上的 100 个多播组上生成 UDP 数据包,并在另一台 VM Ubuntu 16.04 机器上订阅这些组。两者都在由 Hyper-V 管理器运行的 HP
这个问题在这里已经有了答案: How can I fix 'android.os.NetworkOnMainThreadException'? (66 个回答) 6年前关闭。 我正在尝试创建一个简单的
我正在寻找使用 Java 来欺骗 UDP 数据包。是否有任何好的 Java 库可以让您创建自己的 RAW SOCKETS? 最佳答案 我会使用包装 libpcap 的 Java API . libpc
我在基于 Tyrus 的客户端和 tomcat Web 服务器之间使用没有压缩的 websocket。我在 tomcat 端看到消息传入和传出我的套接字,但如果我设置一个wireshark来观察它们传
我的应用程序在模拟器中运行时无法接收 UDP 数据包。 UDP 数据包由“localhost”上的以下 java 程序通过端口 49999 发送。 DatagramSocket clien
我正在开发一个 Google Glass 应用程序,它需要在工作线程中监听 UDP 数据包(与发送 UDP 数据包的现有系统集成)。我之前发布了一个问题(请参阅 here )并收到了一个答案,其中提供
我正在从客户端向服务器发送两个数据包。我遇到的问题是,在服务器上读取的数据使两个字符串对于发送的最长字符串具有相同的长度。例如: 如果字符串 1 为:1234 字符串 2 为:abcdefghi 服务
我知道这是不好的做法,但是可以执行以下操作吗? Send packet1 to UDP port 1 port 1 receives packet1 and sends it to port 2 po
我是一名优秀的程序员,十分优秀!