发送结构中使用recvfrom的()和SENDTO() [英] Sending structure using recvfrom() and sendto()
问题描述
我使用的 C语言这是服务器端和客户端一个通用平台
I am using C language which is a common platform for both the server and the client.
我有一个特定类型的结构,其欲从服务器发送到客户端。
I have a structure of a particular type which I want to send to the client from the server.
有关例如
服务器code
//necessary declarations
struct hostent *hp;
hp=gethostbyname("www.google.com");
sendto(sockDes,&hp,sizeof(hp),0,(struct sockaddr *)&cli_addr,sizeof(cli_addr));
客户端code 的
CLIENT CODE
struct hostent *hp;
msg=recvfrom(mysock,&hp,sizeof(hp),0,(struct sockaddr)&serv_addr,&size_len);
所以,基本上我想从服务器从上面的code片我得到分段错误发送结构以client.But,我不知道whther这样的结构转移是可行的,
有没有任何办法?
So , basically I want to send a structure from the server to the client.But from the above pieces of code I am getting segmentation faults , and I am not sure whther such structure transfer is feasible, IS THERE ANY WAY OUT?
推荐答案
首先,你必须以对结构hostent分配内存的malloc()。
您可以通过转移SENDTO()/ recvfrom的(一个结构),但由于结构hostent包含指针,这个结构是指针没有任何意义的成员尽快为他们转移到另一端。
First, you have to malloc() in order to allocate memory for the struct hostent. You can transfer a struct through sendto()/recvfrom() but since struct hostent contains pointers, the members of this struct that are pointers have no meaning as soon as they are transfered to the other end.
这篇关于发送结构中使用recvfrom的()和SENDTO()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!