Home > Archive > Unix Programming > April 2007 > recvmsg
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| mrunalp@gmail.com 2007-04-23, 10:03 pm |
| 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
| |
| Robert Harris 2007-04-23, 10:03 pm |
| 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
| |
| guenther@gmail.com 2007-04-23, 10:03 pm |
| 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
| |
| Bill Pursell 2007-04-24, 4:07 am |
| 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
| |
| Bin Chen 2007-04-24, 7:05 pm |
| On 4=D4=C224=C8=D5, =C9=CF=CE=E73=CA=B129=B7=D6, 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.
| |
|
| 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);
|
|
|
|
|