in reply to IO::Socket Bi-directional comm

Since no one else has answered you, I will, so you won't feel like you are being ignored. You are not, it's just that the question demands too much from the reader.

First, you probably would get more answers if you were using Perl for doing your socket work, rather than netcat and telnet. Not many monks use that combination.

Second, your question is vague, at least to me. It seems like you are saying "it dosn't work and I don't know why, and here is a partial snippet of some complex forking code". You really should post a complete snippet that does ( or dosn't work) and point out where you need changes. Try to limit the snippet to only show the problem, and make it workable, so others can test it.


I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: IO::Socket Bi-directional comm
by carric (Beadle) on Jan 28, 2006 at 03:35 UTC
    I appreciate your feedback.

    I realize one side of this problem isn't Perl, but what I was aiming for with my question was if someone else had some example code (client/server) that would demonstrate more two-way communication. I didn't post the whole thing, because my sub was only somewhat modified from the original example in the perlipc docs for the calc server. If you look at the IO::Socket, multi-threaded example, that IS my script. I just don't need a calculator server. =)

    My daemon needs to accept a connection, and then print all of the command output from the client to a log file (taking the multi-threaded facet into consideration).

    So, to summarize, I am asking if someone has some good sample code showing:

    1. Client makes a connection to server
    2. Server prints stuff to client socket
    3. The results of that input to the client (the client output) is captured by the server.


    Again, thank you for responding to my post. =) I apologize for not being more clear the first time.
      Check out Sockets-File-Upload with Net::EasyTCP. It carries on a dialog between client and server, and saves a file. It could easily be extended/modified to do what you want. The only drawback to using Net::EasyTCP is that it uses IO::Select to handle multiple connections. (as opposed to forking or threading). This means that one long connection to a client will block connections from other clients, so it will be good if the exchanges are relatively short.

      Also check out ztk-enchat encrypted server client for a Tk gui version doing chat, in case you want a gui.

      These are just my old code, others may have better code examples suited to your purpose. Net::EasyTCP is nice to use, because it lets you have password-protected ports, automatic encryption, and it will stingify hashes for you, so you can actually pass hashes of data through the socket.


      I'm not really a human, but I play one on earth. flash japh
        Much thanks brother. I'll check it out.
        I've narrowed it down to a small section:

        while ( <$client> ) { next unless /\S/; # blank line if (/quit|exit/i) { last; } elsif (/microsoft/i) {foreach $command(@commands) { print $client "$command\r"; } } else { last; } } continue { }

        So, we skip empty lines; if we see "quit/exit", we close the socket, if we see "microsoft", we send a list of commands to the client, and that's where the problem crops up; I know it prints the whole list of commands to the client if the code is left like this, but I want the results of these commands, so I added:
        while ( <$client> ) { next unless /\S/; # blank line if (/quit|exit/i) { last; } elsif (/microsoft/i) {foreach $command(@commands) { print $client "$command\r"; while( <$client> ) { print;} } } else { last; } } continue { }
        Now, what happens is I get ALL the output from the first command I sent to $client, but the foreach loop is no longer able to iterate through all of "@commands" because I think it's hanging in the "while" loop. I have tried various things like putting "next;" after "print;", but I either get none of the command output, or I get what I want, but only for the first command, and then the client never closes it's connection (I send "exit" as the third command). I am trying to figure out if I'm just coming at this the wrong way (I have put various print statements within the top "while" loop, as well as trying to create an array to which all output would be stored, and then print the array:
        while ( <$client> ) { while( my @response = <$client>) { next unless /\S/; # blank line if (/quit|exit/i) { last; } elsif (/microsoft/i) {foreach $command(@commands) { print $client "$command\r"; } print @client; } else { last; } } continue { } }
        None of it is getting the results I want. #######################
        Just in case anyone ever runs into this.. I figured it out:
        ... snip ... open(LOGFILE,>>"mylog.log") or die "Can't open mylog.log\n"; print $client "$cmds\r"; while( <$client> ) { print LOGFILE; } close $client; }
        What I wanted to do was print commands to a netcat listener that was setup with "-e cmd.exe" (a la "reverse telnet") and capture all the output to a log file. Print commands to the socket, then do a "while( <$client>)". That seems to get the job done.

        Good luck, and thanks for all the input.