perl socket客户端无法识别消息的结束

时间:2017-12-01 08:00:49

标签: perl sockets while-loop

我正在运行一个查询服务器的简单perl套接字客户端,然后在遇到关键字或短语时尝试关闭套接字。

...
local $\ = "\x{0d}";
while($line = <$sock>){
  print $line."\n";
  last if ($line =~ /C6/);
}
...
close($sock);

我很高兴终止于(0x0d)或&#34; C6&#34; string - 它们都终止消息。我使用Wireshark监视它,并且两个触发都发生在消息的末尾,但是我无法打破while循环,无论是中断还是最后一次,也不会打印$ line。 / p>

想法? TIA

2 个答案:

答案 0 :(得分:1)

当您收到C6而没有收到回车(或EOF)时,您不会退出,因为您的代码始终等待回车(或EOF)。修正:

# sysread returns as soon as data is available, so this is a just a maximum.
use constant BLOCK_SIZE => 4*1024*1024;

my $buf = '';
while (1) {
   my $rv = sysread($sock, $buf, length($buf), 4*1024*1024);
   die($!) if !defined($rv);
   last if !$rv;

   process_message($1)
      while $buf =~ s/^( (?: [^C\x0D] | C (?=[^6]) )*+ (?: C6 | \x0D ) )//xs;
}

die("Premature EOF") if length($buf);

答案 1 :(得分:0)

我认为你问题的根源在于你设置了<StackLayout> <ScrollView orientation="vertical" #scrollR> <StackLayout> <image src="~/images/scorelogo.png" id="score-logo" stretch="fill" horizontalAlignment="center" class="score-logo" > </image> <image src="~/images/score.png" id="score-text" stretch="fill" horizontalAlignment="center" class="score-image" > </image> <StackLayout class="inputsGroup"> <TextField hint="Username" keyboardType="email" autocorrect="false" autocapitalizationType="none" style.cursorColor="rgb(255,255,255)" (focus)="openField()" ></TextField> <StackLayout orientation="horizontal" width="100%"> <TextField width="80%" hint="Password" [secure]="passShow" secure="true" #passR (focus)="openField()" ></TextField> <Button [text]="passText" class="hide-show" (tap)="passToogle()"></Button> </StackLayout> <StackLayout orientation="horizontal" width="100%" > <TextField hint="Pin" keyboardType="number" [secure]="pinShow" width="80%" #pinR (focus)="openField()" ></TextField> <Button [text]="pinText" class="hide-show" (tap)="pinToogle()"></Button> </StackLayout> <StackLayout> <CheckBox text="Remember Me" checked="true" fillColor="rgb(255,255,255)" style="color:white;font-size:18px" > </CheckBox> <CheckBox text="Set Up Fingerprint" checked="false" fillColor="rgb(255,255,255)" style="color:white;font-size:18px" > </CheckBox> </StackLayout> </StackLayout> <Button text="Log in" class="button-login"></Button> <Button text="Need help logging in?" class="button-need-help"></Button> </StackLayout> </ScrollView> <StackLayout> <image src="~/images/fingerprint.png" id="score-fingerprint" stretch="none" horizontalAlignment="center" > </image> <Button text="Log In With Fingerprint" class="button-need- help"></Button> </StackLayout> </StackLayout> ,这是输出记录分隔符,而不是$\,它是输入记录分隔符。

因此,在将$/移交到循环的其余部分之前,您的while正在等待\n

但如果失败了,那么套接字上还存在缓冲和自动刷新的问题。

而且......当你说你正在使用wireshark进行监控时,你有多确定这些值是数据包内容有效负载的一部分而不是数据包的一部分?实际上,您是否在任何时候从服务器发送$line作为数据包的一部分?

相关问题