Client error while received: Connection refused
####
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket::INET;
my $port = 5000;
my $IP = "127.0.0.1";
# flush after every write
$| = 1;
my $server_socket = IO::Socket::INET->new(
#PeerAddr => inet_ntoa(INADDR_BROADCAST),
PeerPort => $port,
PeerAddr => $IP,
#LocalPort => $port,
Proto => 'udp',
Type => SOCK_DGRAM,
LocalAddr => 'localhost',
Broadcast => 1,
ReuseAddr => 1,
ReusePort => 1 ) or die "Can't bind: $@\n";
my $recieved_data;
printf("\nServer is up, listens on PORT: ".$port." waiting for client...\n");
while (1) {
# read operation on the socket
$server_socket->recv( $recieved_data , 1024 )
or die "Server error received: $!\n";
#get the peerhost and peerport at which the recent data received.
my $peer_address = $server_socket->peerhost();
my $peer_port = $server_socket->peerport();
print "\n($peer_address , $peer_port) said : $recieved_data";
#send the data to the client at which the read/write operations done recently.
my $data = "Echo: ".$recieved_data."";
$server_socket->send( $data )
or die "Server error send: $!\n";
}
$server_socket->close();
####
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket::INET;
my $port = 5000;
my $IP = "127.0.0.1";
# flush after every write
$| = 1;
my $client_socket = new IO::Socket::INET (
PeerHost => $IP,
Type => SOCK_DGRAM,
PeerPort => $port,
Proto => 'udp'
) or die "ERROR in Socket Creation: $@\n";
my $Peer_Port = $client_socket->peerport();
my $data_send = "Test!!!!!";
$client_socket->send( $data_send )
or die "Client error while sending: $!\n";
#read operation
$client_socket->recv( my $data_rcv , 1024 )
or die "Client error while received: $!\n";
print "Received data: $data_rcv\n";
$client_socket->close();