I have the following code:
use IO::Socket::INET;
use Sys::Hostname;
use Socket;
my($addr)=inet_ntoa((gethostbyname(hostname))[4]);
my $port_to_use = 7777;
my $socket = new IO::Socket::INET (
LocalHost => $addr,
LocalPort => $port_to_use,
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
my $client_socket = $socket->accept();
if I run this on one screen and run another on another screen, I get an error message:
cannot create socket Address already in use
instead of dying, I would like to try using a different port (increment by 1) until I find the one that will be used.
I tried converting the string diewith eval, but im couldn't catch it
any suggestions?
source
share