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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |