//server.c
#include <errno.h>
#include <time.h>
#include <string.h>
#include <spawn.h>
#include <sys/neutrino.h>
#include <sys/netmgr.h>
#include <sys/dispatch.h>
struct Message
{
// This structure contains timer events
struct _pulse timerPulse;
// This is user event message data
char data[100];
}msg,replyMsg,emptyReply;
int main()
{
name_attach_t *nameAttach; // QNX global name struct
int channelId;
int connectId;
int processId = 0; // Ourselves
int recvId;
int ret;
pid_t pid;
char *file="/net/node2/home/client";
char *arg_list[] = {file, NULL};
struct inheritance inherit;
inherit.flags = SPAWN_SETND;
inherit.nd = netmgr_strtond(“node2”,NULL);
if ((nameAttach = name_attach(NULL, “myname”, NAME_FLAG_ATTACH_GLOBAL)) == NULL)
{
printf( “name_attach Error!”);
exit (-1);
}
channelId = nameAttach->chid;
// Connect to the channel to be able to receive messages
if ((connectId = ConnectAttach_r(ND_LOCAL_NODE, processId, channelId, _NTO_SIDE_CHANNEL, 0)) < 0)
{
printf(“Connection failed on channel %d due to %s”, channelId, strerror(abs(connectId)));
exit(-1);
}
if(pid = spawn(file,0,NULL,&inherit,arg_list,NULL) == -1)
{
printf(“spawn error!”);
exit(-1);
}
// Receive a message or timer pulse
while(1)
{
if ((recvId = MsgReceive_r(channelId, &msg, sizeof(msg), NULL)) < 0)
{
printf("Error MsgReceive! ");
exit(-1);
}
else
{
// Determine if we received a message or timer pulse
printf(“MsgReceive…\n”);
if (recvId == 0)
{
printf(“recvId: %d\n”,recvId);
switch (msg.timerPulse.code)
{
case _PULSE_CODE_DISCONNECT:
// Remote side disconnecting via name_close() from a
// name_open (this only occurs for a server process
ConnectDetach_r(msg.timerPulse.scoid);
break;
case _PULSE_CODE_UNBLOCK:
// Sender hit by a signal so release them to process it
if ((ret = MsgReply_r(recvId, 0, &emptyReply, sizeof(emptyReply))) < 0)
{
printf(“Error %s while trying to reply to sender over connection %d on channel %d”, strerror(abs(ret)), connectId, channelId);
}
break;
case _PULSE_CODE_THREADDEATH:
case _PULSE_CODE_COIDDEATH:
// Remote side died/exited!
break;
default:
// Timer pulse received. Do timer processing…
break;
}
}
else
{
// Reply to a name_open() call for servers
printf(“received something:\n”);
if (msg.timerPulse.type == _IO_CONNECT)
{
MsgReply_r(recvId, EOK, NULL, 0);
printf(“Remote side %d connected to server\n”, recvId);
}
else
{
// Process the message…
printf (" server received a message of %s from client\n", msg.data);
// Reply to unblock the other side.
sprintf(replyMsg.data,“server reply msg to client!”);
printf("\n");
MsgReply_r(recvId, 0, &replyMsg, sizeof(replyMsg));
}
}
}
}
}