recvmsg - Unix

This is a discussion on recvmsg - Unix ; Hi, How to get the senders address after recvmsg is called. I am using recvmsg to get UDP data and want to send ACK back to sender. Thanks...

+ Reply to Thread
Results 1 to 6 of 6

Thread: recvmsg

  1. recvmsg

    Hi,
    How to get the senders address after recvmsg is called.
    I am using recvmsg to get UDP data and want to send ACK back to
    sender.
    Thanks


  2. Re: recvmsg

    mrunalp@gmail.com wrote:
    > Hi,
    > How to get the senders address after recvmsg is called.
    > I am using recvmsg to get UDP data and want to send ACK back to
    > sender.
    > Thanks
    >

    Use recvfrom() instead.

    Robert

  3. Re: recvmsg

    On Apr 23, 1:29 pm, mrun...@gmail.com wrote:
    > How to get the senders address after recvmsg is called.
    > I am using recvmsg to get UDP data and want to send ACK back to
    > sender.


    To quote the recvmsg() manpage:

    Here msg_name and msg_namelen specify the source address if the
    socket is
    unconnected; msg_name may be given as a null pointer if no names
    are de-
    sired or required. <...>

    So, initialize the msg_name and msg_namelen members of the struct
    msghdr whose address you're passing to recvmsg() such that msg_name
    points to a buffer of sufficient size to hold the expected address and
    msg_namelen contains the size of that buffer. If this is protocol
    independent code, then allocating (perhaps as a local variable) a
    struct sockaddr_storage and pointing msg_name at that is likely to be
    the simplest solution.

    If the socket is connected, then your recv* calls will only return
    datagrams whose source address is the address to which you connected
    the socket. I suppose you could use getpeername() to get that address
    if it was impossible to pass the address directly with the socket.


    Philip Guenther


  4. Re: recvmsg

    On Apr 23, 8:29 pm, mrun...@gmail.com wrote:

    > How to get the senders address after recvmsg is called.
    > I am using recvmsg to get UDP data and want to send ACK back to
    > sender.



    If you want to send an ACK, perhaps you should consider
    using TCP.

    --
    Bill Pursell


  5. Re: recvmsg

    On 4月24日, 上午3时29分, mrun...@gmail.com wrote:
    > Hi,
    > How to get the senders address after recvmsg is called.
    > I am using recvmsg to get UDP data and want to send ACK back to
    > sender.
    > Thanks


    Be careful with this programming scheme. Because if the other peer is
    behind any NAT(such as router), when you want to send the ACK back,
    the NAT window may has been closed by the router. Using TCP is more
    reliable.


  6. Re: recvmsg

    mrunalp wrote:

    > How to get the senders address after recvmsg is called.


    struct msghdr blob = { 0 };

    struct sockaddr_in src;
    blob.msg_name = &src;
    blob.msg_namelen = sizeof src;

    int res = recvmsg(sock, &blob, flags);

+ Reply to Thread