message passing between threads

Hello,

I met some problems when writing message passing between threads. I just
want to
implement the function -client send message to several several times, and
server
got the message and reply the client.Firstly how to define “EOK” in
MsgReply() function?
And if I define EOK is 1, the running result seems uncorrect since server
just receive
the message just once whereas the client send message ten times.

Thanks,

Belinda


include <sys/neutrino.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>


int chid;

void* client() {
char *smsg=“This is the outgoing message”;
int i;
char rmsg[200];
int coid;

coid =ConnectAttach(0, 0, 1, 0, 0 );
if (coid==-1) {
printf( " Couldn’t ConnectAttach to 0/0/1!\n");
perror(NULL);
exit ( EXIT_FAILURE);
}
for (i=0;i<10;i++) {
if (MsgSend (coid, smsg, strlen(smsg) +1, rmsg, sizeof(rmsg))==-1) {
fprintf(stderr, “Error during MsgSend \n”);
perror(NULL);
exit(EXIT_FAILURE);

}
if(strlen(rmsg)>0)

printf(“Process returns “%s”\n”, rmsg);
}
}
}

void* server()
{
int rcvid;
int chid;
int pid;

char message[512];

chid=ChannelCreate(0);
while (1) {
rcvid=MsgReceive(chid, message, sizeof(message), NULL);
printf(“Got a message, rcvid is %X \n”, rcvid);
printf(“Message was received”%s".\n", message);
MsgReply(rcvid,EOK,NULL,0)
}
}

int main() {
pthread_t pid1,pid2;

pthread_create(&pid1, NULL, server, NULL);
pthread_create(&pid2, NULL, client, NULL);
return 0;

}

EOK should be defined as 0, although you should not define it yourself since
it is already defined in <errno.h>. You should include that header file
rather than defining EOK yourself.

“Belinda” <belinda_ye@hotmail.com> wrote in message
news:a49c70$t5f$1@inn.qnx.com

Hello,

I met some problems when writing message passing between threads. I just
want to
implement the function -client send message to several several times, and
server
got the message and reply the client.Firstly how to define “EOK” in
MsgReply() function?
And if I define EOK is 1, the running result seems uncorrect since server
just receive
the message just once whereas the client send message ten times.

Thanks,

Belinda


include <sys/neutrino.h
#include <stdio.h
#include <pthread.h
#include <stdlib.h
#include <string.h


int chid;

void* client() {
char *smsg=“This is the outgoing message”;
int i;
char rmsg[200];
int coid;

coid =ConnectAttach(0, 0, 1, 0, 0 );
if (coid==-1) {
printf( " Couldn’t ConnectAttach to 0/0/1!\n");
perror(NULL);
exit ( EXIT_FAILURE);
}
for (i=0;i<10;i++) {
if (MsgSend (coid, smsg, strlen(smsg) +1, rmsg, sizeof(rmsg))==-1) {
fprintf(stderr, “Error during MsgSend \n”);
perror(NULL);
exit(EXIT_FAILURE);

}
if(strlen(rmsg)>0)

printf(“Process returns “%s”\n”, rmsg);
}
}
}

void* server()
{
int rcvid;
int chid;
int pid;

char message[512];

chid=ChannelCreate(0);
while (1) {
rcvid=MsgReceive(chid, message, sizeof(message), NULL);
printf(“Got a message, rcvid is %X \n”, rcvid);
printf(“Message was received”%s".\n", message);
MsgReply(rcvid,EOK,NULL,0)
}
}

int main() {
pthread_t pid1,pid2;

pthread_create(&pid1, NULL, server, NULL);
pthread_create(&pid2, NULL, client, NULL);
return 0;

}

I have included <errno.h> in my file, however, the client still didn’t
receive
reply message from the server, moreover, I send message from client to
server ten
times, however, the server just receive message one time.

Thanks,

“David LeBlanc” <dleblanc@qnx.com> wrote in message
news:a49csv$l8c$1@nntp.qnx.com

EOK should be defined as 0, although you should not define it yourself
since
it is already defined in <errno.h>. You should include that header file
rather than defining EOK yourself.

“Belinda” <> belinda_ye@hotmail.com> > wrote in message
news:a49c70$t5f$> 1@inn.qnx.com> …
Hello,

I met some problems when writing message passing between threads. I just
want to
implement the function -client send message to several several times,
and
server
got the message and reply the client.Firstly how to define “EOK” in
MsgReply() function?
And if I define EOK is 1, the running result seems uncorrect since
server
just receive
the message just once whereas the client send message ten times.

Thanks,

Belinda


include <sys/neutrino.h
#include <stdio.h
#include <pthread.h
#include <stdlib.h
#include <string.h


int chid;

void* client() {
char *smsg=“This is the outgoing message”;
int i;
char rmsg[200];
int coid;

coid =ConnectAttach(0, 0, 1, 0, 0 );
if (coid==-1) {
printf( " Couldn’t ConnectAttach to 0/0/1!\n");
perror(NULL);
exit ( EXIT_FAILURE);
}
for (i=0;i<10;i++) {
if (MsgSend (coid, smsg, strlen(smsg) +1, rmsg, sizeof(rmsg))==-1)
{
fprintf(stderr, “Error during MsgSend \n”);
perror(NULL);
exit(EXIT_FAILURE);

}
if(strlen(rmsg)>0)

printf(“Process returns “%s”\n”, rmsg);
}
}
}

void* server()
{
int rcvid;
int chid;
int pid;

char message[512];

chid=ChannelCreate(0);
while (1) {
rcvid=MsgReceive(chid, message, sizeof(message), NULL);
printf(“Got a message, rcvid is %X \n”, rcvid);
printf(“Message was received”%s".\n", message);
MsgReply(rcvid,EOK,NULL,0)
}
}

int main() {
pthread_t pid1,pid2;

pthread_create(&pid1, NULL, server, NULL);
pthread_create(&pid2, NULL, client, NULL);
return 0;

}

\

Hmm, I compiled the example myself and it appeared to work as expected.
What might be throwing you off is the lack of verbosity, since your server
isn’t actually replying with any data. Try replying with a string (or in
your client have a blanket printf that catches the case where
strlen(rmsg)==0).

BTW are you using this code exactly as it appears in your app? Your main
thread should be doing something in between the last pthread_create() and
the return from main(), otherwise your client and server will go away
immediately… I just inserted a big sleep() but you might want to do
something a little smarter (like waiting for the process’ threads to
finish)…

“Belinda” <belinda_ye@hotmail.com> wrote in message
news:a4b5ms$86f$1@inn.qnx.com

I have included <errno.h> in my file, however, the client still didn’t
receive
reply message from the server, moreover, I send message from client to
server ten
times, however, the server just receive message one time.

Thanks,

“David LeBlanc” <> dleblanc@qnx.com> > wrote in message
news:a49csv$l8c$> 1@nntp.qnx.com> …
EOK should be defined as 0, although you should not define it yourself
since
it is already defined in <errno.h>. You should include that header file
rather than defining EOK yourself.

“Belinda” <> belinda_ye@hotmail.com> > wrote in message
news:a49c70$t5f$> 1@inn.qnx.com> …
Hello,

I met some problems when writing message passing between threads. I
just
want to
implement the function -client send message to several several times,
and
server
got the message and reply the client.Firstly how to define “EOK” in
MsgReply() function?
And if I define EOK is 1, the running result seems uncorrect since
server
just receive
the message just once whereas the client send message ten times.

Thanks,

Belinda


include <sys/neutrino.h
#include <stdio.h
#include <pthread.h
#include <stdlib.h
#include <string.h


int chid;

void* client() {
char *smsg=“This is the outgoing message”;
int i;
char rmsg[200];
int coid;

coid =ConnectAttach(0, 0, 1, 0, 0 );
if (coid==-1) {
printf( " Couldn’t ConnectAttach to 0/0/1!\n");
perror(NULL);
exit ( EXIT_FAILURE);
}
for (i=0;i<10;i++) {
if (MsgSend (coid, smsg, strlen(smsg) +1, rmsg,
sizeof(rmsg))==-1)
{
fprintf(stderr, “Error during MsgSend \n”);
perror(NULL);
exit(EXIT_FAILURE);

}
if(strlen(rmsg)>0)

printf(“Process returns “%s”\n”, rmsg);
}
}
}

void* server()
{
int rcvid;
int chid;
int pid;

char message[512];

chid=ChannelCreate(0);
while (1) {
rcvid=MsgReceive(chid, message, sizeof(message), NULL);
printf(“Got a message, rcvid is %X \n”, rcvid);
printf(“Message was received”%s".\n", message);
MsgReply(rcvid,EOK,NULL,0)
}
}

int main() {
pthread_t pid1,pid2;

pthread_create(&pid1, NULL, server, NULL);
pthread_create(&pid2, NULL, client, NULL);
return 0;

}



\

thanks, I got it.

“David LeBlanc” <dleblanc@qnx.com> wrote in message
news:a4bgft$8ld$1@nntp.qnx.com

Hmm, I compiled the example myself and it appeared to work as expected.
What might be throwing you off is the lack of verbosity, since your server
isn’t actually replying with any data. Try replying with a string (or in
your client have a blanket printf that catches the case where
strlen(rmsg)==0).

BTW are you using this code exactly as it appears in your app? Your main
thread should be doing something in between the last pthread_create() and
the return from main(), otherwise your client and server will go away
immediately… I just inserted a big sleep() but you might want to do
something a little smarter (like waiting for the process’ threads to
finish)…

“Belinda” <> belinda_ye@hotmail.com> > wrote in message
news:a4b5ms$86f$> 1@inn.qnx.com> …
I have included <errno.h> in my file, however, the client still didn’t
receive
reply message from the server, moreover, I send message from client to
server ten
times, however, the server just receive message one time.

Thanks,

“David LeBlanc” <> dleblanc@qnx.com> > wrote in message
news:a49csv$l8c$> 1@nntp.qnx.com> …
EOK should be defined as 0, although you should not define it yourself
since
it is already defined in <errno.h>. You should include that header
file
rather than defining EOK yourself.

“Belinda” <> belinda_ye@hotmail.com> > wrote in message
news:a49c70$t5f$> 1@inn.qnx.com> …
Hello,

I met some problems when writing message passing between threads. I
just
want to
implement the function -client send message to several several
times,
and
server
got the message and reply the client.Firstly how to define “EOK” in
MsgReply() function?
And if I define EOK is 1, the running result seems uncorrect since
server
just receive
the message just once whereas the client send message ten times.

Thanks,

Belinda


include <sys/neutrino.h
#include <stdio.h
#include <pthread.h
#include <stdlib.h
#include <string.h


int chid;

void* client() {
char *smsg=“This is the outgoing message”;
int i;
char rmsg[200];
int coid;

coid =ConnectAttach(0, 0, 1, 0, 0 );
if (coid==-1) {
printf( " Couldn’t ConnectAttach to 0/0/1!\n");
perror(NULL);
exit ( EXIT_FAILURE);
}
for (i=0;i<10;i++) {
if (MsgSend (coid, smsg, strlen(smsg) +1, rmsg,
sizeof(rmsg))==-1)
{
fprintf(stderr, “Error during MsgSend \n”);
perror(NULL);
exit(EXIT_FAILURE);

}
if(strlen(rmsg)>0)

printf(“Process returns “%s”\n”, rmsg);
}
}
}

void* server()
{
int rcvid;
int chid;
int pid;

char message[512];

chid=ChannelCreate(0);
while (1) {
rcvid=MsgReceive(chid, message, sizeof(message), NULL);
printf(“Got a message, rcvid is %X \n”, rcvid);
printf(“Message was received”%s".\n", message);
MsgReply(rcvid,EOK,NULL,0)
}
}

int main() {
pthread_t pid1,pid2;

pthread_create(&pid1, NULL, server, NULL);
pthread_create(&pid2, NULL, client, NULL);
return 0;

}





\