UCM Unitec Home Page
      

Generic UCM Server Function

 UcmServer(F)

Synopsis   Environment   Description   Diagnostics   Notes   See also   Examples

Synopsis

#include "ucm.h"

int UcmServer(service, DoService);
char *service;
int (*DoService)(T_SOCKET sock);

Environment

Unix, Win32

Description

The UcmServer function provides the same services as the UcmServer(U) utility. In contrast to the utility implementation, UcmServer allows you to develop self-contained servers. This server type features faster connect times, since it does not have to use the exec(2) system call. It results however in larger executables for the daemon, since it includes the full server logic. The call of this function will detach the program from the parent process, unless the parent has the PID 1, i.e. is the init(1) process.

The first argument identifies the service that the socket will be assigned to. Service may be given as symbolic TCP/IP service name or as numeric port number. When using the symbolic service name, it must be defined in /etc/services or the Windows services file with type ftp.

After each successful connect request, UcmServer forks (fork(2)) and calls the function at address DoService with the socket number sock as first and only argument. On Win32 platforms each DoService invocation is executed in its own thread. The DoService function should use UcmOpen for the new socket and return a non negative (int) return code after UcmClose.

UcmServer will fail if one or more of the following is true:

UE_NOTINIT The UCM software has not been initialize (see UcmInit)
UE_INVALID An invalid parameter has been passed.
UE_SERVICE The value service does not identify a known service (i.e. is not defined in /etc/services).
UE_CONNECT The bind(2) or accept(2) system call has failed.
UE_INTR The accept(2) system call has been interrupted by a signal.

Diagnostics

Upon successful completion UcmServer returns (int) >= 0. Otherwise, a value of UCM_ERROR is returned and ucmErrno is set to indicate the error.

Notes

Typically programs using the UcmServer function, are invoked at boot time from the /etc/rc startup file or from /etc/inittab (System V only).

See also

init(1), initab(4), services(4),  UcmOpen, UcmServer(U), UcmTerm

Examples

 
#include "ucm.h"

#if defined(__STDC__)
static int DoService(T_SOCKET sock);
#else
static int DoService();
#endif

main()
{
    ucmProgName = argv[0]; /* for UCM error messages */
    UcmInit();
    UcmServer("7001", DoService);
    UcmTerm();
    return 0;
}

static int DoService(sock)

T_SOCKET sock;
{
    T_UCM_MSG msg;     /* buffer to hold message */
    T_UCM_MSGID msgId; /* buffer to hold message ID */

    if (UcmOpen(sock))
        Error("UcmOpen failed");
    while (UcmRecv(sock, msgId, msg, -1, UF_NOFLAGS) > 0)
    {
        /* this server just echoes back its input */
        UcmSendf(sock, "Got ‘%s:%s’", msgId, msg);
    }
    UcmClose(sock);
    return 0;
}

 

Back to top