Written by: Udhaya Kumar.V 
Document  : TLI sample programs for UNIX environment
Date      : December – 2000
Contact   : URL: http://www.udhaya.com , Mail Id: [email protected]

TLI for TCP - server sample programme for UNIX environment

#include <stdio.h>

#include <fcntl.h>

#include <tiuser.h>

#include <stropts.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

 

main ( int argc, char *argv[] )

{

      int                                 tfd, newtfd, clilen, childpid;

      const int flags = 1 ;

      struct sockaddr_in            cli_addr, serv_addr;

      struct t_bind                 req;

      struct t_call                 *callptr;

      char                          *pname;

 

      pname = argv [0];

 

      if ( ( tfd = t_open ( "/dev/tcp", O_RDWR, ( struct t_info * ) 0 ) ) < 0 )

      {

            printf ( "\nError in the open " );

            exit ( 0 );

      }

      else

            printf ( "\nServer Open...." );

 

      bzero ( (char * ) &serv_addr, sizeof ( serv_addr ) );

      serv_addr.sin_family          = AF_INET;

      serv_addr.sin_addr.s_addr     = inet_addr ( "140.0.0.37" );

      serv_addr.sin_port                  = htons ( 8000 );

 

      req.addr.maxlen         = sizeof( serv_addr );

      req.addr.len            = sizeof( serv_addr );

      req.addr.buf            = (char *) &serv_addr;

      req.qlen                = 5;

      setsockopt (  tfd , SOL_SOCKET , SO_REUSEPORT , &flags , sizeof ( int ) ) ;

      setsockopt (  tfd , SOL_SOCKET , SO_REUSEADDR , &flags , sizeof ( int ) ) ;

 

      if ( t_bind( tfd, &req, ( struct t_bind * ) 0 ) < 0 )

      {

            printf ( "\nCan't bind ...." );

            exit ( 0 );

      }

      else

            printf ( "\nServer Bind ..." );

 

      if ( ( callptr = ( struct t_call * ) t_alloc( tfd, T_CALL, T_ADDR )) == NULL)

      {

            printf( "\nt_alloc error" );

            exit( 0 );

      }

      else

            printf ( "\nMemory alloc ..." );

 

      for ( ; ; )

      {

            if ( t_listen( tfd, callptr ) < 0 )

                  printf ( "\nServer t_listen error" );

            else

                  printf ( "\nServer Listen ..." );

 

            if ( ( newtfd = accept_call( tfd, callptr, "/dev/tcp", 1 ) ) < 0 )

                  printf ( "\nServer accept_call error" );

            else

                  printf ( "\nClient Connected ..." );

 

            if ( ( childpid = fork () ) < 0 )

                  printf ( "Server: Fork error" );

            else if ( childpid == 0 )

            {

                  printf ( "\nOne Child is created" );

                  t_close( tfd );

                  str_echo( newtfd );

                  exit( 0 );

            }

            close( newtfd );

      }

}

 

str_echo( int sockfd )

{

      int         n;

      char  line[ 512 ];

 

      for ( ;; )

      {

            n = read ( sockfd, line, 512 );

            if ( n == 0 )

                  return;                 // Connection terminate

            else if ( n < 0 )

                  printf ( "\nRead data error" );

           

            if ( write( sockfd, line, n ) != n )

                  printf( "\nWrite Error " );

      }

}

 

int accept_call( int listenfd, struct t_call *callptr, char *name, int rwflag )

{

      int         newfd;

      extern int t_errno;

 

      if ( ( newfd = t_open( name, O_RDWR, ( struct t_info * ) 0 ) ) < 0 )

            printf ( "\nt_open error" );

 

      if ( t_bind( newfd, ( struct t_bind * ) 0, ( struct t_bind * ) 0 ) < 0 )

            printf ( "\nt_bind error" );

 

      if ( t_accept( listenfd, newfd, callptr ) < 0 )

      {

            if ( t_errno == TLOOK )

            {

                  if ( t_rcvdis( listenfd, ( struct t_discon * ) 0 ) < 0 )

                        printf( "\nt_rcvdis error" );

                  if ( t_close( newfd ) < 0 )

                        printf( "\nt_close" );

                  return( -1 );

            }

            printf( "\nt_accept error" );

      }

      if ( rwflag )

      {

            if ( ioctl( newfd, I_POP, ( char * ) 0 ) < 0 )

                  printf( "\nI_POP of timeod failed" );

            if ( ioctl( newfd, I_PUSH, "tirdwr" ) < 0 )

                  printf( "\nI_PUSH of tirdwr failed" );

      }

      return( newfd );

}

 

TLI for TCP - Client sample programme for UNIX environment

#include <stdio.h>

#include <fcntl.h>

#include <tiuser.h>

#include <stropts.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

 

main( int argc, char *argv[] )

{

      int                           tfd;

      char                    *t_alloc();

      struct t_call           *callptr;

      struct sockaddr_in      serv_addr;

      char                    *pname;

 

      pname = argv[ 0 ];

      if ( ( tfd = t_open( "/dev/tcp", O_RDWR, 0 ) ) < 0 )

            printf( "\nClient Can't t_open error" );

      else

            printf ( "\nClient open ..." );

 

      if ( t_bind( tfd, ( struct t_bind * ) 0, (struct t_bind * ) 0 ) < 0 )

            printf ( "\nclient error: t_bind" );

      else

            printf ( "\nClient bind..." );

 

      bzero ( ( char * ) &serv_addr, sizeof( serv_addr ) );

      serv_addr.sin_family          = AF_INET;

      serv_addr.sin_addr.s_addr     = inet_addr ( "140.0.0.37" );

      serv_addr.sin_port                  = htons( 8000 );

 

      if ( ( callptr = ( struct t_call * ) t_alloc ( tfd, T_CALL, T_ADDR ) ) ==     NULL )

            printf ( "\nClientError: t_alloc " );

      else

            printf ( "\nClient allocate..." );

 

      callptr->addr.maxlen    = sizeof( serv_addr );

      callptr->addr.len             = sizeof( serv_addr );

      callptr->addr.buf       = ( char * ) &serv_addr;

      callptr->opt.len        = 0;

      callptr->udata.len            = 0;

 

      if ( t_connect ( tfd, callptr, ( struct t_call *) 0 ) < 0 )

            printf ( "\nClient Error t_connect " );

      else

            printf ( "\nClient connected..." );

 

      doit ( stdin, tfd );

      close ( tfd );

      exit ( 0 );

}

 

doit ( register FILE *fp, register int tfd )

{

      int         n, flags;

      char  sendline[ 512 ], recvline[ 512 ];

 

      while ( fgets ( sendline, 512, fp ) != NULL )

      {

            n = strlen ( sendline );

            if ( t_snd ( tfd, sendline, n, 0 ) != n )

            {

                  printf ( "\nClient Error: t_snd" );

                  exit ( 0 );

            }

            n = t_rcv ( tfd, recvline, 512, &flags );

            if ( n < 0 )

            {

                  printf ( "\nClient Error: t_rcv" );

                  exit ( 0 );

            }

            recvline[ n ] = '\0';

            fputs ( recvline, stdout );

      }

 

      if ( ferror ( fp ) )

            printf ( "\nClient: Error reading files" );

}

 

TLI for UDP - server sample programme for UNIX environment

#include <stdio.h>

#include <fcntl.h>

#include <tiuser.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

 

main ( int argc, char *argv[] )

{

      int                                 tfd;

      int                                 n, flag;

      struct sockaddr_in            serv_addr;

      struct t_bind                 req;

      struct t_unitdata       *udataptr;

      char                          *pname;

      char                          line [ 512 ];

      char                          *t_alloc();

 

      pname = argv[ 0 ];

      if ( ( tfd = t_open( "/dev/udp", O_RDWR, (struct t_info * ) 0 )) < 0 )

      {

            printf ( "Server Error: t_open..." );

            exit ( 0 );

      }

      else

            printf ( "\nServer Open ..." );

 

      bzero ( ( char * ) &serv_addr, sizeof ( serv_addr ) );

      serv_addr.sin_family          = AF_INET;

      serv_addr.sin_addr.s_addr     = htonl ( INADDR_ANY );

      serv_addr.sin_port                  = htons ( 7000 );

     

      req.addr.maxlen                     = sizeof ( serv_addr );

      req.addr.len                        = sizeof ( serv_addr );

      req.addr.buf                        = ( char * ) &serv_addr;

      req.qlen                            = 5;

 

      if ( t_bind ( tfd, &req, ( struct t_bind * ) 0 ) < 0 )

      {

            printf ( "\nBind Error : t_bind" );

            exit ( 0 );

      }

      else

            printf ( "\nServer Binded ..." );

 

      udataptr = ( struct t_unitdata * ) t_alloc ( tfd, T_UNITDATA, T_ADDR );

      if ( udataptr == NULL )

      {

            printf ( "\nt_alloc error ..." );

            exit ( 0 );

      }

      else

      {

            printf ( "t_alloc allocated ..." );

            printf ( "\nStarted ..." );

      }

 

      for ( ;; )

      {

            udataptr->opt.maxlen    = 0;

            udataptr->opt.len       = 0;

            udataptr->udata.maxlen  = 512;

            udataptr->udata.len           = 512;

            udataptr->udata.buf           = line;

 

            if ( t_rcvudata ( tfd, udataptr, &flag ) < 0 )

            {

                  printf ( "\nRead Error..." );

                  exit ( 0 );

            }

            else

                  printf ( "\nRecieve data..." );

 

            if ( t_sndudata ( tfd, udataptr ) < 0 )

            {

                  printf ( "\nWrite Error..." );

                  exit ( 0 );

            }

            else

                  printf ( "\nSend Data ..." );

      }

}

 

TLI for UDP  - Client sample programme for UNIX environment

#include <stdio.h>

#include <fcntl.h>

#include <tiuser.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

 

main ( int argc, char *argv[] )

{

      int                                 tfd;

      struct t_unitdata       unitdata;

      struct sockaddr_in            serv_addr;

      char                          *pname;

 

      pname = argv[ 1 ];

 

      if ( ( tfd = t_open ( "/dev/udp", O_RDWR, (struct t_info * ) 0 ) ) < 0 )

      {

            printf ( "\nClient Error: t_open" );

            exit ( 0 );

      }

 

      if ( t_bind ( tfd, ( struct t_bind * ) 0, ( struct t_bind * ) 0 ) < 0 )

      {

            printf ( "\nClient Error: t_bind" );

            exit ( 0 );

      }

     

      bzero ( ( char * ) &serv_addr, sizeof ( serv_addr ) );

      serv_addr.sin_family          = AF_INET;

      serv_addr.sin_addr.s_addr     = inet_addr ( "140.0.0.37" );

      serv_addr.sin_port                  = htons ( 7000 );

 

      unitdata.addr.maxlen    = sizeof ( serv_addr );

      unitdata.addr.len             = sizeof ( serv_addr );

      unitdata.addr.buf       = ( char * ) &serv_addr;

      unitdata.opt.maxlen           = 0;

      unitdata.opt.len        = 0;

      unitdata.opt.buf        = ( char * ) 0;

 

      doit ( tfd, &unitdata, stdin );

      t_close ( tfd );

      exit ( 0 );

}

 

doit ( register int tfd, struct t_unitdata *sudataptr, register FILE *fp )

{

      int                           n,flags;

      char                    sendline[512], recvline[ 513 ];

      char                    *t_alloc();

      struct t_unitdata *rudataptr;       //for Recieve

 

      rudataptr = ( struct t_unitdata * ) t_alloc ( tfd, T_UNITDATA, T_ADDR );

      if ( rudataptr == NULL )

      {

            printf ( "\nClient Error: t_alloc error for T_UNITDATA" );

            exit ( 0 );

      }

 

      while ( fgets ( sendline, 512, fp ) != NULL )

      {

            n = strlen ( sendline );

            sudataptr->udata.len    = n;

            sudataptr->udata.buf    = sendline;

            if ( t_sndudata ( tfd, sudataptr ) < 0 )

            {

                  printf ( "\nClient Error: t_sndudata " );

                  exit ( 0 );

            }

 

            rudataptr->opt.maxlen         = 0;

            rudataptr->udata.maxlen       = 512;

            rudataptr->udata.buf          = recvline;

            if ( t_rcvudata ( tfd, rudataptr, &flags ) < 0 )

            {

                  printf ( "\nClient Error: t_rcvudata");

                  exit ( 0 );

            }

            recvline [ rudataptr->udata.len ] = '\0';

            fputs ( recvline, stdout );

      }

      if ( ferror ( fp ) )

      {

            printf ( "\nError Reading file" );

            exit ( 0 );

      }

}

 

TLI - UnNamed SP

#include <sys/types.h>

#include <sys/stream.h>

#include <stropts.h>

#include <fcntl.h>

 

int s_pipe ( int fd[ 2 ] )

{

      struct strfdinsert            ins;

      queue_t                             *pointer;

 

      if ( ( fd[ 0 ] = open ( "/dev/spx", O_RDWR ) ) < 0 )

            return ( -1 );

      if ( ( fd[ 1 ] = open ( "/dev/spx", O_RDWR ) ) < 0 )

      {

            close ( fd[ 0 ] );

            return ( -1 );

      }

 

      ins.ctlbuf.buf          = ( char * ) &pointer;

      ins.ctlbuf.maxlen = sizeof ( queue_t * );

      ins.ctlbuf.len          = sizeof ( queue_t * );

 

      ins.databuf.buf         = ( char * ) 0;

      ins.databuf.len         = -1;

      ins.databuf.maxlen      = 0;

 

      ins.fildes              = fd[ 1 ];

      ins.flags               = 0;

      ins.offset              = 0;

 

      if ( ioctl ( fd[ 0 ], I_FDINSERT, ( char * ) &ins ) < 0 )

      {

            close ( fd[ 0 ] );

            close ( fd[ 1 ] );

            return ( -1 );

      }

}

 

int ns_pipe ( char *name, int fd[ 2 ] )

{

      int                     omask;

      struct stat       statbuff;

 

      if ( s_pipe ( fd ) < 0 )

            return ( -1 );

      if ( fstat ( fd [ 0 ], &statbuff ) < 0 )

      {

            close ( fd [ 0 ] );

            close ( fd [ 1 ] );

            return ( -1 );

      }

 

      unlink ( name );

      omask = umask ( 0 );

      if ( mknod ( name, S_IFCHR | 0666, statbuf.st_rdev ) < 0 )

      {

            close ( fd [ 0 ] );

            close ( fd [ 1 ] );

            umask ( omask );

            return ( -1 );

      }

      umask ( omask );

      return ( 0 );

}

 

#define NSPIPENAME "/tmp/nspipe.serv"

#define BUFFSIZE 1024

 

main ()

{

      int                     fd[ 2 ], flags;

      char              cntlbuff[ 1024 ], databuff[ 1024 ];

      struct strbuf     cntlstr, datastr;

 

      if ( ns_pipe ( NSPIPENAME, fd ) < 0 )

      {

            printf ( "\nCan't create named stream pipe" );

      }

     

      cntlstr.buf       = cntlbuff;

      cntlstr.maxlen    = 1024;

      cntlstr.len       = 0;

 

      datastr.buf       = databuff;

      datastr.maxlen    = 1024;

      datastr.len       = 0;

      flags             = 0;

 

      for ( ;; )

      {

            if ( getmsg ( fd [ 1 ], &cntlstr, &datastr, &flags ) < 0 )

            {

                  printf ( "\nError getmsg ");

                  exit ( 0 );

            }

            if ( cntlstr.len > 0 )

                  printf ( "recieve: %d ", cntlstr.len );

            if ( datastr.len > 0 )

                  printf ( "Data Len: %d Data: %s", datastr.len, datastr.buf );

      }

}

 

 

Hosted by www.Geocities.ws

1