"Does anyone else use this ? Am I right in assumeing that the port is TCP
        (SOCK_STREAM)? Is 2106 the right number?"

Yep, and nope.

port 2106, udp.

Here's my version:

--------------------------------------------------------------------------------


/*
** Just pick up any junk you find on port 2106.
*/
#include        <stdio.h>
#include        <netdb.h>
#include        <sys/types.h>
#include        <sys/socket.h>
#include        <netinet/in.h>
#include        <time.h>

        char host[128];
        char * hend;
        char data[256];

main()
        {
        struct hostent * he;
        struct in_addr ai;
        /*
        ** First, figure out the host name.
        */
        gethostname( host, sizeof(host) );
        printf( "Hostname %s\n", host );

        /*
        ** Now, figure out what the address is...
        */
        if ((he = gethostbyname( host )) == 0)
                {
                printf( "Can't get host address for %s\n", host );
                exit(1);
                }
        /*
        ** Whaata know joe?
        */
        bcopy( he->h_addr, &ai.s_addr, sizeof(ai) );
        printf( "Host %s hostent %x Address %x Port %x\n",
                host, he->h_addr, ai, 2106 );
        /*
        ** Get the formal name...
        */
        he = gethostbyaddr( &ai.s_addr, sizeof(ai), AF_INET );
        if (he) {
            printf( "Formal name %s\n", he->h_name );
            strcpy( host, he->h_name );
        }

        /*
        ** Find the end of the host name.
        */
        hend = &host[strlen(host)-1];

        /*
        ** Construct the sockaddr in, get the socket,
        ** bind it, then listen...
        */
                {
                int s;
                struct sockaddr_in addr, from;

                s = socket( AF_INET, SOCK_DGRAM, 0 );

                addr.sin_family = AF_INET;
                addr.sin_addr = ai;
                addr.sin_port = 2106;

                if (bind( s, &addr, sizeof(addr) ))
                        {
                        perror( "bind" );
                        exit(1);
                        }


                while(1)
                        {
                        int cc;
                        int now;
                        extern struct tm * localtime();
                        struct tm * ptm;

                        cc = recvfrom(s, data, sizeof(data),
                                0, &from, sizeof(from));

                        now = time(0);
                        ptm = localtime(&now);


                        if (cc == 0)
                                break;
                                {
                                extern struct hostent *gethostbyaddr();
                                struct hostent * he;

                                he = gethostbyaddr( &from.sin_addr,
                                        sizeof(from.sin_addr), AF_INET );

                                if (he) {
                                    char * tend = hend;
                                    char * cend = &(he->h_name[
                                            strlen(he->h_name)-1]);

                                    while( *cend == *tend ) {
                                        cend --; tend --;
                                    }
                                    if (tend <= host) {
                                        strcpy( he->h_name, "==" );
                                    } else if (tend != hend) {
                                        cend++;
                                        *cend = 0;
                                    }

                                    printf( "%2.2d:%2.2d:%2.2d %s:%d ",
                                            ptm->tm_hour,
                                            ptm->tm_min,
                                            ptm->tm_sec,
                                            he->h_name,
                                            from.sin_port );
                                } else
                                    printf( "%2.2d:%2.2d:%2.2d %x:%d ",
                                            ptm->tm_hour,
                                            ptm->tm_min,
                                            ptm->tm_sec,
                                            from.sin_addr.s_addr, from.sin_port
);
                                }

                        data[cc] = 0;
                        printf( "%*.*s", cc, cc, data );
                        }
                }
        }


Reply via email to