3

I currently have a basic client/server setup. The server needs to take requests from client(s) and need to respond to different request message types. An example of client request could be get list of files available, how many other clients are connected to the server, etc.

I would obviously have to figure out a way to determine the type of message from the message received from the sender. I was wondering, if I have a struct defined with necessary data, whehter if I can cast the struct to void*, send it as through send(sockfd, message, length, flags) syscall, and than on the receiver side, cast it back to the struct. This of course assumes that I am running the client and server in a same environment.

For example, if I have the following struct:

struct message {
   enum messageType { GET_FILES, GET_CLINET_NUMBER} messageType,
} 

and use this struct to send the message in following way

struct message msg;
msg.messageType = GET_FILES;
send(server_sockfd, (void*)&msg, sizeof(struct), 0)

and on the receiver,

recv(,msg_buffer);
struct message received = (struct message*)msg_buffer;

Ignoring the minor syntactical issues, can anyone advise if this scheme is possible? If not, is there any other way to pass a message without sending raw char*?

3 Answers 3

4

This is perfectly possible. Not recommended, though, because now you're using the same environment for the client and the server, but you may change in the future, and you'll have to change this code to be more platform/implementation independent.

Options? boost::serialization, XML-RPC, even HTTP/REST or any other high-level protocol that suits you, Google Protocol Buffers, CORBA, etc.

1

There are two things you need to be very mindful of:

  1. Endianness. If the endianness of your client and server every diverge you'll be in a situation where you can't communicate. Given today's heavy emphasis on x86 systems, you might convince yourself to feel comfortable with this limitation. If I were you, I would at a minimum, build in a validation. For example, at the beginning of every communication session send a known checksum, 0xdeadbeef for example. This would allow you to assert that the endianness between the client and server is the same, hopefully avoiding the continuation of the session and all kinds of potential bugs.

  2. Word Size. This is the more insidious and stronger reason why you should not do what you're thinking of doing. Unless you can strongly assert that the word size of the sender and receiver will never differ then your method is fraught with peril. Consider you have a struct as follows:

    struct some_msg { long payload; };

    What happens when a 32-bit gcc/linux system sends that message to a gcc/linux 64-bit system? Well, on the 32-bit system it will happily send out 4 bytes of data. However, the 64-bit system will overlay the struct onto 8 bytes of data. Bad news bears.

While it might be unlikely that you'll switch from x86 to say, sparc, it is almost a given that over time your systems will be 64-bit. Whether you have legacy 32-bit systems is unknown to me, but without very tight control over both client and server its almost guaranteed that you'll have a mixed word size environment.

I'm sure it can be made to work if you very specifically define and control the platforms being used. Further, you might limit your risk by using the fixed sized typedefs such as int32_t and uint64_t. You are playing with fire, IMO, if you do this. Any conceivable advantage is quickly reduced by the high level of lock-in your designing into your system.

2
  • Another, possibly even worse issue: padding bytes. C++ compilers will sometimes add 'invisible' padding bytes between members of the struct, in order to make memory access more efficient for the target CPU. Padding isn't standardized, so it could be done differently between one compiler and another one, or between two versions of the same compiler, or even between builds from the same compiler version (particularly if they are built with different optimization levels). So things are likely to break unless both ends of the connection are running the exact same executable binary. Commented Jun 5, 2011 at 18:35
  • Alignment is standardized on a given ISA ABI, and on any real-word machine, padding is the minimum needed for alignment. Moreover, for almost all types on real-world machines, the alignment is the size of the type. The alignment must always evenly divide the size of the type (due to C array representation semantics), so if you design your structures such that, in the absence of padding, each element would begin on a multiple of its size, then you can be essentially sure there will be no padding. Commented Jun 5, 2011 at 18:52
0

'can cast the struct to void*, send it as through send(sockfd, message, length, flags) syscall, and than on the receiver side, cast it back to the struct' - see Diego's answer. Note well that any protocol that cannot recover, or worse, will actually crash the server if one extra random byte is inserted/ deleted/ changed in the stream is insufficiently secure, transported over TCP or not.

'If not, is there any other way to pass a message without sending raw char*?' The only alternative to a protocol that can reliably and coerrectly reassmble messages from a byte, (octet), strean is to disconnect/reconnect after each message so terminating the stream. The performance/latency of this scheme is exactly what you would expect - really, really poor.

Rgds, Martin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.