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

Dear community, I'm programming a simple socket client who connect to a server and send data. The below code works perfect, but I'm searching a method to check connection prior to send data. This because I'm sending data continuosly using a loop, and if, for some reason, the connection goes down, the script hangs. So, is there a way to check if the connection is established and, if not, call again OpenSocket Sub inside the loop?
#!/usr/bin/perl use IO::Socket; my $sock1; sub OpenSocket{ $sock1 = new IO::Socket::INET( PeerAddr => '192.168.10.7', PeerPort => 8000, Proto => 'tcp'); $sock1 or die "no socket :$!"; } ################ START SCRIPT ################# OpenSocket(); $str="Something to send"; while (1) { $sock1->send($str); } close($sock1);

Replies are listed 'Best First'.
Re: Check connection state prior to send data
by BrowserUk (Patriarch) on Sep 24, 2016 at 07:04 UTC

    Take a look at select, IO::Select, PerlIPC.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Sorry, I don't wish to be controversial, but your reply sounds like: "check perlmonks for solution" :-)

        You achieved your wish. That's not controversial.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Check connection state prior to send data
by Lucas Rey (Sexton) on Sep 26, 2016 at 06:02 UTC
    Dear community, thank you all to partecipate, I would like to share my (very simply) solution to my question. The following code works perfect and suit my needs.

    BTW, the concept behind solution is simple too:
    -- Open a socket
    -- Server is listining?
    ---- YES: OK Connect
    ---- NO: Enter in loop until Server will be ready
    -- Send data
    -- Is there any response from server?
    ---- YES: Continue sending loop
    ---- NO: (Empty Response): Call OpenSocket and wait untill Server will be ready again.
    #!/usr/bin/perl use IO::Socket; my $sock1; sub OpenSocket{ while ( !$sock1 ) { $sock1 = IO::Socket::INET->new ( Proto => "tcp", PeerAddr => "192.168.10.7", PeerPort => "8000", Timeout => "1") } } ################ START SCRIPT ################# OpenSocket(); sleep(1); my $str="DATA TO SEND" while(1) { $sock1->send($str); sleep(1); $sock1->recv($channel1, 128); if ( !$channel1 ) { $sock1->shutdown(2); $sock1=""; print "DISCONNECTED... I'M RECONNECTING AND RESEND PREVIOUS PACKET +\n"; OpenSocket(); sleep(1); $sock1->send($str); } } close($sock1);

      What do you think this code does? $sock1  = shift; Ie. Where are you shifting the value you are assigning to $sock1 from?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Thank you, that was a typo left there from previous tests. I modified my post with the right code. That one works perfect also with one or more parameters.