Reading between the lines of the OP and 728564, I think you want something like this:

#!/usr/bin/perl use strict; use warnings; use IO::Socket; use threads; use Thread::Queue; use Mysql; #ignore child processes to prevent zombies $SIG{CHLD} = 'IGNORE'; #get the port to bind to or default to 8000 my $port = $ARGV[0] || 8000; ## Queue for commands input from the keyboard ## That will be sent to the currrently connected device my $qin = Thread::Queue -> new; ## Queue for the data inbound from the device my $qout = Thread::Queue -> new; my $listen_socket = IO::Socket::INET->new( LocalPort => $port, Listen => 1, Proto => 'tcp', Reuse => 1 ) or die "Cant't create a listening socket: $@"; warn "Server ready. Waiting for connections on $port ... \n"; async { ## Monitor the keyboard and forward any input ## to the currently connected client GPS device while( <STDIN> ) { chomp; $qin->enqueue( $_ ); } }; async{ ## Monitor the queue and do whatever with the inbound data while( my $data = $qout->dequeue ) { ## Do something with the gps data (like write it to a database +?) } }; ## Spawn a new connection if the device reattaches. ## Old connections clean themselves up automatically while (my $connection = $listen_socket->accept) { threads->create ("read_data", $qin, $qout, $connection)->detach; print $connection; } sub read_data { # accept data from the socket and put it on the queue my( $qin, $qout, $socket ) = @_; while (<$socket>) { print "$_"; ## Process data from connected devices $qout -> enqueue(time." $_"); ## if there are any commands pending, send them to the GPS dev +ice while( $qin->pending ) { my $cmd = $qin->dequeue; print $socket $cmd; } } close $socket; }

Based on your original above, this is light on error handling. The main changes are:

  1. It starts two extra threads (using async()).
    1. One monitors the keyboard and forwards any input (commands) via a second queue, to the current client thread which will in turn forward those to the device.

      This could be an additional listening socket so the command input could be initiated via another script, or even remotely.

    2. The second reads your original queue (which you were doing nothing with), allowing you to process the data asynchronously from it's arrival.

      Going by the use MySql; in the OP, you intend to put it into a DB?

  2. Starts a second queue used to pass keyboard input commands to the currently connected device.

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".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: tcp server (bidirectional) by BrowserUk
in thread tcp server (bidirectional) by igor1212

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.