home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Network Support Encyclopedia 96-1
/
novell-nsepro-1996-1-cd2.iso
/
download
/
netware
/
tliex2.exe
/
DCLIENT.C
next >
Wrap
C/C++ Source or Header
|
1994-09-07
|
5KB
|
214 lines
/* ************************************************************************ */
/* DCLIENT.C
This example is the client side of a simple TLI example. In this example,
a simple message string is sent by the client, received by the server,
and sent back to the client. The client compares the information sent
back, and then terminates.
This example can be found in the NetWare Programmers Guide for C, under
the TLI Services section.
*/
/* ************************************************************************ */
#define INCL_BASE
#define INCL_DOS
#define INCL_VIO
#include <os2.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <malloc.h>
#include <string.h>
#define TLIINT short
#define TLIUINT unsigned short
#define NWOS2
#define IS32BIT
#define BCPP
#include <nwcaldef.h>
#include <tispxipx.h>
#include <tiuser.h>
#define SPX_SOCKET 4646 /* arbitrary socket */
#define SPX_BUF 100 /* Buffer Size */
int ParseAddress( char *address, IPX_ADDR *destination );
int PutAddress( char *string, char *buf, TLIINT hexBytes );
void SPXDisconReason( TLIINT fd );
int ParseAddress( char *addr, IPX_ADDR *destination )
{
if ( strlen( addr ) == 21 && addr[ 8 ] == '/' )
if ( PutAddress( addr, destination->ipxa_net, 4 ) )
if ( PutAddress( &addr[ 9 ], destination->ipxa_node, 6 ) )
return 1;
return 0;
} /* ParseAddress */
int PutAddress( char *string, char *buf, TLIINT hexBytes )
{
TLIINT i, j, value;
char c;
for ( i = 0; i < hexBytes; i++ )
{
value = 0; /* build a byte from two nibbles */
for ( j = 0; j < 2; j++ )
{
value <<= 4;
if ( ( c = toupper( *string ) ) >= '0' && c <= '9')
value += c - '0';
else if ( c >= 'A' && c <= 'F' )
value += c - 'A' + 10;
else
return 0;
string++;
}
*buf++ = value;
}
return 1;
} /* PutAddress */
struct t_call tcall;
int flags;
char buf1[ SPX_BUF ];
char buf2[ SPX_BUF ];
void main ( TLIINT argc, char *argv[] )
{
IPX_ADDR spx_addr; /* server address from command line */
SPX_OPTS spx_options;
TLIINT fd, i1;
/* Step 1 */
if ( argc != 2 || !ParseAddress( argv[ 1 ], &spx_addr ) )
{
printf( "Usage:\tdclient ServerAddress\n" );
printf( "\tServerAddress = Net/Node (in hex, leading zero's required)\n" );
printf( "\tExample:\"dclient 00001234/00001b02362e\"\n" );
exit( 1 );
}
/* Step 2 */
if (( fd = t_open( "/dev/nspx", O_RDWR, ( struct t_info * )0) ) == -1 )
{
t_error( "open of /dev/nspx failed" );
exit( 2 );
}
/* No need to bind to a specific socket */
if ( t_bind( fd, ( struct t_bind * )0, ( struct t_bind * )0 )== -1 )
{
t_error( "bind failed" );
exit( 2 );
}
/* Step 3 */
spx_addr.ipxa_socket[ 0 ] = 0;
spx_addr.ipxa_socket[ 1 ] = SPX_SOCKET;
tcall.addr.buf = ( char * )&spx_addr;
tcall.addr.len = sizeof( spx_addr );
tcall.addr.maxlen = sizeof( spx_addr );
spx_options.spx_connectionID[ 0 ] = 0;
spx_options.spx_connectionID[ 1 ] = 0;
spx_options.spx_allocationNumber[ 0 ] = 0;
spx_options.spx_allocationNumber[ 1 ] = 0;
tcall.opt.buf = ( char * )&spx_options;
tcall.opt.len = sizeof( spx_options );
tcall.opt.maxlen = sizeof( spx_options );
tcall.udata.buf = ( char * )0;
tcall.udata.len = 0;
tcall.udata.maxlen = 0;
/* Step 4 */
if ( t_connect( fd, &tcall, &tcall ) == -1 )
{
t_error( "t_connect failed" );
if ( t_errno == TLOOK && t_look( fd ) == T_DISCONNECT )
SPXDisconReason( fd );
exit( 2 );
}
printf( "t_connect successful, beginning send loop\n" );
/* Step 5 */
for ( i1 = 0; i1 < 10; i1++ )
{
sprintf( buf1, "message %d", i1 );
if ( t_snd( fd, buf1, strlen( buf1 ) + 1, 0 ) == -1 )
{
t_error( "t_snd failed" );
exit( 2 );
}
printf( "Sent message '%s'\n", buf1 );
flags = 0;
if ( t_rcv( fd, buf2, SPX_BUF, &flags ) == -1 )
{
t_error( "t_rcv failed" );
printf( "t_errno = %d, t_look() = %d\n",t_errno,t_look(fd));
if ( t_errno == TLOOK && t_look( fd ) == T_DISCONNECT )
SPXDisconReason( fd );
exit( 2 );
}
if ( strcmp( buf1, buf2 ) == 0 )
printf( "Received message '%s'\n", buf2 );
else
{
printf( "Received back invalid message %s\n", buf2 );
t_close( fd );
exit( 3 );
}
}
/* Step 6 */
if ( t_snddis( fd, ( struct t_call * )0 ) == -1 )
{
t_error( "t_snddis failed" );
exit( 2 );
}
t_close( fd );
} /* main */
void SPXDisconReason( TLIINT fd )
{
struct t_discon discon;
char buf[ 100 ];
char *msg;
discon.udata.buf = buf;
discon.udata.len = 0;
discon.udata.maxlen = sizeof(buf);
printf( "SPX Connection terminated\n" );
if ( t_rcvdis( fd, &discon ) == -1 )
{
t_error( "t_rcvdis failed" );
exit( 2 );
}
printf( "Reason = %d\n" , discon.reason );
switch( discon.reason )
{
case TLI_SPX_CONNECTION_FAILED:
msg = "Connection failed";
break;
case TLI_SPX_CONNECTION_TERMINATED:
msg = "Connection terminated by client";
break;
case TLI_SPX_MALFORMED_PACKET:
msg = "Internal SPX TLIINTerface error -- malformed packet";
break;
default:
msg = "Unknown termination reason";
}
printf( "SPX Connection terminated: %s\n", msg );
} /* SPXDisconReason */