several sockets in the same process
several sockets in the same process
am 22.08.2007 03:13:43 von Larry
hi folks,
I'd like to get 10 sockets listening on different ports in the same
process at the same time...can it actually be done?
here's a chunck of code:
#!/perl
use strict;
use warnings;
use Socket;
$| = 1;
my ($usock, $uport);
while (1)
{
($usock,$uport) = &udp_setup("127.0.0.1");
print $uport;
my $buff;
my $lbuff = 1024;
#while ( recv($usock, $buff, $lbuff, 0) )
#{
# print $buff;
#}
}
sub udp_setup
{
my $HOSTNAME = $_[0];
my $fh;
socket($fh, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or return "";
binmode($fh);
my $ipaddr = inet_aton($HOSTNAME);
my $portaddr = sockaddr_in(0, $ipaddr);
bind($fh, $portaddr) or return "";
my ($nport, $naddr);
($nport, $naddr) = sockaddr_in(getsockname($fh));
return ($fh,$nport);
}
yet, the while {} block is blocking...
Re: several sockets in the same process
am 22.08.2007 03:24:34 von xhoster
Larry wrote:
> hi folks,
>
> I'd like to get 10 sockets listening on different ports in the same
> process at the same time...can it actually be done?
Sure. And if you want to read from them as they become readable,
you will want to use select or IO::Select.
perldoc IO::Select
perldoc -f select
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: several sockets in the same process
am 22.08.2007 03:41:25 von Larry
In article <20070821212437.798$Pe@newsreader.com>, xhoster@gmail.com
wrote:
> Sure. And if you want to read from them as they become readable,
> you will want to use select or IO::Select.
could you provide me with some chunk of code please? by the way i've
tried the following , yet it's a total mess:
#!/perl
use strict;
use warnings;
use threads;
use Socket;
$| = 1;
while (1)
{
threads->create ( \&manage, undef )->detach;
}
sub manage
{
my ($usock, $uport);
($usock,$uport) = &udp_setup("127.0.0.1");
#print $uport;
#print "\n";
my $buff;
my $lbuff = 1024;
while ( recv($usock, $buff, $lbuff, 0) )
{
print $buff;
}
}
sub udp_setup
{
my $HOSTNAME = $_[0];
my $fh;
socket($fh, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or return "";
binmode($fh);
my $ipaddr = inet_aton($HOSTNAME);
my $portaddr = sockaddr_in(0, $ipaddr);
bind($fh, $portaddr) or return "";
my ($nport, $naddr);
($nport, $naddr) = sockaddr_in(getsockname($fh));
return ($fh,$nport);
}
I get this error: recv() on unopened socket at ff.pl line 30.
Bus error
Re: several sockets in the same process
am 22.08.2007 04:01:55 von Larry
In article <20070821212437.798$Pe@newsreader.com>, xhoster@gmail.com
wrote:
> perldoc IO::Select
> perldoc -f select
>
> Xho
ok, I've coded this:
#!/perl
use strict;
use warnings;
use Socket;
use IO::Select;
$| = 1;
my $s = IO::Select->new();
my ($usock, $uport);
($usock,$uport) = &udp_setup("127.0.0.1");
print $uport;
$s->add($usock);
my @ready = $s->can_read();
sub udp_setup
{
my $HOSTNAME = $_[0];
my $fh;
socket($fh, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or return "";
binmode($fh);
my $ipaddr = inet_aton($HOSTNAME);
my $portaddr = sockaddr_in(0, $ipaddr);
bind($fh, $portaddr) or return "";
my ($nport, $naddr);
($nport, $naddr) = sockaddr_in(getsockname($fh));
return ($fh,$nport);
}
how can i handle the incoming request?
Re: several sockets in the same process
am 22.08.2007 04:24:11 von xhoster
Larry wrote:
>
> #!/perl
>
> use strict;
> use warnings;
> use Socket;
> use IO::Select;
>
> $| = 1;
>
> my $s = IO::Select->new();
>
> my ($usock, $uport);
>
> ($usock,$uport) = &udp_setup("127.0.0.1");
>
> print $uport;
>
> $s->add($usock);
>
> my @ready = $s->can_read();
....
> how can i handle the incoming request?
I don't know what you are trying to accomplish, so I don't know how
to accomplish it. I assumed you already knew what you wanted to do
and how to do it with one socket at a time and only wanted to expand
it to more than one socket.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: several sockets in the same process
am 22.08.2007 04:34:09 von Larry
In article <20070821222414.765$Wa@newsreader.com>, xhoster@gmail.com
wrote:
> I don't know what you are trying to accomplish, so I don't know how
> to accomplish it. I assumed you already knew what you wanted to do
> and how to do it with one socket at a time and only wanted to expand
> it to more than one socket.
I'm sorry ... I coulda done with telling you what I've been trying to
do. I want port numbers from 45000 to 65535 to be busy... and I want
this to work background, so that p2p apps can't be launched as there's
no free port to bind to...am I crazy?
thanks ever so much
Re: several sockets in the same process
am 22.08.2007 10:55:41 von dformosa
On Wed, 22 Aug 2007 04:34:09 +0200, Larry wrote:
[...]
> I'm sorry ... I coulda done with telling you what I've been trying to
> do. I want port numbers from 45000 to 65535 to be busy... and I want
> this to work background, so that p2p apps can't be launched as there's
> no free port to bind to...am I crazy?
A better approach would be to use a firewall to prevent packets in
that range from being used.