Sack Library Documentation
ContentsIndexReferenceHome
Example

Example One : A simple client side application. Reads standard input, and writes it to a server it connects to. Read the network and write as standard output.

#include <network.h>
#include <logging.h>
#include <sharemem.h>
       
void CPROC ReadComplete( PCLIENT pc, void *bufptr, int sz )
{
   char *buf = (char*)bufptr;
    if( buf )
    {
        buf[sz] = 0;
        printf( "%s", buf );
        fflush( stdout );
    }
    else
    {
        buf = (char*)Allocate( 4097 );
        //SendTCP( pc, "Yes, I've connected", 12 );
    }
    ReadTCP( pc, buf, 4096 );
}
       
PCLIENT pc_user;
       
void CPROC Closed( PCLIENT pc )
{
   pc_user = NULL;
}
       
int main( int argc, char** argv )
{
    SOCKADDR *sa;
    if( argc < 2 )
    {
        printf( "usage: %s <Telnet IP[:port]>\n", argv[0] );
        return 0;
    }
    SystemLog( "Starting the network" );
    NetworkStart();
    SystemLog( "Started the network" );
    sa = CreateSockAddress( argv[1], 23 );
    pc_user = OpenTCPClientAddrEx( sa, ReadComplete, Closed, NULL );
    if( !pc_user )
    {
        SystemLog( "Failed to open some port as telnet" );
        printf( "failed to open %s%s\n", argv[1], strchr(argv[1],':')?"":":telnet[23]" );
        return 0;
    }
   //SendTCP( pc_user, "Some data here...", 12 );
    while( pc_user )
    {
        char buf[256];
        if( !fgets( buf, 256, stdin ) )
        {
            RemoveClient( pc_user );
            return 0;
        }
        SendTCP( pc_user, buf, strlen( buf ) );
    }
    return -1;
}

Example Two : A server application, opens a socket that it accepts connections on. Reads the socket, and writes the information it reads back to the socket as an echo.

#include <stdhdrs.h>
#include <sharemem.h>
#include <timers.h>
#include <network.h>
       
void CPROC ServerRecieve( PCLIENT pc, POINTER buf, int size )
{
    //int bytes;
    if( !buf )
    {
        buf = Allocate( 4096 );
        //SendTCP( pc, (void*)"Hi, welccome to...", 15 );
    }
    //else
        //SendTCP( pc, buf, size );
       
    // test for waitread support...
    // read will not result until the data is read.
    //bytes = WaitReadTCP( pc, buf, 4096 );
    //if( bytes > 0 )
    //   SendTCP( pc, buf, bytes );
       
    ReadTCP( pc, buf, 4095 );
    // buffer does not have anything in it....
}
       
void CPROC ClientConnected( PCLIENT pListen, PCLIENT pNew )
{
    SetNetworkReadComplete( pNew, ServerRecieve );
}
       
int main( int argc, char **argv )
{
    PCLIENT pcListen;
    SOCKADDR *port;
    if( argc < 2 )
    {
        printf( "usage: %s <listen port> (defaulting to telnet)\n", argv[0] );
        port = CreateSockAddress( "localhost:23", 23 );
    }
    else
        port = CreateSockAddress( argv[1], 23 );
    NetworkStart();
    pcListen = OpenTCPListenerAddrEx( port, ClientConnected );
    if(pcListen)
        while(1) WakeableSleep( SLEEP_FOREVER );
    else
        printf( "Failed to listen on port %s\n", argv[1] );
    return 0;
}
       
Created with a commercial version of Doc-O-Matic. In order to make this message disappear you need to register this software. If you have problems registering this software please contact us at support@toolsfactory.com.
Copyright (c) 2000+. All rights reserved.