排列数据包中的字节
如果我需要创建这样的数据包:
field 1: SOCKS protocol version, 1 byte
field 2: status, 1 byte:
field 3: reserved, must be 0x00
field 4: address type, 1 byte:
field 5: destination address of
1 byte of name length followed by the name for Domain name
field 6: network byte order port number, 2 bytes
char packet[6];
packet[0] = 5;
packet[1] = 0;
packet[2] = 0;
packet[3] = 1;
packet[4] = /* ... ???? ... */;
packet[5] = 80;`
How do I write the packet[4]
(field 5) for www.google.com
?提前致谢。
If I need to create a packet like this:
field 1: SOCKS protocol version, 1 byte
field 2: status, 1 byte:
field 3: reserved, must be 0x00
field 4: address type, 1 byte:
field 5: destination address of
1 byte of name length followed by the name for Domain name
field 6: network byte order port number, 2 bytes
char packet[6];
packet[0] = 5;
packet[1] = 0;
packet[2] = 0;
packet[3] = 1;
packet[4] = /* ... ???? ... */;
packet[5] = 80;`
How do I write the packet[4]
(field 5) for www.google.com
? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,当然,您需要六个以上的字节。一个简单的选择是使用
std::vector
:Well, you need more than six bytes, certainly. One straightforward option is to use a
std::vector
:为了获得您想要的内容,您不能将每个字段作为数据包数组中的特定固定索引,因为每个位置最多只能容纳一个字节。你必须做这样的事情:
编辑:实际上我为 C 做了这个例子。也应该在 C++ 中工作,但我不知道你正在使用的网络库的接口。该 malloc 可能会被新的 malloc 取代。或者您可以使用标准容器,因为我认为它们也可以作为数组访问。
In order to have what you want you can't have each field as a specific fixed index in the packet array because each position can only hold atmost one byte. You'd have to do something like this:
EDIT: Actually I made this example for C. Should work in C++ aswell but I don't know the interface of the network library you're using. That malloc probably could be replaced by a new. Or you could use the standard containers since I think those can be accessed as an array aswell.