如何在 Perl 中测试 TCP 套接字状态? [英] How can I test TCP socket status in Perl?

查看:65
本文介绍了如何在 Perl 中测试 TCP 套接字状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个读取数据的 TCP 套接字.当读取数据时发生错误时,我返回一个 undef (NULL) 值.错误可能是由格式错误的消息或损坏的套接字引起的.有人能告诉我是否有返回套接字状态的特定函数吗?

I've got a TCP socket which reads data. When an error occurs when reading the data, I return an undef (NULL) value. Errors can be caused by badly formatted messages or broken sockets. Can someone tell me if there is a specific function which returns the status of a socket?

推荐答案

doh!,回想起来答案很明显,使用连接的呼叫.

doh!, the answer was obvious in retrospect, use the connected call.

$socket = IO::Socket::INET(localhost, 1000);
die "no connection" unless $socket -> connected();
$socket -> send("your face here for $20");
die "socket is dead" unless $socket -> connected();
$socket -> recv($data);

这篇关于如何在 Perl 中测试 TCP 套接字状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆