Results 1 to 3 of 3
I am currently playing around with Unix socket communications in C. I currently have two programs that just pass some data back and forth through domain sockets. Now, I am ...
- 10-05-2009 #1Just Joined!
- Join Date
- Oct 2009
- Posts
- 3
Unix domain socket communications (C/C++)
I am currently playing around with Unix socket communications in C. I currently have two programs that just pass some data back and forth through domain sockets. Now, I am trying to get a little more complex data passed back and forth. I am trying to switch from passing just a string to passing a struct. The struct would just contain an int and char array (basically create standard communication structs).
The way I have it pass strings now is just with simple read/writes to the socket.
What would be the best way to go about doing this?
- 10-05-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
First, sending structs via sockets is not generally a good idea. There are several problems with this. Some of them are:
1. You have to make sure that sender/receiver are compatible from the perspective of CPU, operating system, compiler use to build the application, compiler options, word size, etc.
2. Assuming that everything is compatible per #1 above, you need to make sure that the buffer receiving the message on either end is properly aligned, otherwise you are likely to have some nasty processor panics.
3. You have to deal with partial reads of the message and know how to determine where the end of the message is, bearing in mind that the way TCP/IP buffers data you can get from <1 to >1 message in one read.
Anyway, for all of the above reasons, most often TCP/IP messaging either uses network byte order for binary data, or some sort of textual wire format, which in either case the message is then decoded and the data placed in the appropriate structure for the use of your program. Even though it is quite verbose, XML is a popular wire format these days. I also use a variant of the TCL (Tool Command Language) wire format which is less verbose than XML, but fully orthogonal and component position independent (parts of your structure don't have to be in any specific order).Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 10-05-2009 #3Just Joined!
- Join Date
- Oct 2009
- Posts
- 3
Well, at the current moment, the server/client are running on the same machine, but it should be able to work from different ones.
I am new to development on Linux, so having a hard time following you. Let my try to explain what I want to do a little better.
I have struct that basically has an int that specifies the action to be taken (1 - lower case a string, 2 - uppercase a string). It also contains the string to be uppercased. I want to be able to write this data to the socket (server), then retrieve it and put it into the struct on the receiver (client). I would most likely create the struct in a header file and include it in both applications (I could also take recommendations on this, not sure what is the best way to share a struct between two apps). The client would then do the work, and send a different struct back with the transformed string and an int to signal if it wants more work or not.
By the way, I am using Unix domain sockets. Is it better to use the INET sockets instead?
Thanks for the help!


Reply With Quote