How is it possible to convert a string to a key_t
, in order to use it for creating a shared memory segment by using shmget
?
This is because the key for mapping the shared memory is being transmitted over TCP/IP.
Thanks in advance!
How is it possible to convert a string to a key_t
, in order to use it for creating a shared memory segment by using shmget
?
This is because the key for mapping the shared memory is being transmitted over TCP/IP.
Thanks in advance!
Make sure the receiver interprets the data in the same way that the sender put them on the line.
E.g., if the sender does
char buf[32];
*((key_t*)buf) = data;
and sends the 32 byte buffer over the net, the receiver should read them into a matching buffer and extract the key_t
using
data = *((key_t*)buf);
The key is created by a call to
ftok()
. The latter uses a file path and an 8bit value to do so.For the same file path and the same 8bit value
ftok()
(re-)creates the same key.So transfer the file path and the 8bit value (typically another
char
) and let the receiver callftok()
on the values received. This will create the same key as the sender uses.From
ftok()
's documentation: