Client-Server communication problem with mailslot

A

Alhazred

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.

Can you understand why does that happen?

You can see the code here:
http://lacasa.altervista.org/software/Server_01.c
http://lacasa.altervista.org/software/Client_01.c
http://lacasa.altervista.org/software/input.txt
the last one must be inside the folder where you put the server process and
it is needed to use the "Read email" option.

That host doesn't support direct linking, then you have to copy and paste
the links into the browser address bar.
 
B

Ben Bacarisse

Alhazred said:
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.

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 = (char)ch;
MyMessage='\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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top