MsgReceive unexpected behaviour

Hi all,

I have two application:

msgserver.c

[code]#include <sys/neutrino.h>
#include <stdio.h>
#include <inttypes.h>
#include <errno.h>

void main(){

int receivedId;
int channelId;
char message[512];

channelId = ChannelCreate(0);
printf("Channelcreated. Id: %d\n", channelId);

while(1){
	receivedId = MsgReceive( channelId, message, sizeof(message), NULL );

	printf("Server: Message id: %d, message text: %s\n", receivedId, message);

	sprintf(message, "This is a reply to message with id(%d)\n", receivedId);
	MsgReply( receivedId, EOK, message, sizeof(message) );
}

}[/code]

and msgclient.c

[code]#include <sys/neutrino.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char** argv){

char *smsg = "msg";
char rmsg[512];

int connectionId;

connectionId = ConnectAttach(0, atoi(argv[1]), 1, 0, 0 );

if( connectionId == -1){
	fprintf(stderr, "Connection attach error!\n");
	return EXIT_FAILURE;
}


if( MsgSend(connectionId, smsg, strlen(smsg) + 1,
	rmsg, sizeof(rmsg) ) == -1 )
{

	fprintf(stderr, "MsgSend error!\n");
	return EXIT_FAILURE;
}

if( strlen(rmsg) >0){
	printf("Received message: %s\n", rmsg);
}

}[/code]

when I run msgclient, msgserver prints following:
Server: Message Id: 2, message text: msg
Server: Message Id: 2, message text:

and msgclient gets reply as expected.

Client sends the message only once, So the quiestion is: why the second “empty” message is received?
Actually this message is not emty, it contains 3 symbols: SYN, SOH and EOT (0x16, 0x1 and 0x4)

Is it correct behaviour? I need server to receive only one message that I send from client.

You need to check for the return value of MsgReceived. You are most probably receiving a pulse telling the client that the server gone away, more precisely that the channel was closed.

MsgReceive not only receive messages but also pulses. Pulse is a small message which gets put in your message array.

As I understand, MsgReceive returns 0 when pulse is received.

But it returns 2 both times.

Mario is right.

The 2nd message is the close of the client connection to your server.

You can see this if you put a ‘sleep (5);’ in the client before you exit. Then you’ll see that 2nd message printed on the server after 5 seconds when the client actually exits.

Tim

Thank you for your help.