在 Perl 中通过套接字发送多个有效负载
编辑:问题出在 IIS 上,而不是我正在使用的 Perl 代码上。其他人在这里谈论同样的问题: https://stackoverflow.com/a/491445/1179075
很长时间读者在这里,第一次发帖。
因此,我正在 Perl 中处理一些现有代码,这些代码执行以下操作:
- 创建套接字
- 发送一些数据
- 关闭套接字
- 循环回到1,直到发送完所有数据
为了避免一直创建和关闭套接字的开销,我决定这样做:
- 创建套接字
- 发送一些数据
- 循环回到2,直到所有数据发送完毕
- 关闭套接字
事实是,仅发送第一个有效负载 - 所有后续有效负载都将被忽略。我正在将此数据发送到 .NET Web 服务,但 IIS 根本不接收该数据。不知何故,套接字被关闭,我不知道为什么。
这是我用来测试新更改的脚本:
use IO::Socket;
my $sock = new IO::Socket::INET(PeerAddr => $hostname, PeerPort => 80, Proto => "tcp", Timeout => "1000") || die "Failure: $! ";
while(1){
my $sent = $sock->send($basic_http_ping_message);
print "$sent\n";
sleep(1);
}
close($sock);
所以这不起作用 - IIS 只接收第一个 ping。但是,如果我将 $sock
的分配移动并关闭到循环中,则 IIS 会正确接收每个 ping。
我是否只是在这里错误地使用了套接字,或者 IIS 中是否有一些我需要更改的神秘设置?
谢谢!
Edit: the problem is with IIS, not with the Perl code I'm using. Someone else was talking about the same problem here: https://stackoverflow.com/a/491445/1179075
Long-time reader here, first time posting.
So I'm working on some existing code in Perl that does the following:
- Create socket
- Send some data
- Close socket
- Loop back to 1 until all data is sent
To avoid the overhead of creating and closing sockets all the time, I decided to do this:
- Create socket
- Send some data
- Loop back to 2 until all data is sent
- Close socket
The thing is, only the first payload is being sent - all subsequent ones are ignored. I'm sending this data to a .NET web service, and IIS isn't receiving the data at all. Somehow the socket is being closed, and I have no further clue why.
Here's the script I'm using to test my new changes:
use IO::Socket;
my $sock = new IO::Socket::INET(PeerAddr => $hostname, PeerPort => 80, Proto => "tcp", Timeout => "1000") || die "Failure: $! ";
while(1){
my $sent = $sock->send($basic_http_ping_message);
print "$sent\n";
sleep(1);
}
close($sock);
So this doesn't work - IIS only receives the very first ping. If I move $sock
's assignment and closing into the loop, however, IIS correctly receives every single ping.
Am I just using sockets incorrectly here, or is there some arcane setting in IIS that I need to change?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的问题是缓冲。关闭套接字上的缓冲,或在每次写入后刷新它(关闭套接字具有刷新它的副作用)。
I think your problem is buffering. Turn off buffering on the socket, or flush it after each write (closing the socket has the side-effect of flushing it).
你得到什么输出?
send()
之后有一个打印,如果send()
失败,它将返回undef
。您可以打印出如下错误:我自己的猜测是您要连接的服务正在关闭连接,这就是为什么只有一个连接能够通过,也是为什么每次创建一个新连接都会起作用。
What output are you getting? You have a print after the
send()
, ifsend()
fails, it will returnundef
. You can print out the error like:My own guess is that the service that you're connecting to is closing the connection, which is why only one gets through, and also why creating a new connection each time would work.