pego has asked for the wisdom of the Perl Monks concerning the following question:

hello, i'm trying to make a server but if i brows to the server the only thing the program prints is the fist hello1. The other prints are not visible. Does anyone know what i have to do to make it work?
$queuelength=1; use Socket; socket(HANDLE, PF_INET, SOCK_STREAM, getprotobyname('tcp')); $addr = sockaddr_in(80, INADDR_ANY); bind(HANDLE, $addr); listen(HANDLE,$queuelength); while(true) { print 'hallo1'; accept(NEW_HANDLE, HANDLE); select(NEW_HANDLE); $line=<NEW_HANDLE>; print $line; print 'hello2'; }

janitored by ybiC: Retitle from uninformative "server"

Replies are listed 'Best First'.
Re: Perl socket server problem
by Thelonius (Priest) on Oct 11, 2003 at 10:39 UTC
    Well, if you open up a second browser window and use it to also browse to your server program, then the first browser window will show something like:
    GET / HTTP/1.1
    hello2hallo1
    
    What's happening is that when you do select(NEW_HANDLE), then all further print statements (that don't have file handles) will print to the socket NEW_HANDLE, so of course they won't show up on the standard output of the server process. The first browswer will wait (a long time) for the socket to close before it prints anything. If you open up a second browser window, then accept() returns a second time and the old NEW_HANDLE will be closed and the first browser will show its output.

    Now, on more general terms, I hope you're doing this only to learn about Perl and/or sockets and/or HTTP at a very low level. If you are just trying to write a server, there are modules for that which handle things for you.

    In any case, you should always enable warnings, use strict; and check the return codes of all your socket calls. Here is a sort of minimal HTTP server which may give you an idea of what's going on:

    #!perl -w use Socket; use strict; $| = 1; my $queuelength=1; socket(HANDLE, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die "socket call failed: $!\n"; my $addr = sockaddr_in(80, INADDR_ANY) or die "sockaddr_in: $!\n"; bind(HANDLE, $addr) or die "bind: $!\n"; listen(HANDLE,$queuelength); while(1) { print "hallo1\n"; accept(NEW_HANDLE, HANDLE); my $line; while (defined ($line = <NEW_HANDLE>) && $line ne "\r\n") { print $line; } print NEW_HANDLE "HTTP/1.0 200 OK\r\n"; print NEW_HANDLE "Content-type: text/plain\r\n\r\n"; print NEW_HANDLE "Howdy!\r\n"; close(NEW_HANDLE); print "hello2\n"; }
    Even if you are interested in working at a very low level, it's still handier to use IO::Socket rather than the naked function calls. E.g. here's the same as the above program:
    #!perl -w use IO::Socket; use strict; $| = 1; my $queuelength=1; my $listensock = IO::Socket::INET->new( Proto => 'tcp', LocalPort => 80, Listen => $queuelength, ReuseAddr => 1, ) or die "new listen socket: $!\n"; while(my $newsocket = $listensock->accept) { my $line; print "hello1\n"; while (defined ($line = <$newsocket>) && $line ne "\r\n") { print $line; } print $newsocket "HTTP/1.0 200 OK\r\n"; print $newsocket "Content-type: text/plain\r\n\r\n"; print $newsocket "Howdy!\r\n"; close($newsocket); print "hello2\n"; }
Re: Perl socket server problem
by gmpassos (Priest) on Oct 12, 2003 at 07:05 UTC
    If you want to make a server forget Socket or IO::Socket. Take a look at Net::Server. Is very good, specially on Linux.

    I Have used a lot IO::Socket for servers, and learned a lot, but for a better result use Net::Server!

    Graciliano M. P.
    "Creativity is the expression of the liberty".

Re: Perl socket server problem
by pg (Canon) on Oct 11, 2003 at 16:40 UTC

    Lots of things said in the first reply is actually wrong. select does not have the magic to redirect standard output. I guess you are trying your program on a PC, if that's the case, now select does not work for windows. Your program simply hangs at that select (remove it, you will see what happens)

    I only read the first several paragraphes of the first reply, the conceptual part. So I am not commenting on those sample codes.

    Couple other trival tips:
    • when you print debug info, always put a \n at the end, so the msg will be flushed right the way, otherwise you could be confused for a long time for nothing, wondering where does the output go, well it is waiting to be flushed in the queue (for example your hallo1 here)
    • Secondly don't put your debug msg in single quos, it makes the \n not interpreted.
    • Last, I doubt your browser will show anything, as what you send to the client is not in html format (and not a http response). (Well, this depends on how your browser works, that's a seperate issue.)
      Lots of things said in the first reply is actually wrong. select does not have the magic to redirect standard output.
      Shocking! pg is usually right, but the first reply is completely accurate and highly educational! Here is a version of the original program with timestamps added:
      $queuelength=1; use Socket; socket(HANDLE, PF_INET, SOCK_STREAM, getprotobyname('tcp')); $addr = sockaddr_in(80, INADDR_ANY); bind(HANDLE, $addr); listen(HANDLE,$queuelength); while(true) { print "hallo1\n"; print STDERR "before accept ", scalar(localtime), "\n"; accept(NEW_HANDLE, HANDLE); print STDERR "after accept ", scalar(localtime), "\n"; select(NEW_HANDLE); print STDERR "after select ", scalar(localtime), "\n"; $line=<NEW_HANDLE>; print STDERR "after readline ", scalar(localtime), "\n"; print $line; print STDERR "after print ", scalar(localtime), "\n"; print "hello2\n"; } __END__ Output: before accept Sat Oct 11 16:01:55 2003 after accept Sat Oct 11 16:02:44 2003 after select Sat Oct 11 16:02:44 2003 after readline Sat Oct 11 16:02:44 2003 after print Sat Oct 11 16:02:44 2003 before accept Sat Oct 11 16:02:44 2003 after accept Sat Oct 11 16:03:12 2003 after select Sat Oct 11 16:03:12 2003 after readline Sat Oct 11 16:03:12 2003 after print Sat Oct 11 16:03:12 2003 before accept Sat Oct 11 16:03:12 2003
      Note that the only pauses are for the accept() call, not select(). And you can see a screen shot with two browser windows as well as the perl program in a DOS box here. The select() call redirects the standard output to a socket which sends is connect to a browser.

        <pedantic>
        Technically speaking one arg select() doesn't redirect standard out, it simply changes the default output handle (i.e. which handle print() will write to if you don't give it an explicit one). The standard out file descriptor (as well as the STDOUT filehandle) is unchanged and still points to the same place before and after select(SOMEOTHERHANDLE) has been called. If you fork/exec something else it's stdout will be the same as the parent, not the select'd handle.
        </pedantic>

        Thanks for being frank and straight with me.

        My apology and I realized that part of the confusion is that, there are two select functions in Perl, and that might be part of the problem in the original program. I don't want to guess whether he used the right select, or the wrong select.

        Any way, it is worth to mention that: 1) The first type of select takes a file handler as parameter, and does what Thelonius described; 2) A second one is the select system call, which determines whether the socket is ready for certain actions (this is what I meant in my original reply, and this one does not work for windows.)

        Great, we have all straightened something!

        Using select call in this context made me right the way rushing to the interpretation that he was making the system call...;-) well maybe he was.

        Anyway autoflush is a major part of the problem, so 1) do autoflush; or 2) $|++; or 3) \n...

Re: Perl socket server problem
by jonadab (Parson) on Oct 12, 2003 at 03:33 UTC

    Thelonius mentions "modules for that which handle things for you", but I thought that (in case you're just trying to get this working, and not interested in the lowlevel details) it might be useful to point you in the direction of HTTP::Daemon. This will let you throw together a small HTTP server much faster than trying to deal with plain sockets directly.

    OTOH, if your goal was to understand sockets, you might want to dispense initially with the complexities of HTTP and go with a simpler protocol for your first server, such as the Hello World protocol...

    If your goal was to understand HTTP itself as a protocol, then just ignore me and carry on as you were.


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/