I’ve developed a small (very small) ftp client and server.
Both of these programs compile and run well only if they use the same arch.
- If client is Ubuntu amd64 and server is Ubuntu i386 i got a segfault (i think it is a pointer problem)
- If client is Ubuntu amd64 and server is Ubuntu amd64 all ok
- If client is Ubuntu i386 and server is Ubuntu i386 all ok
How can i made these 2 programs working on different archs?
It is a code problem? If so what i have to do?
Thanks in advance!
It is probably a difference in the size of various types and you are sending and receiving them across the network. Since the sizes don’t match up, the protocol will become desynchronized.
Linux uses the LP64 data model for x86_64 so
intis still 32-bits butlongis 64-bits. On x86longis 32-bits.Fortunately, the C standard defines several fixed-width integer types in the
stdint.hheader file. If you include that you can use the unsigned typesuintn_tand the signed typesintn_twith n being 8, 16, 32 or 64. Another thing you have take into consideration for network protocols is byte order. POSIX supplies the functionshtonl,htons,ntohl, andntohsthat convert 32- and 16-bit integers from host order to network order and vice versa, respectively.Edit:
As Variable Length Coder notes, sending whole structures is also not portable (though it is between x86 and x86_64). If you are sending a structure over the network you should send it member by member one at a time and assign into the appropriate fields on the receiving end.
Edit 2:
size_tis also not a portable data type. You should pick an explicitly sized type appropriate for the protocol. Probablyuint32_tis a good choice.Also, since all the size modifiers for
printfare based on the standardshort/int/longtypes, you need to use the macros in<inttypes.h>to make the format strings. For example:Your system may have man pages on these header files (my fedora system does) so at a command prompt you can use
and
to get more information on the types, macros and functions defined in these headers.