在 Perl 中通过套接字发送多个有效负载

发布于 2024-12-29 22:23:03 字数 1081 浏览 1 评论 0原文

编辑:问题出在 IIS 上,而不是我正在使用的 Perl 代码上。其他人在这里谈论同样的问题: https://stackoverflow.com/a/491445/1179075

很长时间读者在这里,第一次发帖。

因此,我正在 Perl 中处理一些现有代码,这些代码执行以下操作:

  1. 创建套接字
  2. 发送一些数据
  3. 关闭套接字
  4. 循环回到1,直到发送完所有数据

为了避免一直创建和关闭套接字的开销,我决定这样做:

  1. 创建套接字
  2. 发送一些数据
  3. 循环回到2,直到所有数据发送完毕
  4. 关闭套接字

事实是,仅发送第一个有效负载 - 所有后续有效负载都将被忽略。我正在将此数据发送到 .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:

  1. Create socket
  2. Send some data
  3. Close socket
  4. 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:

  1. Create socket
  2. Send some data
  3. Loop back to 2 until all data is sent
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青巷忧颜 2025-01-05 22:23:03

我认为你的问题是缓冲。关闭套接字上的缓冲,或在每次写入后刷新它(关闭套接字具有刷新它的副作用)。

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).

小瓶盖 2025-01-05 22:23:03

你得到什么输出? send() 之后有一个打印,如果 send() 失败,它将返回 undef。您可以打印出如下错误:

     my $sent = $sock->send($msg);
     die "Failed send: $!\n" unless defined $sent;
     print "Sent $sent bytes\n";

我自己的猜测是您要连接的服务正在关闭连接,这就是为什么只有一个连接能够通过,也是为什么每次创建一个新连接都会起作用。

What output are you getting? You have a print after the send(), if send() fails, it will return undef. You can print out the error like:

     my $sent = $sock->send($msg);
     die "Failed send: $!\n" unless defined $sent;
     print "Sent $sent bytes\n";

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文