I'll probably figure this out 5 minutes after posting this, but hey, if
I don't post the question, I won't figure it out. Sacrifices to the
coding gods and all that.
Anyway, I've traditionally written UDP servers such that when I need to
respond to a client, I just do a sendto() using the address I grabbed
via recvfrom(). This has always worked when I used the same socket for
both the send and receive, i.e.
recvfrom( mySocket, ... );
sendto( mySocket, ... );
I've tried to generalize some things, and in the process I wanted to
remove the constraint that I send data out on the same socket I
received that data from, but it's not working. i.e.
recvfrom( mySocket, &sa, ... );
sendto( defaultSocket, &sa, ... );
This isn't working. sendto() doesn't fail, but the client never sees
that return packet.
The defaultSocket is just a kind of dummy socket I would like to keep
around in case I want to just send random crap out on the wire without
being forced to connect and/or bind first.
I'm assuming I'm making a major assumption here that isn't valid, but
after rereading Stevens again, I can't figure out what I might be doing
wrong. I tried bind()ing the defaultSocket before use to an ephemeral
port, and that didn't make any difference. If I go back to using the
same socket for send/receive, it works fine, but if I substitute a
different socket, it stops working.
I can't see this being an illegal use of the sockets, since otherwise
it would mean you could never sendto() a destination that you hadn't
previously, say, connect()ed to or recvfrom()ed, which doesn't seem
right at all.
Anyway, just to clarify:
//this works (pseudo code obviously)
mySocket = socket( ... );
bind( mySocket, myPort );
recvfrom( mySocket, &sa, ... );
sendto( mySocket, &sa .. );
//this doesn't (in that the client never sees a response)
mySocket = socket( ... );
bind( mySocket, myPort );
defaultSocket = socket( ... );
//doesn't matter if I bind defaultSocket() or not
recvfrom( mySocket, &sa, ... );
sendto( defaultSocket, &sa ... );
-Hook
|