Open System Services Programmer's Guide
Example 45 Using AF_INET in a Guardian Server Process
/* server2g.c
*
* Simple OSS server example using Guardian socket APIs
* for communication between requester and server.
*
* This server accepts AF_INET stream socket connections on
* a listening port. Data requests are handled by printing
* the data and then replying back to the requester with the
* original data. The server exits when idle for 60 seconds.
*
* Note - Guardian sockets do not have Posix semantics.
* select() is not available and nonblocking calls require
* starting operations with a nowait socket call such as
* recv_nw() and then completing the operation with a call
* to AWAITIOX(). Each operation requires a dedicated buffer.
*/
#define _GUARDIAN_SOCKETS /* needed for Guardian sockets */
#include <unistd.h>
#include <fcntl.h>
#include <tal.h>
#include <cextdecs.h(AWAITIOX,FILE_CLOSE_)>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netdb.h>
short fd, listenfd, newfd; /* note - fd is really Guardian fnum */
short bytesread;
long tag;
int addrlen;
struct sockaddr_in addr; /* socket address (family,addr,port) */
enum tag { bind_tag = 1, accept_tag, recv_tag, send_tag };
#define NOWAIT 1
#define SYNC 1
#define FOREVER -1L
#define IDLE_PERIOD 6000 /* 60 seconds = 6000 x 0.01 second */
#define MAX_FDS 100
#define BUFF_LEN 1024
char *msg_buff_ptr[MAX_FDS+1]; /* need one buffer per socket */
char *bufferptr; /* pointer to current buffer */
int main(int argc, char **argv)
{
printf("Server starting\n");
/* create AF_INET stream socket ... if error then exit(1) */
if ((fd = (short)socket_nw(AF_INET, SOCK_STREAM, 0,
NOWAIT, SYNC )) < 0)
{
printf("socket_nw() call failed [errno %d]\n", errno);
exit(1);
}
/* Get server port from the command line or use default 12345 */
if (argc > 1)
addr.sin_port = (short)atoi(argv[1]); /* use command line arg */
else
addr.sin_port = 12345; /* use default */
Interprocess-Communication Interoperability 185