在Perl中使用多播挂起发送和接收

时间:2012-09-28 18:54:19

标签: perl sockets udp multicast

我遇到一个问题,如果我没有收到广播的数据包,那么它就不会发送数据包了。我读了一些关于阻塞套接字的内容,但它似乎不适用于多播,我很确定这是问题所在,因为这是两个例程唯一使用的东西。

有更好的方法吗?关于这个问题的任何建议?

所有脚本都应该是应用程序处理多播支持的中间人,因此它只是通过多播侦听数据,然后将其发送到应用程序,来自应用程序的任何数据都会广播它。 这是我的代码:

#! usr/bin/perl -w

#----------------------------Includes-------------------------------------------
use IO::Socket;
use IO::Socket::Multicast;
use Threads;
require "./IPAddressHelperFunctions.pl";

#----------------------------Constants------------------------------------------
use constant MAX_PACKET_LENGTH => 1500; #Max Packet Length
use constant ADDRESS => '';             #IP address used for multicast (if not
                                        #valid or removed, it is auto-grabbed and
                                        #user is prompted if more than one)
use constant GROUP => '233.0.25.2';     #Multicast Group
use constant MPORT => 20000;            #Multicast Port
use constant INPORT => 28000;           #In to Application Port
use constant OUTPORT => 30000;          #Out from Application Port
use constant PROTO => 'udp';            #Protocol to use

#If no ip address is defined find it
my $ipAddress = ADDRESS;
if(!isIpAddress($ipAddress))
{
    $ipAddress = chooseIpAddress();
    if($ipAddress eq "")
    {
        my $temp = <>;
        exit 0;
    }
}

#----------------------------Socket Setup---------------------------------------
#Standard Socket to Communicate with Application
my $appS = IO::Socket::INET->new(Proto     => PROTO,
                                    PeerAddr  => 'localhost',
                                    PeerPort  => INPORT)
                                    or die "Can't bind app send: $@\n";

my $appL = IO::Socket::INET->new(Proto     => PROTO,
                                    LocalAddr => 'localhost',
                                    LocalPort => OUTPORT)
                                    or die "Can't bind app listen: $@\n";

# Socket to listen/send command packets on/to
my $bcastCP = IO::Socket::Multicast->new(Proto       => PROTO,
                                         LocalAddr   => $ipAddress,
                                         LocalPort   => MPORT,
                                         ReuseAddr   => 1,
                                         PeerPort    => MPORT)
                                         or die "Can't bind bcastCP: $@\n";

# set our mutlicast address to listen on
$bcastCP->mcast_add(GROUP,$ipAddress) || die "Couldn't set group: $!\n";

# Turn on loopbacking (had bug when it was off)
$bcastCP->mcast_loopback(1);

#----------------------------Send/Recieve Data----------------------------------
#Start a thread to listen for app data and send it to the network
my $appThread = threads->create('getFromApp');
$appThread->detach();

#loop to listen for network data and send it to app
while(1)
{
     my $InCPdata;
     $bcastCP->recv($InCPdata, MAX_PACKET_LENGTH,0);

     #check the data and ready it for app then send it to app
     if($InCPdata)
     {
          $appS->send("CPB".$InCPdata);
          print("Packet sent to app\n");
     }
}

#----------------------------Close Sockets--------------------------------------
$appL->close();
$appS->close();
$bcastCP->close();

#----------------------------Subroutines--------------------------------------
#----------------------------------------------------
# Name: get From App
# Written By: Nathan Bowhay
#
# Args:
# None
#
# Description:
# Listens for data coming out of application and broadcast it to the network
# using multicast
#-----------------------------------------------------
sub getFromApp{

     while(1)
     {
          my $OutCPdata;
          $appL->recv($OutCPdata, MAX_PACKET_LENGTH,0);
          if($OutCPdata)
          {
               $bcastCP->mcast_send($OutCPdata, GROUP.':'.MPORT);
               print("Packet send OUT\n");
          }
     }

     return 1;
}

PS:这段代码被修改了一下,试图隐藏一些东西。另外,如果您需要查看我用来获取IP地址的脚本,请告诉我,但这很简单,它只是使用ipconfig来获取机器的本地IP地址。

0 个答案:

没有答案