Sack Library Documentation
ContentsIndexReferenceHome
PreviousUpNext
sack::network Namespace

Event based networking interface. 

 

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;
}
       
Name 
Description 
tcp 
Transmission Control Protocol connection methods. This controls opening sockets that are based on TCP. 
udp 
User Datagram Packet connection methods. This controls opening sockets that are based on UDP. 
Name 
Description 
The following table lists classes in this documentation. 
The following table lists functions in this documentation. 
The following table lists structs, records, enums in this documentation. 
The following table lists types in this documentation. 
The following table lists macros in this documentation. 
 
Name 
Description 
 
 
 
Name 
Description 
 
result with TRUE if equal, else FALSE 
 
This is function sack::network::CompareAddressEx. 
 
dwIP would be for 1.2.3.4 (0x04030201 - memory 01 02 03 04) - network order 
 
dwIP would be for 1.2.3.4 (0x01020304 - memory 04 03 02 01) - host order VERY RARE! 
 
This is function sack::network::CreateLocal. 
 
This is function sack::network::CreateRemote. 
 
this is the preferred method to create an address name may be "* / *" with a slash, then the address result will be a unix socket (if supported) name may have an options ":port" port number associated, if there is no port, then the default port is used. 
 
PING.C 
 
This is function sack::network::DoPingEx. 
 
WHOIS.C 
 
return a copy of this address... 
 
This is function sack::network::GetAddressParts. 
 
This is function sack::network::GetMacAddress. 
 
This is function sack::network::GetNetworkInt. 
 
This is function sack::network::GetNetworkLong. 
 
This is function sack::network::GetNetworkWord. 
 
typedef struct Client { unsigned char Private_Structure_information_here; }CLIENT, *PCLIENT; #endif 
 
compare this address to see if it is any of my IPv4 interfaces 
 
returns true if network layer still active... Shutdown these network services, stop the network thread, and close all sockets open, releasing all internal resources. 
 
This is function sack::network::NetworkLockEx. 
 
This is function sack::network::NetworkQuit. 
 
This is function sack::network::NetworkUnlockEx. 
 
wwords is BYTES and wClients=16 is defaulted to 16 
 
release a socket resource that has been created by an above routine 
 
This is function sack::network::RemoveClientExx. 
 
This is function sack::network::SetAddressPort. 
 
This is function sack::network::SetNetworkCloseCallback. 
 
This is function sack::network::SetNetworkReadComplete. 
 
This is function sack::network::SetNetworkWriteComplete. 
 
This is function sack::network::SetNetworkCloseCallback. 
 
This is function sack::network::SetNetworkInt. 
 
This is function sack::network::SetNetworkLong. 
 
This is function sack::network::SetNetworkReadComplete. 
 
Obsolete. See SetNetworkLong
 
This is function sack::network::SetNetworkWriteComplete. 
 
This is function sack::network::SetNonDefaultPort. 
Name 
Description 
This is function sack::network::NetworkLockEx. 
wwords is BYTES and wClients=16 is defaulted to 16 
This is function sack::network::NetworkUnlockEx. 
This is function sack::network::RemoveClientExx. 
This is function sack::network::RemoveClientExx. 
This is macro sack::network::SA_COMPARE_FULL. 
This is macro sack::network::SA_COMPARE_IP. 
This is function sack::network::SetNetworkCloseCallback. 
This is function sack::network::SetNetworkReadComplete. 
This is function sack::network::SetNetworkWriteComplete. 
 
Name 
Description 
 
Symbols which may be passed to GetNetworkLong to get internal parts of the client. 
 
 
Name 
Description 
This is type sack::network::cCloseCallback. 
This is type sack::network::cConnectCallback. 
This is type sack::network::cNotifyCallback. 
This is type sack::network::cppCloseCallback. 
This is type sack::network::cppConnectCallback. 
This is type sack::network::cppNotifyCallback. 
This is type sack::network::cppReadComplete. 
This is type sack::network::cppReadCompleteEx. 
This is type sack::network::cppWriteComplete. 
This is type sack::network::cReadComplete. 
This is type sack::network::cReadCompleteEx. 
This is type sack::network::cWriteComplete. 
ifndef CLIENT_DEFINED 
This is type sack::network::PNETWORK. 
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.
What do you think about this topic? Send feedback!