Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Socket Handle not sending data ?

by evilkamikaze (Initiate)
on Jul 09, 2009 at 02:26 UTC ( #778459=perlquestion: print w/ replies, xml ) Need Help??
evilkamikaze has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys,

I am stuck in a problem and can't figure out what's going on.

What I am doing is this:

I have an array of ports that I need to connect to. For each port in this array I call a function ConnectToPort.

ConnectToPort function creates a socket, and if it successfully connects to the given port (which we have passed as argument) it adds the socket file handle into a text file along with the port number. Now when I read this socket file handle from the file and try to send something on that file handle, I get an error stating that Can't locate object method "send" via package "IO::socket::INET"

I am guessing that it is because when I wrote the socket file handle to a file, it is converted to string. (pardon me if this is really stupid. I am new to perl)

Any help would be greatly appreciated.

Thanks

Comment on Socket Handle not sending data ?
Re: Socket Handle not sending data ?
by jbt (Chaplain) on Jul 09, 2009 at 03:36 UTC
    It is not clear to me why you are saving the socket file handle into a text file. Handles are not valid after you exit a script, assuming your intent is to "save" the connection. Please post your code so that we can offer a solution.

      Hi, Thanks for the reply. Yes I do understand that Handle won't be valid after script exit.

      I doing this because, I am sending large amount of data on multiple sockets. So, I want to check everytime before sending the data that whether the socket is still active or not. If it is not active then I create a new thread which calls function putinBadFile with argument as port number. This function takes this port number, search in a file and removes it from there. (This is the same file in which I have stored entries during connection i.e. activeConnection file) This thread will now add this port number to a badFile and try to restablish connection with it. If it is successful, then it will remove this port number from badFile and once again add new sockethandle along with port number in activeConnections file.

      Now, my main thread, which is sending data, after particular time, checks if the file with active connections is updated. If yes, then reload the array of active socket handles else continue sending data to current active ports.

      Please let me know, if this clears up my question a little bit. Thanks

        Lauching a thread everytime to check the availability of the socket connection is not a good idea.
        just check defined on socket handle or check eof on socket handle or establish a heart beat mechanism to know the liveliness of the socket/program.
        If you show the piece of code which requires efficiency, we would able to tell you what could be the best way of dealing it.


        Vivek
        -- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.
Re: Socket Handle not sending data ?
by targetsmart (Curate) on Jul 09, 2009 at 10:42 UTC
    push that created socket handle(no need to push the port number too) into an array, and iterate through that array to send data when you want to.
    that text file idea won't work.
    I am not sure about, whether one can persistently store a socket handle into a file and use it on the next launch of the same script. This could happen rarely and I think you have not encountered a relevant situation like this.


    Vivek
    -- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.

          For starters, please use <c>...</c> instead of <pre>...</pre>.

          It looks like your object got stringified

          $ perl -e' > sub method {} > my $o = bless({}); > $o->method(); # Ok > $o = "$o"; > $o->method(); > ' Can't locate object method "method" via package "main=HASH(0x814ec28)" + (perhaps you forgot to load "main=HASH(0x814ec28)"?) at -e line 6.

          Tracing back I see you read a string from a file and expect it to magically become a socket handle. That's not gonna happen.

          In fact, the handle gets closed at the end of PortConn as the last reference to it ($rethandle) goes out of scope.

          The idea is fundamentally flawed. A handle is a token that represents a resource the OS has handed your process. The token is useless to any other process. If you want to store something, you need to store the connection details so you can re-establish the connection.

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlquestion [id://778459]
    Approved by AnomalousMonk
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (8)
    As of 2014-11-27 14:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (184 votes), past polls