Hi,

Refer here
https://perldoc.perl.org/perlipc#Sockets%3A-Client%2FServer-Communication
use
my($port, $iaddr) = sockaddr_in($client_addr);
then
gethostbyaddr($iaddr, AF_INET )

Regards,
Heince

On Fri, Apr 2, 2021 at 3:48 PM stefano cerbioni <stf...@gmail.com> wrote:

> hi i try to recive  a stream string  by  a client (write in C++) , if i
> use netcat ,  work ok but  when i use a  script in perl work partially ,
> recive "Connection recieved from $name\n";,  but  $name is  blank , why ??
> thanks
> this is a script
> use strict;
> use Socket;
>
> # use port 7890 as default
> my $port = shift || 23456;
> my $proto = getprotobyname('tcp');
> my $server = "localhost";  # Host IP running the server
>
> # create a socket, make it reusable
> socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
>    or die "Can't open socket $!\n";
> setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1)
>    or die "Can't set socket option to SO_REUSEADDR $!\n";
>
> # bind to a port, then listen
> bind( SOCKET, pack_sockaddr_in($port, inet_aton($server)))
>    or die "Can't bind to port $port! \n";
>
> listen(SOCKET, 5) or die "listen: $!";
> print "SERVER started on port $port\n";
>
> # accepting a connection
> my $client_addr;
> while ($client_addr = accept(NEW_SOCKET, SOCKET)) {
>    # send them a message, close connection
>    my $name = gethostbyaddr($client_addr, AF_INET );
>    print NEW_SOCKET "Smile from the server";
>    print "Connection recieved from $name\n";
>    close NEW_SOCKET;
> }
>

Reply via email to