Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a structure through unix domain socket

Tags:

c

linux

sockets

I am working on a project that is using Unix domain socket (AF_UNIX) as a choice of IPC between different processes.

When I want to pass a data structure from one process to another, do I need to do serialization on the data structure as mentioned in this question (Passing a structure through Sockets in C)?

Since these processes are compiled with same compiler and running on the same machine, there should be no endianness , nor different padding issue. So I am not sure if serialization is necessary.

like image 417
SSC Avatar asked May 14 '26 19:05

SSC


1 Answers

You need only ensure that the received structure is intelligible.

If the structure is composed of self-contained types then no processing is required, you can just call write() or send() to push the data into the socket.

Serialisation is needed where the structure is not self-contained ( eg if it contains pointers, or platform-specific data types)

If there a chance that the two processes could have different bit-ness (eg 32 bit vis 64 bit) or different endian-ness you'll want to take care that the struct is well-defined such that it comes out with the same binary representation in both forms.

like image 68
Jasen Avatar answered May 16 '26 08:05

Jasen