View topic - MsgReceive unexpected behaviour

MsgReceive unexpected behaviour

anything that doesn't fit to other groups.

MsgReceive unexpected behaviour

Postby Ichigeki » Wed Feb 08, 2012 10:32 am

Hi all,

I have two application:

msgserver.c
Code: Select all
#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) );
   }


}


and msgclient.c
Code: Select all
#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);
   }

}


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.
Ichigeki
New Member
 
Posts: 3
Joined: Wed Feb 08, 2012 10:01 am

Re: MsgReceive unexpected behaviour

Postby mario » Wed Feb 08, 2012 2:56 pm

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.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Re: MsgReceive unexpected behaviour

Postby Ichigeki » Wed Feb 08, 2012 4:16 pm

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

But it returns 2 both times.
Ichigeki
New Member
 
Posts: 3
Joined: Wed Feb 08, 2012 10:01 am

Re: MsgReceive unexpected behaviour

Postby Tim » Wed Feb 08, 2012 10:32 pm

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
Tim
Senior Member
 
Posts: 1469
Joined: Wed Mar 10, 2004 12:28 am

Re: MsgReceive unexpected behaviour

Postby Ichigeki » Thu Feb 09, 2012 7:07 am

Thank you for your help.
Ichigeki
New Member
 
Posts: 3
Joined: Wed Feb 08, 2012 10:01 am


Return to General Programming

Who is online

Users browsing this forum: No registered users and 3 guests