use IO::Socket::IP ??? how to make a client in perl Ipv6

一曲冷凌霜 提交于 2019-12-11 20:36:22

问题


I have been trying to find a simple client ipv6 script that would work with Evens server script , of course I dont know what Im doing, so all I can do is rewrite someone else's work until I know what Im doing ...

so here is a server script that works on Microsoft widows server

   use IO::Socket::IP -register; 
   my $sock = IO::Socket->new(
   Domain    => PF_INET6,
   LocalHost => "::1",
   Listen    => 1,
   ) or die "Cannot create socket - $@\n";

   print "Created a socket of type " . ref($sock) . "\n";

   {
   $in = <STDIN>;
   print $in->$sock;
   redo }

of course the $in->$sock is not working, cause I dont know how to send data using just $sock ??? so I need to know how to send information properly and
what I need is A client script to connect to the above script using the ipv6 protocol

can anyone help with this ??? I would like to be able to send information from one perl program to another perl program using this being able to send information back and forth would be Ideal ...

Thanks in advance -Mark


回答1:


That's a server socket (Listen => 1), so you have to accept a connection.

use IO::Socket::IP -register;

my $listen_sock = IO::Socket::IP->new(
   LocalHost => "::1",                    # bind()
   Listen    => 1,                        # listen()
) or die "Cannot create socket - $@\n";

print("Listening to ".$listen_sock->sockhost()." "
                     .$listen_sock->sockport()."\n");

while (1) {
   my $sock = $listen_sock->accept()
      or die $!;

   print("Connection received from ".$sock->peerhost()." "
                                    .$sock->peerport()."\n");

   while (<$sock>) {
      print $sock "echo: $_";
   }
}

A client:

use IO::Socket::IP -register;

@ARGV == 2 or die("usage");
my ($host, $port) = @ARGV;

my $sock = IO::Socket::IP->new(
   PeerHost => $host,                     # \ bind()
   PeerPort => $port,                     # /
) or die "Cannot create socket - $@\n";

print $sock "Hello, world!\n";
$sock->shutdown(1);                       # Done writing.
print while <$sock>;

The comments indicate the underlying system call used to perform the action.



来源:https://stackoverflow.com/questions/16908163/use-iosocketip-how-to-make-a-client-in-perl-ipv6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!