client <-> server interaction in C

rl at rl at
Tue Nov 22 23:21:51 PST 2005

On 2005-11-22 10:48:00 +0000, Gerard Beekmans wrote:
> Attached server.c and client.c. Compile with "gcc server.c -o server" 
> and same for client. Run "server" followed by "client"
You have the first spokes in the wheel of ucspi-tcp:

The first step to programming C quickly and effieciently is to use 
existing libraries and programs. If you want to run programs securely 
and remotely, use ssh. You can have the romote machine restricted to 
running a command specified in /etc/ssh/sshd_config for the lfs 
account, or you can specify a command in the command line to ssh.

Ralfs died from lack of maintainance years ago, but you might like one 
of it components 'chrude': CHange Root, User, Directory and Environment. 
You can build this up out of chroot, su, bash, cd and env with some 
complex escaping of characters with special meaning to the shell. I 
found it quicker to write chrude than to debug bad escapes.

Missing header files:

In the beginning, C compilers assumed that is you do not say otherwise, 
functions take integer pararmeters, and return intergers. When C got 
standardised this behaviour was made mandatory (to support legacy code), 
despite the common knowledge that it is a really dumb thing to do. If 
you miss out header files, things very badly wrong when the arguments 
are not the same size as an int. Short and char get promoted to int, 
when used as an argument. On 32-bit machines, sizeof(int) == 
sizeof(void *), and many programs to not use floating point, so the 
disaster may not be obvious at first.

When things do go wrong, it will be because one of the arguments or 
return values if a long, long long, (float gets that promoted to) 
double, double, long double, or agregate type. The argument stack 
created by the call will not be aligned with the way that the library 
function reads arguments. This will cause library functions to receive 
random arguments. If the library returns a value in two registers, or 
a floating point register, the calling function will get random 
results. Things get worse if the return value is a structure - this 
is typically handled by adding an invisible first parameter that is 
a pointer to where the result should be stored. If you miss out the 
header files, some library functions will crap on some random piece 
of memory.

The result is a pain to debug, so do not miss out the header files. 
The following are the minimum required compiler switches for C:

-Wall    Turns on all warnings
-W       Turns on the other warnings
-Werror  All warnings should be treated as errors.
         (There is some excuse for missing out the last on for released 
         code when your code is being compiled by something other than
         gcc with glibc on linux).

I would also recommend -std=gnu99 -D_GNU_SOURCE, but that is personal 
taste. The first allows modern contructs in C without warnings, and 
both allow Gnu extensions (to the compiler, and the C library) which 
may make the program incompatible with other compilers and libraries.

Debugging C is difficult. I recommend not putting bugs in the code in 
the first place. The compiler will find plenty for you if you let it.

Ambition is a poor excuse for not having enough sense to be lazy.

More information about the alfs-discuss mailing list