home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IBM Presents OS/2 Software Hits 1995
/
OS-2_SW_HITS_2ND_EDITION_1995.ISO
/
i17
/
tliex2.exe
/
TLICLIPX.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-09
|
4KB
|
146 lines
/* ************************************************************************ */
/* TLICLIPX.C
This example is the client side of a simple TLI example. In this example,
a simple message string is sent by the server and received by the client.
This example is connectionless, or IPX. Once the client receives the packet,
it issues another receive. This example will work in blocking or
non-blocking mode (the t_open will need to have O_NDELAY or'ed with
O_RDWR for non-blocking mode.
*/
/* ************************************************************************ */
#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>
#include <conio.h>
#define NWOS2
#define IS32BIT
#define BCPP
#include <nwcaldef.h>
#include <tispxipx.h>
extern "C"{
#include <tiuser.h>
}
#define IPX_SOCKET 0x4645 /* arbitrary socket */
#define DATABUFSIZE 100 /*arbitrary buffer size */
void main ( void )
{
IPX_ADDR ipxReqAddr;
IPX_ADDR ipxRetAddr;
IPX_ADDR ipxAddr;
IPX_OPTS ipxOptions;
struct t_bind tbindReq;
struct t_bind tbindRet;
int fd;
char dataBuf[DATABUFSIZE];
char dataBuf2[DATABUFSIZE*20];
struct t_unitdata unitdata;
int flags=0;
int dataPos;
/* Open in blocking mode, replace O_RDWR */
/* with O_RDWR|O_NDELAY for non-blocking mode */
if (( fd = t_open( "/dev/nipx", O_RDWR, ( struct t_info * )0) ) == -1 )
{
t_error( "open of /dev/nipx failed" );
exit( 2 );
}
/* set up the socket that we will be listening on */
ipxReqAddr.ipxa_socket[ 0 ] = (IPX_SOCKET & 0xFF00)>>8;
ipxReqAddr.ipxa_socket[ 1 ] = IPX_SOCKET & 0x00FF;
tbindReq.addr.buf = ( char * )&ipxReqAddr;
tbindReq.addr.len = sizeof( IPX_ADDR );
tbindReq.addr.maxlen = sizeof( IPX_ADDR ); /*Can be any value for Req*/
tbindRet.addr.buf = ( char * )&ipxRetAddr;
tbindRet.addr.len = sizeof( IPX_ADDR );
tbindRet.addr.maxlen = sizeof(IPX_ADDR);
if ( t_bind( fd, &tbindReq, &tbindRet )== -1 )
{
t_error( "bind failed" );
exit( 2 );
}
/* set up the unitdata structure for the receive */
ipxAddr.ipxa_socket[ 0 ] = (IPX_SOCKET & 0xFF00)>>8;
ipxAddr.ipxa_socket[ 1 ] = IPX_SOCKET & 0x00FF;
unitdata.addr.buf = ( char * )&ipxAddr;
unitdata.addr.len = sizeof( ipxAddr );
unitdata.addr.maxlen = sizeof( ipxAddr );
unitdata.opt.buf=(char *)&ipxOptions; /* Options structure is defined*/
unitdata.opt.len = sizeof(IPX_OPTS); /* in tispxipx.h*/
unitdata.opt.maxlen = sizeof(IPX_OPTS);
unitdata.udata.buf = (char *)&dataBuf; /* this is where the data will */
unitdata.udata.len = sizeof(dataBuf); /* be returned */
unitdata.udata.maxlen = DATABUFSIZE;
/* kbhit is of limited use here. Since t_rcvudata is
executing in blocking mode, while( !kbhit() ) only
gets evaluated after a packet has been received */
while(!kbhit())
{
dataPos=0;
printf("Listening... \n");
if ((t_rcvudata(fd, &unitdata, (short NWFAR *)&flags)) == -1)
{
/* this is more for non-blocking mode, this code should
never execute in blocking mode */
if(t_errno != TNODATA)
{
t_error("Error Receiving data");
exit(1);
}
}
else /* receive succeeded */
{
memcpy(&dataBuf2[dataPos],unitdata.udata.buf,DATABUFSIZE);
/* This is just so we can print the data to check it. */
dataBuf2[dataPos+unitdata.udata.len+1]='\0';
/* There is more data to process for this packet */
while (flags==T_MORE)
{
flags=0;
printf(".");
dataPos +=DATABUFSIZE;
t_rcvudata(fd, &unitdata, (short NWFAR *)&flags);
memcpy(&dataBuf2[dataPos], unitdata.udata.buf,DATABUFSIZE);
/* This is just so we can print the data to check it */
dataBuf2[dataPos+unitdata.udata.len+1]='\0';
}
printf("Data Received \nSize %d\n- %s\n",(strlen(dataBuf2)-1),dataBuf2);
}
}
}