Hello Monks,

I'm using a simple tcp server script for my gps tracking device.
Although server works fine it does not closing threads or forks when device disconnects from the server.
I'm totally new to tcp programming and I cant find the sollution.

Another question will be:
How can I send a text string from tcp server to the open socket of gps device.
I can respond to the device, but I need to initiate transmition from the server side (Maybe from some other perl script, php ...).

Thanks for your help!
Igor V.
The script is:

#!/usr/bin/perl #use strict; use warnings; use IO::Socket; use threads; use Thread::Queue; use Mysql; # use Proc::Daemon; # Proc::Daemon::Init; # use Proc::PID::File; # my $pid = Proc::PID::File->running(name => "foo"); #get the port to bind to or default to 8000 my $port = $ARGV[0] || 8000; my $queue = Thread::Queue -> new; #ignore child processes to prevent zombies $SIG{CHLD} = 'IGNORE'; #create the listen socket my $listen_socket = IO::Socket::INET->new(LocalPort => $port, Listen => 1, Proto => 'tcp', Reuse => 1); #make sure we are bound to the port die "Cant't create a listening socket: $@" unless $listen_socket; warn "Server ready. Waiting for connections on $port ... \n"; #wait for connections at the accept call while (my $connection = $listen_socket->accept) { threads->create ("read_data", $queue, $connection)->detach; print $connection; } sub read_data { # accept data from the socket and put it on the queue my ($queue, $socket) = @_; while (<$socket>) { print "$_"; // Process data from connected devices $queue -> enqueue(time." $_"); } close $socket; }

In reply to 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.