Kiran
Kiran

Reputation: 8518

Socket Programming in Perl

I am new to Socket Programming. Here is what I am trying to do:

I would like to transmit a Packet to another Server in the network over an UDP Connection.

Here is the short code snippet I wrote in Perl:

# create udp socket
$sock = IO::Socket::INET->new(
  Proto       => "udp",
  PeerPort    => '5001',
  PeerAddr    => defined $ip ? $ip : '10.42.43.2'
) or die "Socket could not be created: $!\n";

I would like to handle the exception when the host (10.42.43.2) is not reachable. Any ideas how it can be done ?

Thanks you so much

Upvotes: 2

Views: 367

Answers (1)

ikegami
ikegami

Reputation: 385590

UDP is an unreliable protocol, which means it doesn't provide a means of finding out if delivery was successful or not.

One option is to switch to a reliable protocol like TCP.

Another is to handle delivery notifications yourself. Have the host send a reply on receipt of your packet. If no reply has been received within X seconds, an error occurred.

Upvotes: 7

Related Questions