Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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.


Replies are listed 'Best First'.
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.

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

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

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlquestion [id://778459]
    Approved by AnomalousMonk
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (8)
    As of 2018-03-17 19:10 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (225 votes). Check out past polls.