On Friday 02 July 2004 04:25 am, Gaurav Mengi wrote:
> Hi Annada,
>              I was waiting for some pointers from ur side...
>
> As u have mentioned I had already done that......
>
> I'll be glad If u can send me the code(snapshot) of N/w Programming using
> UDP Sockets which works successfully on the device...........especially
> the snapshot of that code where ur receiving the message

In case it helps.  Here is what I've used.  Ignore the DNS cache stuff.  
This gives a random receive port.  From my server I reply to whatever port 
the packet indicates.

I noticed that in your code you don't NetLibSocketConnect().  This may not 
be required with UDP, but I wouldn't be surprised if PalmOS gets messed up 
without it.

Int16 connectsockudp(char *host, char *service, UInt16 refnum, Int32 timeout)
{
    NetSocketRef sock;
    Int16 one;
    Int16 i1;
    NetSocketAddrINType saddr;
    NetHostInfoPtr phe;
    NetHostInfoBufType AppHostInfo;
    Boolean nameresolved=false;
    UInt32 hosthash;

    one = 1;

    MemSet((char *) &saddr, sizeof(saddr), 0);
    saddr.family = netSocketAddrINET;
    saddr.port = NetHToNS(StrAToI(service));

    if ((sock = NetLibSocketOpen(refnum, netSocketAddrINET,
        netSocketTypeDatagram, 0, timeout, &errno)) < 0) {
        return 0;
    }

    /* see if we can use our DNS cache */
    if (!FtrGet(MainAppID, FTR_HOSTHASH, &hosthash) &&
        (rotating_hash(host, StrLen(host)) == hosthash) &&
        !FtrGet(MainAppID, FTR_HOSTIP, (UInt32 *)&saddr.addr)) {
        nameresolved = true;
    }

    /*
     * Map host name to IP address, allowing for dotted decimal.
     * I try to assimilate the name first to see if it is just
     * a dotted IP.  If not, then I use the DNS to resolve.
     *
     * Also it tries to retrieve a cached IP from a feature.
     */
    if (!nameresolved ||
        ((saddr.addr = NetLibAddrAToIN(refnum, host)) == -1)) {
        if ((phe = NetLibGetHostByName(refnum, host, &AppHostInfo,
            timeout, &errno)) != 0) {
            /*
             * Sometimes the first address in the list is zero.  This
             * makes me have to search through them to find the first
             * non-zero address.
             */
            for (i1=0; i1 < netDNSMaxAddresses; i1++) {
                if (phe->addrListP[i1] != NULL) {
                    if (*phe->addrListP[i1] != '\0') {
                        MemMove((char *) &saddr.addr,
                            (char *) phe->addrListP[i1], phe->addrLen);
                        nameresolved = true;
                        break;
                    }
                }
            }
        }
        if (!nameresolved) {
            NetLibSocketClose(refnum, sock, timeout, &errno);
            return 0;
        } else {
            /*
             * Cache the IP to make future lookups quicker.  Note that
             * I store a hash of the hostname only to validate the need for
             * an uncached DNS lookup later.
             */
            FtrSet(MainAppID, FTR_HOSTHASH, rotating_hash(host, 
StrLen(host)));
            FtrSet(MainAppID, FTR_HOSTIP, (UInt32)saddr.addr);
        }
    }

    if (NetLibSocketConnect(refnum, sock, (NetSocketAddrType *) &saddr,
        sizeof(saddr), timeout, &errno) != 0) {
        NetLibSocketClose(refnum, sock, timeout, &errno);
        return 0;
    }

    /* set to non-blocking */
    NetLibSocketOptionSet(refnum, sock, netSocketOptLevelSocket,
        netSocketOptSockNonBlocking, &one, sizeof(one), timeout, &errno);
    /* turn off Nagle */
    NetLibSocketOptionSet(refnum, sock, netSocketOptLevelTCP,
        netSocketOptTCPNoDelay, &one, sizeof(one), timeout, &errno);

    /*
     * This usually fails in the emulator.  It's needed to have PalmOS
     * assign a non-zero source port.
     */
    MemSet((char *) &saddr, sizeof(saddr), 0);
    NetLibSocketBind(refnum, sock, (NetSocketAddrType *) &saddr,
        sizeof(saddr), timeout, &errno);

    return sock;
} 

An example of receiving a UDP packet:

    NetFDSetType rfds, wfds;
    unsigned char buf[32];

    netFDZero(&rfds);
    netFDZero(&wfds);
    netFDSet(sysFileDescStdIn, &rfds);
    netFDSet(udpsock, &rfds);
    rv = NetLibSelect(appref, netFDSetSize, &rfds, &wfds, NULL, tp,
        &err);
    if ((rv > 0) && netFDIsSet(udpsock, &rfds) &&
        ((bytes = NetLibReceive(appref, udpsock, buf,
        sizeof(buf), 0, 0, 0, 1, &err)) == 16) &&
        (buf[0] == 0xa5) && (buf[1] == 0x5a)) {
        ispacket = true;
        /* success */
        if (buf[6] == 1) ismail = true; /* now we'll do a full POP */
        break;
    }

-- 
/* Chris Faherty <[EMAIL PROTECTED]> */

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to