Jeff Abrahamson on 5 Mar 2004 16:11:02 -0000


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

[PLUG] UDP and ephemeral ports


I'm confused about UDP port numbers in an app I'm writing.

I have a simple toy echo client/server.

The client does

    sendto(...)
    recvfrom(...)

The server does

    bind(...)
    while(1) {
        recvfrom(...)
        sendto(...)
    }

The result is that the client sends the server a string and then gets
it back from the server.  Works great.

I have my reall app which is more symmetric: both sides have done
bind.

Now in my echo server, the from port filled in by recvfrom was the
right place to send that reply packet.  But in my much more
complicated example, it's not.

Now that I think about it, I don't fully understand why it works in
the simple echo server.  When the client sends to remote:8000 and then
does a recv, even on the same socket, why is the local port number
preserved?  ("Because that's the way it is, son...")

But in the more complex example, it's equally mysterious to me.  Does
the fact that I've bound both sockets mean that this simple business
of using the from address provided by recvfrom no longer works for
sending back?

For the moment my simple echo client/server lives here:

    http://www.purple.com/udp/

(Gabe and others who don't like glib/gtk stuff may note that it's just
habit and the use of g_assert, so may be stripped if you actually want
to compile and run on a non-linux box.)

The more complex example is too much code to understand easily, so I
won't post it.  I know a work-around, anyway, to get done what I need
to, but I'd really like to understand what's happening.

Thanks for any thoughts.

-- 
 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