Skip navigation.
Home
The QNX Community Portal

View topic - Client-server C program

Client-server C program

This is where you can ask a software to be ported or written for QNX. You can also request to buy/sell second hand software.

Postby munder » Wed May 23, 2007 12:32 pm

When I started gns -c I provided server name, and next time I didn't. Both ways it didn't work.
What do you ment about supporting QNX Messages?
munder
Active Member
 
Posts: 22
Joined: Sun Sep 24, 2006 11:57 am

Postby cburgess » Wed May 23, 2007 2:27 pm

You need to start gns as a server on one node, and gns as a client on all other nodes, as I read it from the docs, ie

(on node1)
gns -s

(on node2)
gns -c node1
cburgess
QNX Master
 
Posts: 209
Joined: Tue Aug 31, 2004 8:40 pm
Location: Ottawa

Postby munder » Wed May 23, 2007 3:43 pm

And I did it. On client I did
gns -c ser
(where ser was my ATTACH_POINT in name_attach(NULL, ATTACH_POINT, 0) on server)
It didn't work :(
munder
Active Member
 
Posts: 22
Joined: Sun Sep 24, 2006 11:57 am

Postby mario » Wed May 23, 2007 4:08 pm

can you post all of your code. I know for a fact this works.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Postby munder » Wed May 23, 2007 4:21 pm

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/dispatch.h>

#define ATTACH_POINT "ser"

/* We specify the header as being at least a pulse */
typedef struct _pulse msg_header_t;

/* Our real data comes after the header */
typedef struct _my_data {
msg_header_t hdr;
int data;
} my_data_t;

/*** Server Side of the code ***/
int server() {
name_attach_t *attach;
my_data_t msg;
int rcvid;

/* Create a global name (/dev/name/global/...) */
if ((attach = name_attach(NULL, ATTACH_POINT, NAME_FLAG_ATTACH_GLOBAL)) == NULL) {
return EXIT_FAILURE;
}

/* Do your MsgReceive's here now with the chid */
while (1) {
rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL);

if (rcvid == -1) {/* Error condition, exit */
break;
}

if (rcvid == 0) {/* Pulse received */
switch (msg.hdr.code) {
case _PULSE_CODE_DISCONNECT:
/*
* A client disconnected all its connections (called
* name_close() for each name_open() of our name) or
* terminated
*/
ConnectDetach(msg.hdr.scoid);
break;
case _PULSE_CODE_UNBLOCK:
/*
* REPLY blocked client wants to unblock (was hit by
* a signal or timed out). It's up to you if you
* reply now or later.
*/
break;
default:
/*
* A pulse sent by one of your processes or a
* _PULSE_CODE_COIDDEATH or _PULSE_CODE_THREADDEATH
* from the kernel?
*/
}
continue;
}

/* name_open() sends a connect message, must EOK this */
if (msg.hdr.type == _IO_CONNECT ) {
MsgReply( rcvid, EOK, NULL, 0 );
continue;
}

/* Some other QNX IO message was received; reject it */
if (msg.hdr.type > _IO_BASE && msg.hdr.type <= _IO_MAX ) {
MsgError( rcvid, ENOSYS );
continue;
}

/* A message (presumable ours) received, handle */
printf("Server receive %d \n", msg.data);
MsgReply(rcvid, EOK, 0, 0);

}

/* Remove the name from the space */
name_detach(attach, 0);

return EXIT_SUCCESS;
}


/*** Client Side of the code ***/
int client() {
my_data_t msg;
int fd;

if ((fd = name_open(ATTACH_POINT, NAME_FLAG_ATTACH_GLOBAL)) == -1) {
return EXIT_FAILURE;
}

/* We would have pre-defined data to stuff here */
msg.hdr.type = 0x00;
msg.hdr.subtype = 0x00;

/* Do whatever work you wanted with server connection */
for (msg.data=0; msg.data < 5; msg.data++) {
printf("Client sending %d \n", msg.data);
if (MsgSend(fd, &msg, sizeof(msg), NULL, 0) == -1) {
break;
}
}

/* Close the connection */
name_close(fd);
return EXIT_SUCCESS;
}

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

if (argc < 2) {
printf("Usage %s -s | -c \n", argv[0]);
ret = EXIT_FAILURE;
}
else if (strcmp(argv[1], "-c") == 0) {
printf("Running Client ... \n");
ret = client(); /* see name_open() for this code */
}
else if (strcmp(argv[1], "-s") == 0) {
printf("Running Server ... \n");
ret = server(); /* see name_attach() for this code */
}
else {
printf("Usage %s -s | -c \n", argv[0]);
ret = EXIT_FAILURE;
}
return ret;
}

It's an example from library reference. I've run it on server as server and on client (other computer) as a client...
munder
Active Member
 
Posts: 22
Joined: Sun Sep 24, 2006 11:57 am

Postby mario » Wed May 23, 2007 9:30 pm

Just tried it and it works fine for me.

I added some printfs when the program was terminating because of error. I started gns -s on machine 1 then gns -c <machine1> on machine2 , then started the program with -s ( as root ) on machine 1. Ran the program with -c n machine 2 and got 2 prints on both server and client

My guess is you didn't start the server as root and it was terminating right away.

Or you don't even have qnet running ( it's not by default ).
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Postby munder » Thu May 24, 2007 6:18 am

I did run it as root.
If I start gns than Qnet may not be working?
If so than how to check if it's running and how to run it?
I'll try again from start. Maye you can write me what I should pay special attention to?

Btw I used some printfs too :) Program working as client always exits after name_open().
munder
Active Member
 
Posts: 22
Joined: Sun Sep 24, 2006 11:57 am

Postby qnxloader » Thu May 24, 2007 8:00 am

For run QNET at startup, create empty file: /etc/system/config/useqnet
qnxloader
Senior Member
 
Posts: 206
Joined: Mon Oct 06, 2003 8:29 am

Postby mario » Thu May 24, 2007 11:56 am

munder wrote:I did run it as root.
If I start gns than Qnet may not be working?
If so than how to check if it's running and how to run it?
I'll try again from start. Maye you can write me what I should pay special attention to?

Btw I used some printfs too :)


Then it would have been nice if you posted the code that you tried ;-)

munder wrote: Program working as client always exits after name_open().


And what is the value of errno?

If you have a directory call /net then qnet is running.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Postby munder » Thu May 24, 2007 12:51 pm

mario wrote:
Then it would have been nice if you posted the code that you tried ;-)


It was the same only in each exit I had my special printf("dupa1...n")
Thats how I know it was in name_open()
I'll back to QNX on Saturday and then I'll write how I'm going.

I had /net directory so QNet was working.
munder
Active Member
 
Posts: 22
Joined: Sun Sep 24, 2006 11:57 am

Postby ingraham » Thu May 24, 2007 3:48 pm

munder wrote:Only how to read the file that is on the server if I can't connect to server?


Simply add "/net/nodename" before the path to the file on the server.

munder wrote:What is "nodename"? Is it the name attached on server by name_attach()?


No, actually. Your server has a name, which you can find by going to your network configuration on the network tab, where it says "Host Name:" If you don't supply anything, QNX will generate a semi-random name like "EA33a7ae", or else you can type in something meaningful. By the way, this may be the problem you are having with gns. The -s isn't the name of the server APPLICATION, it's the node (host) name of the MACHINE the server is running on.

munder wrote:Are you sure it's so difficult?


Well, it WAS this difficult. Three years ago or so. It turns out no one from QNX ever bothered to point gns out to me. It's not listed in the "New Content" section of the 6.3 documentation (any version), even though it was NOT in 6.2.1. So I'm really glad you asked this question because I sure just learned something.

-James Ingraham
Sage Automation, Inc.
ingraham
Senior Member
 
Posts: 160
Joined: Wed Nov 05, 2003 2:25 pm
Location: Texas

Postby cburgess » Thu May 24, 2007 8:42 pm

munder wrote:And I did it. On client I did
gns -c ser
(where ser was my ATTACH_POINT in name_attach(NULL, ATTACH_POINT, 0) on server)
It didn't work :(


You're supposed to run

gdb -c <NODENAME>, it has nothing to do with your ATTACH_POINT.

That's probably your problem.
cburgess
QNX Master
 
Posts: 209
Joined: Tue Aug 31, 2004 8:40 pm
Location: Ottawa

Postby mario » Fri May 25, 2007 1:21 am

cburgess wrote:gdb -c <NODENAME>, it has nothing to do with your ATTACH_POINT.


You mean gns -c <NODENAME>. lol!
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Postby cburgess » Fri May 25, 2007 3:43 pm

mario wrote:
cburgess wrote:gdb -c <NODENAME>, it has nothing to do with your ATTACH_POINT.


You mean gns -c <NODENAME>. lol!


LOL - my fingers are used to typing gdb and they did it on their own, with no input from my brain! :-)
cburgess
QNX Master
 
Posts: 209
Joined: Tue Aug 31, 2004 8:40 pm
Location: Ottawa

Postby munder » Sat May 26, 2007 11:30 am

First of all thank you all for your help.
Now it's working... I was making few mistakes :(
- QNet on client's computer wasn't running
- on client I was using gns not I supossed to

Btw can I do something to run gns automatic after system starts?
munder
Active Member
 
Posts: 22
Joined: Sun Sep 24, 2006 11:57 am

PreviousNext

Return to Request Software

Who is online

Users browsing this forum: No registered users and 1 guest