in reply to Re: flushing sockets
in thread flushing sockets

ok guys, I thought flushing the buffer would help, apparently not. I'm using select as a sort of timeout for data on an incomming socket. The scenario goes like this. I've got a socket waiting for data, if it's there enterpret it, if however, no data has been recieved within 30 seconds, check the status of the line with a pseudo ping to make sure the host on the other side is listening. Here's the problem, when the aplication starts, it never times out, ONLY when I press return on the kb does it start to time out. I'm thinking it's because for SOME reason, the socket wasn't updated and it's still waiting for data on it, wich wasn't the case.
my $selectFD; $selectFD = new IO::Select($rec[$RXThreadNumber], undef, undef, 1); $|++; # flush the buffer apparently... while(@bits = $selectFD->can_read) { select(undef, undef, undef, 0.1); foreach $fdThread (@bits) { if($fdThread == $rec[$RXThreadNumber]) { print("Fetching pdu\n"); # the following if is to make sure that the only time we try + fetch an sms # is when there is actual sms activity on the socket... and +nothing else... # this was causing problems before because a sock kill init +was creating# noise on the line and the aplication was trying to ent +erperet this as # a PDU... if($pdu = $rec[$RXThreadNumber]->read_pdu()) { print("CMD: $pdu->{cmd}\n"); print("STATUS: $pdu->{status}\n"); print("SERTYPE: $pdu->{service_type}\n"); print("SEQ: $pdu->{seq}\n"); print("SOURCE: $pdu->{source_addr}\n"); print("DESTINATION: $pdu->{destination_addr}\n"); #print("DATA: $pdu->{data}\n"); print("MESSAGE: $pdu->{short_message}\n"); $test = $rec[$RXThreadNumber]->deliver_sm_resp(seq=>$pd +u->{seq}, message_id=>''); my $nowRXTime = genTime(); my $queryRX = $tdbh->prepare("insert into MOHits values +(NULL, NULL, \"$pdu->{source_addr}\", \"$nowRXTime\", \"$pdu->{short_ +message}\", 0, \"$pdu->{destination_addr}\")"); $queryRX->execute; print("Fetched pdu\n"); } } if((time() - $startTime) > 5) { $startTime = time(); print("Enquiring link...\n"); if($rec[$RXThreadNumber]->enquire_link()) { print("Link is fine...\n"); } else { print("Link is DOWN! Bringing up again...\n"); $rec[$RXThreadNumber] = Net::SMPP-new_receiver( $tshost, smpp_version => $smppVer, system_id => $tssysid, password => $tspassword, system_type => $tssystype, dest_addr_ton => 0x91, # Type Of Number, +this means its "2783XXXXXXX" dest_addr_npi => 0x00, addr_ton => 0x91, addr_npi => 0x00, source_addr_ton => 0x91, source_addr_npi => 0x00, port => $tsport ) or warn("can't connect to smsc: $!\n"); } } } }
The timeout ONLY fires when I press return on the kb once the aplication starts.... any idea's? Thanks Scuzz. (;

Replies are listed 'Best First'.
Re: Re: Re: flushing sockets
by Thelonius (Priest) on Feb 17, 2003 at 14:59 UTC
    This line waits forever for input:
    while(@bits = $selectFD->can_read)
    and then this line pauses for a tenth of second for no apparent reason:
    select(undef, undef, undef, 0.1);
    What you probably want to do is:
    @bits = $selectFD->can_read(30); if (@bits == 0) { # we timed out } else { for (@bits) { blah blah blah } }
      As it is a good setup for timoutes, the problem being that if the sockets drops for some reason, the timouts cease, wich means that it can't re-establish the link, wich is the whole point of the timeout... Any other idea's?
Re: Re: Re: flushing sockets
by Tomte (Priest) on Feb 17, 2003 at 10:48 UTC

    I don't know nothing about thread-programming in perl, but using fork I used a scheme like the folling:

    eval { local $SIG{ALRM} = sub { die "Timed Out!\n" }; my $timeout = 30; # give 30 seconds to accept and process data my $previous_alarm = alarm($timeout); # get input # and do heavy and interesting and cool stuff :-) # ... # finished with this alarm($previous_alarm) }; # eval if ($@ =~ /timed out/i) { # check stuff, report error, etc }

    hth

    regards,
    tomte


    Edit:Fixed indentation.