OlegG has asked for the wisdom of the Perl Monks concerning the following question:
Now I want to connect socket, but with my own function, because this function should do some more than simple connection.my $sock = IO::Socket::INET->new(Proto => 'tcp');
Fictional module IO::Socket::WhatEver returns its own typeglob which can be used as IO::Socket::INET object or tcp socket. And now I need to do something to embed $newsock into $sock to use $sock after myconnect() call as properly connected socket. Is there a way to do something like this?sub myconnect { my ($sock, $addr) = @_; my $newsock = IO::Socket::WhatEver->new($addr); # ... # and what to do next? }
It works with simple socketuse Socket; BEGIN { *CORE::GLOBAL::connect = sub(*$) { my ($socket, $name) = @_; return CORE::connect($socket, $name) if (scalar(caller 2) eq ' +IO::Socket::Socks'); my ($port, $host) = sockaddr_in($name); $host = inet_ntoa($host); $_[0] = IO::Socket::Socks->new( ProxyAddr => 'localhost', ProxyPort => 1080, SocksVersion => 5, ConnectAddr => $host, ConnectPort => $port, SocksDebug => 1 ) or return undef; return 1; } } use IO::Socket::Socks;
But doesn't work with IO::Socket::INET:my $remote = "perlmonks.org"; my $port = 80; my $iaddr = inet_aton($remote) || die "no host: $remote"; my $paddr = sockaddr_in($port, $iaddr); my $proto = getprotobyname('tcp'); socket(my $sock, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; connect($sock, $paddr); syswrite($sock, "GET / HTTP/1.0\r\n"); close $sock;
The last example raises SIGPIPE, because $sock remains closed IO::Socket::INET object.my $sock = IO::Socket::INET->new('perlmonks.org:80') or die $@; $sock->syswrite("GET / HTTP/1.0\r\n"); $sock->close();
|
|---|