"Alhazred" <a.********@email.itwrites:
I'm writing a client-server software which should emulate an email service
on a local machine. I'm now at the beginning and I'm trying to let
communicate the client and the server process, but have a problem with
GetMailslotInfo() in the server process inside the main() which returns with
error code 1 which means "Invalid Function" though it looks correct
to me.
You will have to ask in a Windows group about why GetMailslotInfo is
returning an error. This group deals with standard C.
You can see the code here:
http://lacasa.altervista.org/software/Server_01.c
http://lacasa.altervista.org/software/Client_01.c
I was looking to see if this was indeed Windows code, and I saw:
for( i = 0; (i < 99) && ((ch = getchar()) != EOF) && (ch != '\n'); i++ )
MyMessage[i] = (char)ch;
MyMessage[i]='\n'; // ritorno a capo
MyMessage[i+1] = '\0'; // termina la stringa
strcpy (request_message.req.da, MyMessage);
(and this pattern is repeated several times). The buffer MyMessage
and request_message.req.da are all arrays of size 100.
This is a convoluted way to put line from stdin into a buffer and in
all the convolution you have introduced a serious error. When the
input is long, the loop can terminate with i == 99. MyMessage[i+1]
invokes undefined behaviour when i == 99.
Every time you write a loop (for, while, do), invert the condition and
then look to make sure that the code following makes sense in all of
the cases that might terminate the loop:
!((i < 99) && ((ch = getchar()) != EOF) && (ch != '\n'))
implies that i == 99 or ch == EOF or ch == '\n' at the loop exit.
[Aside: your 99 should be calculated from the macro used to define the
arrays: MAX_DIMENSION - 1 and it should be '- 2' rather than '- 1', of
course].
Also, the above code is trying to do what the one line:
fgets(request_message.req.da, MAX_DIMENSION, stdin);
does.
--
Ben.