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()
). 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. 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. 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. 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.
精彩评论