Open System Services Programmer's Guide

printf("?"); /* send prompt */
fgets(msg_buff, BUFF_LEN, stdin); /* read user input */
if (feof(stdin)) /* if EOF then exit(0) */
exit(0);
bytesread = (int)strlen(msg_buff); /* includes newline */
/* send line to server... if error then exit(1) */
if (send(fd, msg_buff, bytesread, 0) < 0)
{
printf("send() call failed [errno %d]\n", errno);
exit(1);
}
/* expect echo from server... if close or error then
exit(1) */
if ((bytesread = recv(fd, msg_buff, bytesread, 0)) <= 0)
{
if (bytesread == 0)
printf("Server closed connection\n");
else
printf("recv() call failed [errno %d]\n", errno);
exit(1);
}
printf("Read %d bytes back from server\n",bytesread);
}
}
Communication Using Mixed APIs
Example 45 (page 185) and Example 44 (page 182) should give you an idea of how the AF_INET
communication mechanism works when different sets of APIs are used and provide you with a
template you can expand in your applications.
Example 45 and Example 44 show a Guardian server process and an OSS requester process
using Guardian AF_INET and OSS AF_INET functions, respectively, to communicate.
NOTE: These examples use the AF_INET sockets functions, which provide an interface to NonStop
TCP/IP. The programs could also be written using the AF_INET6 sockets functions to interface to
NonStop TCP/IPv6. For information and examples of AF_INET6 sockets usage, refer to the TCP/IP
Programming Manual.
The server program in Example 45 is compiled as a linked program file named server2g that
is secured for execution in the current working directory. The requester program in Example 44 is
compiled as a linked program file named requester2 that is secured for execution in the current
working directory.
The server process can be started in the background from the OSS shell using the command:
run ./server2g &
After you start the server process, the requester process in Example 44 can be started in the
foreground from the OSS shell using the command:
./requester2
Alternatively, both processes can be started in the foreground from separate terminal sessions. The
following sample dialog illustrates the processes run from two terminal sessions. The server terminal
session for the program in Example 45 appears as:
/home/software/chrisbl/cabpubs: ./server2g
Server starting
Message received = hello
Message received = goodbye
Server stopping
/home/software/chrisbl/cabpubs:
while the requester terminal session for the program in Example 47 appears as:
Interprocess-Communication Interoperability 183