Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Datagram-oriented transaction server

&pagelevel(3)&pagelevel

The following program for a transaction system in connectionless mode is described in detail in section "Connectionless service using an example transaction system". The server waits for incoming requests for data packets, then processes each request and sends a reply.

#include <stdio.h>
#include <fcntl.h>
#include <xti.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SRV_ADDR 0x7F000001
#define SRV_PORT 8888
main()
{
   int fd;
   int flags;
   struct t_bind *bind;
   struct t_unitdata *ud;
   struct t_uderr *uderr;
   struct sockaddr_in *sin;
   if ((fd = t_open("/dev/udp", O_RDWR, NULL)) < 0) {
      t_error("Not possible to open /dev/udp");
      exit(1);
   }
   if ((bind = (struct t_bind *)t_alloc(fd,
      T_BIND, T_ADDR)) == NULL) {
      t_error("t_alloc() of the t_bind structure failed");
      exit(2);
   }
   bind->addr.len=sizeof(struct sockaddr_in);
   sin=(struct sockaddr_in *)bind->addr.buf;
   sin->sin_family=AF_INET;
   sin->sin_port=htons(SRV_PORT);
   sin->sin_addr.s_addr=htonl(SRV_ADDR);
   bind->qlen = 0;
   if (t_bind(fd, bind, bind) < 0) {
      t_error("t_bind() failed");
      exit(3);
   }
   if ((ud = (struct t_unitdata *)t_alloc(fd,
      T_UNITDATA, T_ALL)) == NULL) {
      t_error("t_alloc() of t_unitdata structure failed");
      exit(5);
   }
   if ((uderr = (struct t_uderr *)t_alloc(fd,
      T_UDERROR, T_ALL)) == NULL) {
      t_error("t_alloc() of t_uderr structure failed");
      exit(6);
   }
   for(;;) {
      if (t_rcvudata(fd, ud, &flags) < 0) {
         if (t_errno == TLOOK) {
                  /*
                   * Error because of previous datagram
                   */
                  if (t_rcvuderr(fd, uderr) < 0) {
                     t_error("t_rcvuderr() failed");
                     exit(7);
                  }
                  fprintf(stderr, 
                  "Datagram error, error = %d\n", 
                  uderr->error);
                  continue;
         }
         t_error("t_rcvudata() failed");
         exit(8);
      }
      /*
       * query() processes the request and writes the reply in
       * ud->udata.buf and the length in ud->udata.len
       */
      query(ud);
      if (t_sndudata(fd, ud, 0) < 0) {
         t_error("t_sndudata() failed");
         exit(9);
      }
   }
}
query()
{
/* Only an extract, for simplification reasons */
}