简体   繁体   English

通过c linux模块中的UDP套接字发送带有指针char的结构体

[英]send struct with pointer char over UDP socket in c linux module

I must send a struct between two machines via a udp socket the information I need is to send a structure as follows: 我必须通过udp套接字在两台计算机之间发送结构,我需要的信息是发送如下结构:

unsigned long x;

unsigned long y;

char *buf;

I have to send the struct in a single time. 我必须一次发送该结构。

My problem is: How can I handle this structure to, for example, set in one variable that I can send through the socket, especially as the size of the variable buf is not fixed 我的问题是:如何处理这种结构,例如设置一个可以通过套接字发送的变量,尤其是当变量buf的大小不固定时

Thank you for your help 谢谢您的帮助

You can't send a pointer, it makes no sense outside your process space. 您无法发送指针,在进程空间之外没有任何意义。 Instead, you have to serialize it, ie copy to an array and send that. 相反,您必须对其进行序列化,即复制到阵列并发送。 And before the string you also need to store its length. 并且在字符串之前,您还需要存储其长度。 You can try: 你可以试试:

char sendbuf[...];
int len = strlen(buf);

memcpy(sendbuf, &x, sizeof(x));
memcpy(sendbuf + sizeof(x), &y, sizeof(y));
memcpy(sendbuf + ..., &len, sizeof(len));

memcpy(sendbuf + ..., buf, len);

You will need to copy everything within your struct sequentially into a separate char buffer and then write that to the socket. 您将需要将结构中的所有内容依次复制到单独的char缓冲区中,然后将其写入套接字。 Optionially, because your char* buffer inside your struct is not of fixed length it is often a good idea to calculate the size of what you want to send and also write this as an integer at the start of your message so that at the other end the length of the packet that you are sending can be verified by your receiving socket. 可选地,由于结构中的char *缓冲区的长度不是固定的,因此通常最好计算要发送的内容的大小,并在消息的开头将其写为整数,以便在另一端您正在发送的数据包的长度可以通过接收套接字进行验证。

When unpacking the data at the other end, simply start at the beginning of your receive buffer and memcpy data into your values 在另一端解压缩数据时,只需从接收缓冲区的开头开始,然后将memcpy数据转换为值

char* message; char *消息; // This is the pointer to the start of your // received message buffer //这是指向//收到的消息缓冲区开始的指针

// TODO: assign message to point at start of your received buffer.


unsigned long xx;
unsigned long yy;
memcpy(&xx,message,sizeof(unsigned long));  // copy bytes of message to xx
message += sizeof(unsigned long);           // move pointer to where yy SHOULD BE 
                                            // within your packet    
memcpy(&yy,nessage,sizeof(unsigned long));  // copy bytes to yy
message += sizeof(unsigned long);           // message now points to start of 
                                            // the string part of your message

int iStringLength =   //  ?????? You need to calculate length of the string
char tempBuffer[1000]; // create a temp buffer this is just for illustration
                       // as 1000 may not be large enough - depends on how long
                       // the string is
memcpy(tempBuffer,message,iStringLength);

Then xx,yy contain your long values and tempBuffer contains the string. 然后xx,yy包含您的长值,tempBuffer包含字符串。 If you want the string to persist beyond the current scope then you will need to allocate some memory and copy it there. 如果您希望字符串在当前范围之外继续存在,则需要分配一些内存并将其复制到那里。 You can calculate the size of this string from the size of the whole message minus the size of the 2 unsigned long items (or using an extra item sent in your packet IF you did as I suggested above). 您可以通过整个消息的大小减去2个未签名的长项的大小来计算此字符串的大小(或者按照我上面的建议,也可以使用数据包中发送的额外项)。

I hope this clarifies what you need to do 我希望这可以澄清您需要做什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM