Thank you, this seems to work perfectly. I guess I was trying to be too clever!
However, I've now come across another problem. I need the call to socket->recv to be non-blocking. I see that there is a flags parameter and I'm wondering whether I need to set a flag to make the recv call return immediately but I can't find any documentation for the flags, all I can find it a very old post 129590 which doesn't really help a lot.
I've added my code so you can see what I'm trying to do but it's in no way finish yet hence all the print's to help me find out whats going on.
package Telsis::Session; use strict; use IO::Socket; use Telsis::Cmd_NewSession; use threads; use threads::shared; sub new { my ($class, %args) = @_; my $self = {}; my $replies = &share([]); push(@{$replies}, "ACTIVE"); $self->{SHELF} = $args{"Shelf"}; $self->{SESS_HANDLE} = undef; $self->{SEQ} = 1; $self->{REMOTE_IP} = $args{"RemoteIP"}; $self->{REMOTE_PORT} = defined $args{"RemotePort"} ? $args{"Remot +ePort"} : 10005; $self->{USERNAME} = $args{"Username"}; $self->{PASSWORD} = $args{"Password"}; $self->{REPLIES} = $replies; $self->{SOCKET} = IO::Socket::INET->new(PeerPort=>$self->{RE +MOTE_PORT}, Proto=>'udp', PeerAddr=>$self->{REMOTE_IP}); #$self->{REPLY_THREAD} = threads->new(\&_session_replies, $self->{ +REMOTE_IP}, $self->{REMOTE_PORT}, $replies, $self->{SOCKET}); $self->{REPLY_THREAD} = threads->create( sub { _session_replies($s +elf->{REMOTE_IP}, $self->{REMOTE_PORT}, $replies, $self->{SOCKET}) } +); printf "Connecting to %s on port %u (local port %u)...\n", $self-> +{REMOTE_IP}, $self->{REMOTE_PORT}, $self->{SOCKET}->sockport(); my $cmd = Telsis::Cmd_NewSession->new(); $cmd->interface_version(0x0207); $cmd->username($self->{USERNAME}); $cmd->password($self->{PASSWORD}); sleep 2; printf "Login as %s...\n", $self->{USERNAME}; $self->{SOCKET}->send($cmd->msg); sleep 2; bless ($self, $class); return $self; } sub DESTROY { my $self = shift; print "DESTROY 1\n"; sleep 2; print "DESTROY 2\n"; print "POP:".pop(@{$self->{REPLIES}})."\n"; sleep 2; print "DESTROY 3\n"; } sub _session_replies { my ($remote_ip, $remote_port, $replies, $sck) = @_; my $buf; my $flags = 0; sleep 1; print "IP:$remote_ip, PORT:$remote_port, REPLIES:".$replies.", SOC +KET:".$sck."\n"; while (@{$replies} != undef) { print "Waiting for replies...\n"; $sck->recv($buf, 256, $flags); if( $buf ) { print "RECV: " . unpack("H*", $buf) . "\n"; } else { sleep 1; } } print "THREAD TERMINATING\n"; } sub shelf { my $self = shift; if (@_) { $self->{SHELF} = shift } return $self->{SHELF}; } sub handle { my $self = shift; if (@_) { $self->{HANDLE} = shift } return $self->{HANDLE}; } sub seq { my $self = shift; if (@_) { $self->{SEQ} = shift } return $self->{SEQ}; } sub remote_ip { my $self = shift; if (@_) { $self->{REMOTE_IP} = shift } return $self->{REMOTE_IP} } sub remote_port { my $self = shift; if (@_) { $self->{REMOTE_PORT} = shift } return $self->{REMOTE_PORT} } 1;
In reply to Re^2: Threaded UDP Communication
by valhalla418
in thread Threaded UDP Communication
by valhalla418
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |