简体   繁体   English

在套接字编程中返回指向结构的指针

[英]Returning pointer to structure in socket programming

In one of my projects an opendir() call in a client program is intervened using LD_PRELOAD and sent to a file sever for its processing (I do some logging in the server before calling opendir() ).在我的一个项目中,客户端程序中的opendir()调用使用 LD_PRELOAD 进行干预,并发送到文件服务器进行处理(我在调用opendir()之前登录服务器)。 Thus the call passes through a socket.因此,调用通过一个套接字。 Now how do I return the pointer to a DIR structure (which is returned from the opendir() call) from the server to the client through the socket.现在如何通过套接字将指向 DIR 结构的指针(从opendir()调用返回)从服务器返回到客户端。 Do I convert it to string and send?我是否将其转换为字符串并发送?

Client and server have different address spaces, even if they are on the same machine.客户端和服务器有不同的地址空间,即使它们在同一台机器上。 Therefore you cannot possibly send a pointer through a socket and expect it to do anything meaningful at the other end.因此,您不可能通过套接字发送指针并期望它在另一端做任何有意义的事情。

One valid way of doing such a thing would be to place the structure in a shared memory section, and send an offset into the shared memory region.一种有效的方法是将结构放在共享的 memory 部分中,并将偏移量发送到共享的 memory 区域中。 Obviously that will also only work if client and server are on the same physical machine.显然,这也只有在客户端和服务器位于同一台物理机器上时才有效。
EDIT:编辑:
And, even though this will work for reading the data, you should (obviously) beware of calling functions like closedir or otherwise freeing the memory from the other process.而且,即使这对读取数据有效,您也应该(显然)提防调用诸如closedir之类的函数或以其他方式将 memory 从其他进程中释放出来。 Such things almost certainly mean terribly bad luck (in the sense of: now you're going to die).这样的事情几乎可以肯定意味着非常糟糕的运气(从某种意义上说:现在你要死了)。

In the "normal" case of client and server being on different machines, there is no way you can do such a thing at all.在客户端和服务器位于不同机器上的“正常”情况下,您根本无法做这样的事情。 You can send string data or serialized data just fine of course, but never pointers or anything similar.当然,您可以发送字符串数据或序列化数据,但不能发送指针或类似的东西。

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

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