排列数据包中的字节

发布于 2024-10-14 23:10:58 字数 567 浏览 3 评论 0原文

如果我需要创建这样的数据包:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长不大的小祸害 2024-10-21 23:10:58

嗯,当然,您需要六个以上的字节。一个简单的选择是使用 std::vector

std::vector<unsigned char> v;
v.push_back(5);
v.push_back(0);
v.push_back(0);
v.push_back(1);

std::string address = "www.google.com";
v.push_back(address.size());
std::copy(address.begin(), address.end(), std::back_inserter(v));

v.push_back(80);

// data is accessible as an array by using &v[0]

Well, you need more than six bytes, certainly. One straightforward option is to use a std::vector:

std::vector<unsigned char> v;
v.push_back(5);
v.push_back(0);
v.push_back(0);
v.push_back(1);

std::string address = "www.google.com";
v.push_back(address.size());
std::copy(address.begin(), address.end(), std::back_inserter(v));

v.push_back(80);

// data is accessible as an array by using &v[0]
来日方长 2024-10-21 23:10:58

为了获得您想要的内容,您不能将每个字段作为数据包数组中的特定固定索引,因为每个位置最多只能容纳一个字节。你必须做这样的事情:

char address[] = "www.google.com";
int addressLen = strlen(address);
char* packet = (char *) malloc(sizeof(char)*6+addressLen);
int i;

packet[0] = 5;
...
packet[3] = 1;
packet[4] = addressLen;

for (i = 0; i < addressLen; i++) 
   packet[i + 5] = address[i];

packet[4 + addressLen] = 80;

编辑:实际上我为 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:

char address[] = "www.google.com";
int addressLen = strlen(address);
char* packet = (char *) malloc(sizeof(char)*6+addressLen);
int i;

packet[0] = 5;
...
packet[3] = 1;
packet[4] = addressLen;

for (i = 0; i < addressLen; i++) 
   packet[i + 5] = address[i];

packet[4 + addressLen] = 80;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文