使用 UDP 传递结构
我一直在尝试使用 UDP 在同一台计算机上发送和接收结构,在这种情况下,服务器和客户端在同一台计算机上运行并共享通用的结构定义(使用头文件)。
Hostent 结构 defn(UNIX 内置类型):
struct hostent{
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
}
服务器代码片段如下:
struct hostent* resolved_host = DNS_translate(DNSname);
if((numbytes = sendto(sockfd, (void*)&resolved_host, sizeof(struct hostent), 0, (struct sockaddr *)&client_addr, sizeof(struct sockaddr))) == -1)
{
perror("sendto failed");
exit(EXIT_FAILURE);
}
客户端代码片段如下:
struct hostent resolved_host;
int addr_len = sizeof(struct sockaddr);
if((numbytes = recvfrom(sockfd, (void*)&resolved_host, sizeof(struct hostent), 0, (struct sockaddr *)&server_addr, &addr_len)) == -1)
{
perror("recvfrom failed");
exit(EXIT_FAILURE);
}
服务器正常发送且客户端接收(未引发错误)。 *resolved_host* 结构已填充到服务器中,并且可以毫无问题地访问其所有数据。 但是,如果我现在尝试在客户端中使用 *resolved_host* 结构,则会出现段错误。例如:
printf("Name : %s\n", resolved_host.h_name);
引发段错误。 (但可以在服务器中工作)
I have been trying to send and receive structures on the same machine using UDP and the server and client in this case run on the same machine and share common structure definitions (using a header file).
Hostent structure defn(UNIX built-in type) :
struct hostent{
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
}
Server Code snippet follows :
struct hostent* resolved_host = DNS_translate(DNSname);
if((numbytes = sendto(sockfd, (void*)&resolved_host, sizeof(struct hostent), 0, (struct sockaddr *)&client_addr, sizeof(struct sockaddr))) == -1)
{
perror("sendto failed");
exit(EXIT_FAILURE);
}
Client Code snippet follows:
struct hostent resolved_host;
int addr_len = sizeof(struct sockaddr);
if((numbytes = recvfrom(sockfd, (void*)&resolved_host, sizeof(struct hostent), 0, (struct sockaddr *)&server_addr, &addr_len)) == -1)
{
perror("recvfrom failed");
exit(EXIT_FAILURE);
}
The server sends and the client receives as normal (no error raised).
The *resolved_host* structure is filled in the server and all its data can be accessed with no problem.
However, if I now try to use the *resolved_host* structure in the client, I get a seg fault. For example:
printf("Name : %s\n", resolved_host.h_name);
raises a seg fault. (but works in the server)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的结构充满了指针。当您通过网络发送它时,您发送的是实际地址,而不是这些指针指向的数据。
这些地址在目标进程中无效。
您需要自己序列化数据。请参阅示例:
a 的序列化/反序列化C 中的 struct 到 char*
序列化技术
Your struct is full of pointers. When you send it over the network, you send the actual addresses, not the data pointed to by those pointers.
Those addresses are invalid in the target process.
You will need to serialize the data yourself. See for examples:
Serialization/Deserialization of a struct to a char* in C
Serialization techniques
该结构包含指针 - 因此,当您通过 UDP 复制该结构时,您仅复制这些指针的值(即其他一些的地址) > 数据片段)而不是实际数据本身。
当您在服务器中收到这些指针时,它们不再具有任何意义 - 这些指针地址对其他程序来说毫无意义。
The structure contains pointers - so when you copy the structure over UDP you're only copying the values of those pointers (i.e. the addresses of some other pieces of data) and not the actual data itself.
When you receive those pointers in the server they no longer mean anything - those pointer addresses are meaningless to the other program.
您正在发送指针。即使在同一台机器上,这些在不同的地址空间中也是无效的。
You are sending pointers. Even on the same machine these are not valid in different address spaces.