Jeff Abrahamson on 22 Feb 2004 16:41:02 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] UDP programming question


Here's a code snippet.  It should receive a UDP packet and then send
it back to the guy it got it from.  For brevity in this email, I've
stripped error checking.

What actually happens is that it receives the UDP packet's contents
just fine, but faddr is not filled in, remaining all zeroes.  So the
sendto fails with a bad parameter error.

I've been googling, gdb'ing, and reading man pages for too long.  Any
suggestions on what I mean to do here?

Thanks.


    struct sockaddr_in faddr; /* address of remote peer */
    int num_read;

    memset(&faddr, 0, sizeof(faddr));
    num_read = recvfrom(sock, &buf, MAX_MSG_SIZE, 0,
                        (struct sockaddr *)&faddr, &fromlen);
    printf("Server received message: '%s'\n", buf);

    /* Now just send it back to client to show that we can. */
    ret = sendto(sock, &buf, num_read, 0,
                 (struct sockaddr *)&faddr, sizeof(faddr));

-- 
 Jeff

 Jeff Abrahamson  <http://www.purple.com/jeff/>
 GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B

Attachment: signature.asc
Description: Digital signature