[英]Send and receive struct in c - socket programming
我有一个结构定义如下:
struct controlMessage
{
struct iphdr cmIphdr; //this use ip.h fronm linux library
struct ipPayload cmIpPayload;
};
struct ipPayload
{
uint8_t mType;
uint16_t cid;
char* payload; //this is intended to send different size of content
}
然后,发送方将设置该结构并将controlMessage发送给接收方
struct controlMessage *cMsg = (struct controlMessage*) malloc (sizeof(struct controlMessage);
cMsg->cmIphdr.protocol = 200; //this is not important, could be any number
//etc wth all the other fields in iphdr
//My question is in the ipPayload struct
cMsg->cmIpPayload.mType = 82; //this is fine
cMsg->cmIpPayload.cid = 1; //this is fine
//Now I want to point my char pointer to a struct and send it as content
struct controlMsg_payload
{
uint16_t somePort;
//will be more stuffs, but keep it 1 for now for simplicity
}
struct controlMsg_payload *payload = (struct controlMsg_payload*) malloc(sizeof(struct controlMsg_payload));
payload->somePort = 1000; //just assign a static number to it
cMsg->cmIpPayload.payload = (char*) payload; //not sure if i did it right
//Now sends it using sendto
data = sendto(sockfd, cMsg, sizeof(struct controlMessage), 0, (struct sockaddr*) &rcv_addr, sizeof(rcv_addr));
//remove error check for simplicity
我不确定是否发送正确,但是在接收方,我可以正确检索所有信息,但struct controlMsg_payload中的somePort信息除外。
下面是我在接收方的代码
char* buf = malloc(sizeof(struct controlMessage));
data = recvfrom(sockfd,buf,sizeof(struct controlMessage), 0, struct (sockaddr*) &serv_addr, &serv_len);
struct iphdr* ip_hdr;
struct ipPayload* ip_payload;
ip_hdr = (struct iphdr*) buf;
ip_payload = (struct ipPayload*) (buf+sizeof(struct iphdr));
//from here I can print mType and cid correctly from ip_payload and also src & dest IP addr from ip_hdr
//The only information I did not get correctly is the somePort info below
struct controlMsg_payload* rcv_load;
rcv_load = (struct controlMsg_payload*) ip_payload->payload; //get this from the struct ipPayload and cast it as it is char* type, not sure if I did it right
printf ("my port = %d\n",rcv_load->somePort); //WRONG, not = 1000
我为许多代码感到抱歉,因为没有它很难解释。 基本上,我不会在接收器端读取somePort = 1000。
为了测试目的,我在发送方尝试了最后三行(在接收方),我能够读回1000。因此,我相信这必须是我打包数据并通过它发送/接收数据的方式导致这种情况的网络。
你们能帮我看看吗? 提前致谢!
您正在发送指向有效负载的指针,而不是有效负载本身。 有效负载指针在接收方没有任何意义。 您需要将有效负载中的所有数据发送到接收器,以使其正常工作。
使用Wireshark查看您实际发送给另一端的内容。 一旦看到正确的数据,就可以开始调试接收器。 在这种情况下,您仅发送标头,而从不发送有效负载。
/* This sends the header... */
data = sendto(sockfd, cMsg, sizeof(struct controlMessage), ...
发送标头后,您需要发送有效负载。
sendto(sockfd, payload, sizeof(struct controlMsg_payload), ...
头中指向有效负载的指针( char* payload;
)对接收者而言毫无意义。 如果您期望sendto
函数跟随指针并发送有效负载,那不是它的工作原理。 sendto
实际上只是发送指针的值。
您可以发送指针而只是在接收器上忽略它,或者更好的是您根本无法发送它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.