Front page | perl.beginners |
Postings from April 2021
socket recive problem
Thread Next
From:
stefano cerbioni
Date:
April 2, 2021 08:47
Subject:
socket recive problem
Message ID:
CAAwR5B81nkBTwZkdrXnfhsjfLd9zRAH4E8=Q=yogQugcN2MixQ@mail.gmail.com
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;
}
Thread Next
-
socket recive problem
by stefano cerbioni