Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > two servers listening on same port ?

Reply
Thread Tools

two servers listening on same port ?

 
 
sc_wizard29@hotmail.com
Guest
Posts: n/a
 
      06-19-2006
hi everyone,

This ones makes me scratch my head : I'm using the following script on
Windows and it looks like I can run two instances of it
simultaneously...which means that I have 2 servers listening on the
same port.

#!/usr/bin/perl -w
use strict;
use Socket;
my $port = 8080;
my $proto = getprotobyname('tcp');
socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die
"setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!";
listen(Server,SOMAXCONN) || die "listen: $!";
print "Server : server started on port $port\n";
while(1) {}

I was expecting an error like "port already in use". Am I doing
something wrong ?

 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      06-20-2006

<> wrote in message
news: oups.com...
> hi everyone,
>
> This ones makes me scratch my head : I'm using the following script on
> Windows and it looks like I can run two instances of it
> simultaneously...which means that I have 2 servers listening on the
> same port.
>


I have just been bitten by the same thing on Windows 2000 - during the
tidying up of some server and client scripts. I started getting inconsistent
behaviour that really had me puzzled (and irate) ... until I realised I had
four instances of the server script running concurrently - all listening to
the same port.

Not sure of the "approved" method for determining whether a port is in use.
As an interim measure I'm doing the following in an attempt to make sure I
don't make the same mistake again:

my $server_port = '2004'; # or whatever
my $netstat = `netstat -an`;
if($netstat =~ /:$server_port\s/) {die "Port already in use"}
# Followed by code to create the server listening on
# port $server_port

Cheers,
Rob


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      06-20-2006
Sisyphus wrote:

>
> <> wrote in message
> news: oups.com...
>> hi everyone,
>>
>> This ones makes me scratch my head : I'm using the following script on
>> Windows and it looks like I can run two instances of it
>> simultaneously...which means that I have 2 servers listening on the
>> same port.


[...]

> Not sure of the "approved" method for determining whether a port is in
> use.


As filehandles, sockets are flock-able. An exclusive lock in the
server should do:

flock $server, LOCK_EX | LOCK_NB or die "port in use";

(Untested, I can't open the port another time in the first place.)

Anno
 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      06-20-2006

"Anno Siegel" <> wrote in message
..
..
>
> > Not sure of the "approved" method for determining whether a port is in
> > use.

>
> As filehandles, sockets are flock-able. An exclusive lock in the
> server should do:
>
> flock $server, LOCK_EX | LOCK_NB or die "port in use";
>


Also not sure about the use of flock() on Win32. When I insert the above
code into my server script (which runs with warnings pragma enabled) it dies
with:

Argument "LOCK_OZ" isn't numeric in flock at file_server.plx line 35.
port in use at file_server.plx line 35.

Line 35 is simply the line that implements the above code - the port is
definitely not in use.

From 'perldoc -f flock' I find:
"Produces a fatal error if used on a machine that doesn't implement
flock(2), fcntl(2) locking, or lockf(3)."

'perldoc perlport' reports that fcntl() is not implemented on Win32, though
flock() *is* implemented ... not sure what to make of all that.

Sorry if I'm being dumb.

Cheers,
Rob


 
Reply With Quote
 
Thomas Kratz
Guest
Posts: n/a
 
      06-20-2006
wrote:
> hi everyone,
>
> This ones makes me scratch my head : I'm using the following script on
> Windows and it looks like I can run two instances of it
> simultaneously...which means that I have 2 servers listening on the
> same port.
>
> #!/usr/bin/perl -w
> use strict;
> use Socket;
> my $port = 8080;
> my $proto = getprotobyname('tcp');
> socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
> setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die
> "setsockopt: $!";


Get rid of the SO_REUSEADDR. IIRC this will have the disadvantage that you
may have to wait a bit before you can reopen the socket after a close.

> bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!";
> listen(Server,SOMAXCONN) || die "listen: $!";
> print "Server : server started on port $port\n";
> while(1) {}
>
> I was expecting an error like "port already in use". Am I doing
> something wrong ?
>


You might find IO::Socket easier to use.

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e. r^.>l^..>k^.-
 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      06-20-2006

"Thomas Kratz" <> wrote in message
..
..
>
> You might find IO::Socket easier to use.
>


Yes - that's what *I* use - but it still permits multiple instances of
servers to be listening on the same port.

Seems to be a Win32 thing - on linux I find it's not possible to run
multiple instances of the same server script. I get the fatal error "Address
already in use".

Even with the latest CPAN release of IO, the behaviour persists on Win32.
Should this be submitted as a bug to
http://rt.cpan.org/Public/Dist/Display.html?Name=IO ?

Cheers,
Rob


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      06-20-2006
Sisyphus wrote:

>
> "Anno Siegel" <> wrote in message
> .
> .
>>
>> > Not sure of the "approved" method for determining whether a port is in
>> > use.

>>
>> As filehandles, sockets are flock-able. An exclusive lock in the
>> server should do:
>>
>> flock $server, LOCK_EX | LOCK_NB or die "port in use";
>>

>
> Also not sure about the use of flock() on Win32. When I insert the above
> code into my server script (which runs with warnings pragma enabled) it
> dies with:
>
> Argument "LOCK_OZ" isn't numeric in flock at file_server.plx line 35.
> port in use at file_server.plx line 35.


LOCK_OZ? Whazzat? Some windowy specialty?

> Line 35 is simply the line that implements the above code - the port is
> definitely not in use.
>
> From 'perldoc -f flock' I find:
> "Produces a fatal error if used on a machine that doesn't implement
> flock(2), fcntl(2) locking, or lockf(3)."
>
> 'perldoc perlport' reports that fcntl() is not implemented on Win32,
> though flock() *is* implemented ... not sure what to make of all that.


I have no personal experience with Windows, but I thought that file
locking worked.

Anno
 
Reply With Quote
 
sc_wizard29@hotmail.com
Guest
Posts: n/a
 
      06-20-2006
Looks like the use of netstat before the binding is the most "direct"
solution...

Here is the same script with IO::Socket (the problem is the same but
the script is shorter

#!/usr/bin/perl -w
use strict;
use IO::Socket;
my $port=8080;
my $server = IO::Socket::INET->new(Proto => 'tcp',
LocalPort => $port,
Listen => SOMAXCONN,
Reuse => 1);
die "can't setup server" unless $server;
print "Server : server started on port $port\n";
while(1) {}

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-20-2006

Quoth :
> Sisyphus wrote:
>
> >
> > <> wrote in message
> > news: oups.com...
> >> hi everyone,
> >>
> >> This ones makes me scratch my head : I'm using the following script on
> >> Windows and it looks like I can run two instances of it
> >> simultaneously...which means that I have 2 servers listening on the
> >> same port.

>
> [...]
>
> > Not sure of the "approved" method for determining whether a port is in
> > use.

>
> As filehandles, sockets are flock-able.


I *seriously* doubt if they are on Win32. Sockets on Win32 are not
ordinary filehandles, perl just fakes them up to look like it.

To the OP: remove the SO_REUSEADDR. Winsock is *COMPLETELY* broken;
among other things, SO_REUSEADDR doesn't mean 'allow another process to
bind again after the socket is closed but before the wait period' but
actually means 'bind to this port even if there is another process bound
there, at which point both sockets become non-deterministic' !!

See http://msdn.microsoft.com/library/de.../library/en-us
/winsock/winsock/using_so_reuseaddr_and_so_exclusiveaddruse.asp

(please excuse the wrapping).

Ben

--
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'

 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      06-20-2006

"Anno Siegel" <> wrote in message
..
..
>
> LOCK_OZ? Whazzat? Some windowy specialty?
>


or maybe an Aussie specialty

>
> I have no personal experience with Windows, but I thought that file
> locking worked.
>


Heh ... I had this notion that it worked on Win32 without even being
specifically invoked

Btw, I (inadvertently) omitted some relevant info from my reply - if I run
under strictures I get:

Bareword "LOCK_EX" not allowed while "strict subs" in use at file_server.plx
line 35.
Bareword "LOCK_NB" not allowed while "strict subs" in use at file_server.plx
line 35.

Without strictures the error is as I reported.

I tried not supplying a second argument, but perl insists that a second arg
be supplied. I'm not feeling very energetic about this at the moment ....
maybe later

Cheers,
Rob


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird issue, same code, same browser, two different apache servers,very different css bluebaron HTML 3 11-04-2009 07:13 PM
Start multiple listening servers in one file Thomas W Python 1 01-16-2009 05:59 PM
Need same viewstate for same page across multiple servers =?Utf-8?B?UHVuaXNoZXI=?= ASP .Net 1 03-23-2006 03:54 PM
Interruptions in live news listening Realone Player listening Thaqalain Computer Support 6 07-16-2005 02:11 PM
TCP servers in Python - two processes want to use same port Pif Paf Python 4 02-24-2004 12:46 PM



Advertisments