chr(1) + message type + data #### my $poll = 0; my $messages = 0; # make the socket my $socket = IO::Socket::INET->new( LocalHost => '', LocalPort => 1066, Proto => 'tcp', Listen => 5, Reuse => 1, ) or die "Socket could not be created : $!"; # accept connections while (my $new_sock = $socket->accept()) { while (1) { say "Waiting!"; my $buf=""; # loop until start of message control (chr(1)) while (read($new_sock,$buf,1)) { last if $buf eq chr(1);} read($new_sock,$buf,1); if ($buf eq "0") { # handle the POLL message # byte 1 = ASCII start of header (1) # byte 2 = '0' - poll message ID # byte 3 = number of messages since last poll - 4 bytes # following don't seem to be sent. # byte 7 = ASCII CR (13) # byte 8 = ASCII LF (10) # already have initial bytes now get payload read($new_sock,$poll,4); say "POLL : $poll"; SendPollReply($new_sock); SendSNMP("0","0","--",undef,undef); } else { # Other messages look like they are Cr/LF terminated $buf .= <$new_sock>; $messages++; say "Buffer : $buf Poll : $poll Messages : $messages"; # only send traps for message group "A" or "C" if ($buf =~ /([AC])(.{8})((\+\+|--))(\d{8})(.{10})/msx) { my $message_group = $1; my $station_code = $2; my $change_type = $3; my $date_time = $5; my $point_name = $6; SendSNMP($message_group,$station_code,$change_type,$date_time,$point_name); } } } } close($socket); sub SendPollReply { my $new_sock = shift; # send the poll reply # byte 1 = ASCII start of header chr(1) # byte 2 = '0' - response message ID # byte 3 = number of messages since last poll - 4 bytes # byte 7 = Error status '0' if OK # byte 8 = ASCII CR (13) # byte 9 = ASCII LF (10) SendMessage( $new_sock,"0".sprintf("%04d",$messages)."0"); $messages=0; } # wrap message back to poller in required start/end characters sub SendMessage { my ($new_sock,$message) = @_; $message = chr(1).$message.chr(13).chr(10); say "REPLY : $message"; print $new_sock $message; }