HalNineThousand has asked for the wisdom of the Perl Monks concerning the following question:

I've been searching and I've found a LOT of tutorials on creating sockets in Perl. In a few cases there's even chat applications.

What I haven't found is anything on how to send AND receive on the same port.

I want to be able to send out a broadcast message (which I know is UDP), then listen for a response on the same port. This is a simple app to find a system when you can't be sure where it is on the LAN (in case I can't get an assigned IP address for the system).

Are there any tutorials or is there any information using a socket on one port for sending AND receiving? (Or a way to make one socket to listen and one to send?)

Replies are listed 'Best First'.
Re: Sending AND Receiving on the Same Port
by ikegami (Patriarch) on Feb 23, 2011 at 22:09 UTC

    What I haven't found is anything on how to send AND receive on the same port.

    It's impossible for a socket to do otherwise.

    Maybe the bit you are missing is that you need to use the same socket for sending and receiving? Unlike (most?) pipes, sockets are bidirectional.

      Okay, I get that. I've been playing around with it.

      Now there's only one other issue: My first message is a broadcast message, so I have the socket set to broadcast. It appears the issue is that I can't listen on the same socket I broadcast on.

      Am I right on this? If so, how do I sent out a broadcast message, then listen for a reply on the same port? Since it seems if I broadcast, I can't also listen, in the time it takes to make a new socket, won't data be incoming and lost?

        It appears the issue is that I can't listen on the same socket I broadcast on. Am I right on this?

        That makes no sense. Each message is completely independent in UDP. You can send to anyone and receive from anyone with a single socket.

        in the time it takes to make a new socket, won't data be incoming and lost?

        Why would someone be sending to a socket you haven't created yet?