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

I was wondering if its possible to control weather a port is open and listening or not using perl.

If it is possible to control ports with perl could someone give me a hand with it?

I've tried to find ways to do this but to no avail. I'm all googled out as of right now so I thought I would ask the perl monks while I had the chance today and hope I could get a positive response.

I'm wanting to create a program that will open a particular port. Then a program that will be able to connect to that port.
What I'm trying to do is sort of setup my own ftp server and client. I'm getting a new computer at some point and it would make life easier to transfer files like this. I know there are tons of ways out there to do this but I would like to code something to do it myself in perl.

If I can find a way to open/close ports using perl I could probably create a client and server program that would put that knowledge to use. Of course I would post it here on perl monks.

Is there anyone who could give me a hand?

I know that "re-inventing" the wheel isn't really useful to the community but is there anything wrong with wanting to know how a program does what it does?

I'm not looking for a program or direct solution to the above problem to be handed to me. Thats not what I am asking for. I want a little guidance so I can find my own way to do this.

Replies are listed 'Best First'.
Re: Port control using perl?
by jethro (Monsignor) on Jul 30, 2008 at 01:37 UTC
    The commands to use sockets in perl are accept, bind, connect, getpeername, getsocketname, getsockopt, listen, recv, send, setsockopt, shutdown, socket and socketpair.

    These calls are just perl versions of standard unix calls. So you can either read about them in the perl man pages (in perlfunc and in perlfaq9) or just read a book about socket or unix network programming. Or find a website that gives information about that.

    You could also dissect some perl module like Net::FTP but I would advise against that, at least in the beginning.

    To make a perl script a "port controller" for another program for which you don't have the source code, you could let that other program listen to a port opened by your perl script and let the script open the port to the outside world. That is, your script would work as a pipe/filter between the program and the outside world.

    If you have the source code, it would be far easier to patch the program to listen to signals sent by the perl script.

    Another way would be that your script just kills the program that listens on that port when it wants the port closed, and restarts it if it wants to open the port.

      thank you very much :) thats exactly what i needed.
      Failure isn't about falling. It's remaining where you have fallen.
Re: Port control using perl?
by Khen1950fx (Canon) on Jul 30, 2008 at 00:26 UTC
      thanks for the suggestion. but i would still like to learn how to open a port myself using perl instead of relying on that. i dont want to use it just for ftp. knowing me im going to make a game at some point and a game that can connect to a server to save status or create a leaderboard would be nice.

      and like i said. i want to learn how to do it myself because the knowledge could be useful for more than just transferring files.
      Failure isn't about falling. It's remaining where you have fallen.
        I would practice with Net::FTPServer for a while, then move up to Net::Server. That will give you a foundation that will get you ready to handle sockets. If you encounter any problems, post your questions. There are a lot of monks here that can help you with that. Probably the best thing to do right now is to try your hand at writing some code for your ftp client and go from there. Good luck and have fun...