several sockets in the same process

L

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...
 
X

xhoster

Larry said:
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
 
L

Larry

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
 
L

Larry

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?
 
X

xhoster

Larry said:
#!/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
 
L

Larry

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top