For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2007 > The role of listen()









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author The role of listen()
Monty

2007-07-18, 6:59 pm

In studying network programming (and I'm just beginning at it), I see
where listen() is called to set up a socket that waits for incoming
connection requests. The next step appears to be a call to accept()
where the request is granted and the connection made on a new socket,
as in this code snippet:

listen(SOCK,SOMAXCONN);

while(1) {
next unless my $remote_addr = accept(SESSION,SOCK);
...additional code here
}

Disregarding any limitations of this code, do I understand that
accept() spawns another socket with the file handle of SESSION?

I presume this allows SOCK to go back to listen()ing, and since
SESSION is already in use, another connection might not be possible
until SESSION is closed...am I getting ahead of mysefl?

Thanks

Tom Phoenix

2007-07-18, 6:59 pm

On 7/18/07, Monty <dale.schmitz@offutt.af.mil> wrote:

> In studying network programming (and I'm just beginning at it), I see
> where listen() is called to set up a socket that waits for incoming
> connection requests. The next step appears to be a call to accept()


Unless you really need to work at this low level, you're working too
hard. Check to see whether a module on CPAN already supports whatever
high-level protocol you're using.

http://search.cpan.org/

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training
Peter Scott

2007-07-19, 6:59 pm

On Wed, 18 Jul 2007 17:49:52 +0000, Monty wrote:
> Disregarding any limitations of this code, do I understand that
> accept() spawns another socket with the file handle of SESSION?


If you're studying things at this low level for academic purposes, I
recommend Lincoln Stein's "Network Programming With Perl". It'll explain
all that plus higher level programming which would be the more pragmatic
way of getting a task done. Starting to get a bit dated, but still the
best book around for this, IMHO.

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

Jeff Pang

2007-07-19, 6:59 pm


--- Peter Scott <Peter@PSDT.com> wrote:

> On Wed, 18 Jul 2007 17:49:52 +0000, Monty wrote:
> understand that
> handle of SESSION?
>
> If you're studying things at this low level for
> academic purposes, I
> recommend Lincoln Stein's "Network Programming With
> Perl".


Yes good book.I read it about 3 times,learned much
from it.:)



________________________________________
________________________________________
____
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469
John W. Krahn

2007-07-19, 6:59 pm

Peter Scott wrote:
> On Wed, 18 Jul 2007 17:49:52 +0000, Monty wrote:
>
> If you're studying things at this low level for academic purposes, I
> recommend Lincoln Stein's "Network Programming With Perl". It'll explain
> all that plus higher level programming which would be the more pragmatic
> way of getting a task done. Starting to get a bit dated, but still the
> best book around for this, IMHO.


If you really want to get into the low level details of network programming
then there is no better book than W. Richard Stevens' books:

http://www.kohala.com/start/unpv12e.html
http://www.kohala.com/start/unpv22e/unpv22e.html



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Nobull67@Gmail.Com

2007-07-19, 6:59 pm

On Jul 18, 6:49 pm, dale.schm...@offutt.af.mil (Monty) wrote:
> In studying network programming (and I'm just beginning at it), I see
> where listen() is called to set up a socket that waits for incoming
> connection requests. The next step appears to be a call to accept()
> where the request is granted and the connection made on a new socket,
> as in this code snippet:
>
> listen(SOCK,SOMAXCONN);
>
> while(1) {
> next unless my $remote_addr = accept(SESSION,SOCK);
> ...additional code here
>
> }
>
> Disregarding any limitations of this code, do I understand that
> accept() spawns another socket with the file handle of SESSION?


Yes that is correct.

> I presume this allows SOCK to go back to listen()ing, and since
> SESSION is already in use, another connection might not be possible
> until SESSION is closed...am I getting ahead of mysefl?


Right there are two things here. The first is the Unix-like socket
library listen(), socket() etc...

You seem to have got that bit OK. But now you can forget it - it's
useful to know but almost nobody ever uses it directly.

The limit of only supporting a single client is a limitation of doing
everything in a single process with blocking IO operations. (The fact
that SESSION is a global variable doesn't help).

If you want multiple concurrent clients you need either to fork() off
a child for each client (within "additional code here") or re-write
the whole thing with a more complex event loop with select().

There are numerous modules on CPAN (and even in standard Perl) to
provide warm fuzzy wrappers around all this low level socket stuff
IO::Socket and even to do the event loops for you.

Even if you are going to use the low-level functions I'd recommend
ditching the Perl4-style global handles.

listen( $SOCK,SOMAXCONN);

while(1) {
next unless my $remote_addr = accept(my $SESSION,SOCK);
...additional code here

}

Monty

2007-07-19, 6:59 pm

That's the book I got the code snippet from. I hadn't found (yet) the
explanation I'm looking for, but some testing has shown me that's
what's happening.

Thanks

Jeff Pang

2007-07-20, 3:59 am


--- "John W. Krahn" <krahnj@telus.net> wrote:

> Peter Scott wrote:
> understand that
> handle of SESSION?
> academic purposes, I
> With Perl". It'll explain
> be the more pragmatic
> dated, but still the
>
> If you really want to get into the low level details
> of network programming
> then there is no better book than W. Richard
> Stevens' books:
>
> http://www.kohala.com/start/unpv12e.html
> http://www.kohala.com/start/unpv22e/unpv22e.html
>


I once also read part of this book by RStevens (the
Chinese version).Fairly to say it's hard to understand
for by newbie and it's primarily facing to C/C++
programmers not Perl.



________________________________________
________________________________________
____
Sick sense of humor? Visit Yahoo! TV's
Comedy with an Edge to see what's on, when.
http://tv.yahoo.com/collections/222
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com