[ Top page ]

« Simplified XML interface using Perl -- conversion of table-style data representation between hash and XML | Main | Purpose of this blog »

Network and communication

A program that "kicks" a UDP port using Perl

When connecting multiple computers to a network and making the computers communicate each other through the network, it is convenient to start the communication by kicking a port of the application on one of the computers. For this purpose, I wrote a program that sends a UDP (User Datagram Protocol) message that has no content. (It is easy to replace UDP by TCP, but the receiver program will be a little more complicated.)

It is an easy program, but it sometimes helps us (at least it helps me). If it is possible to start the program wrongly when the UDP packet has no content, a specific value can be included in the payload and the application can check the value. It may be convenient if the address and port can be given by command parameters, but they are assigned in the program here. (It is more convenient for me because I do not have to specify command parameters every time I use the program.)

#!/usr/bin/perl
##############################################################################
#
#	UDP port kicker
#
##############################################################################

use strict;
use Socket;

my $IP = '192.168.2.2';	# destination
my $REMOTE_PORT_RTP = 8000;
my $LOCAL_PORT_RTP = 8000;

socket(SOCK, AF_INET, SOCK_DGRAM, getprotobyname('udp')) ||
    die "socket(SOCK)$!\n";
setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) ||
    die "setsockopt()$!\n";
bind(SOCK, pack_sockaddr_in($LOCAL_PORT_RTP, INADDR_ANY)) ||
    die "bind(SOCK)$!\n";
open_socket($LOCAL_PORT_RTP);

send(SOCK, '', 0, pack_sockaddr_in($REMOTE_PORT_RTP, inet_aton($IP))) ||
    die "send()$!";

close(SOCK);

1;
Keywords:

TrackBack

TrackBack URL for this entry:
https://www.kanadas.com/mt/mt-tb.cgi/1647

Post a comment

About

This page contains a single entry from the blog posted on November 16, 2007 12:25 AM.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by Movable Type