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

Here's a server that implements that protocol in its entirety:

#!/usr/bin/perl # Hello World Server, by Jonadab the Unsightly One A demonstration of # sockets. Listens on port 5 for connections made by the hellowc.pl # client, and answers them with a Hello World response, thus # demonstrating sockets. The system running the hellows.pl server # must have port 5 open. (Alternately, both the server and client # could be altered to use another port, but the server must listen on # the same port the client will connect to.) To stop the server, # delete the PID file. The server will then exit after answering the # next request. So, to terminate it immediately, after deleting the # PID file, run the client one final time to send the last request. # Be sure to change the $serverhost and $serverlog variables to # soemthing that makes sense on your system. $serverhost = 'localhost'; $serverport = 5; $serverlog = '/var/log/hellows.log'; $pidfile = "hellows.pid"; require 5.002; use sigtrap; use Socket; use IO::Socket; open LOG, ">>$serverlog" or die "Unable to append to server log $serverlog : $!"; open PID, ">$pidfile" or die "Unable to write to PID file hellows.pid +: $!\n"; { print PID "$$\n"; close PID; } my $sock = new IO::Socket::INET ( LocalHost => $serverhost, LocalPort => $serverport, Proto => 'tcp', Listen => 1, Reuse => 5, ); if ($sock) { print "Hello World Server is running, waiting for client connections +...\n"; while (-e "hellows.pid") { my $hellowsock = $sock->accept(); $_ = <$hellowsock>; { if (/[Hh]ello.*[Ww]orld.*[?]/) { print "..."; $canswered++; if ($canswered % 20 == 0) { print "\n +"; } print LOG localtime(time) . "\tAnswering Hello World request.\n"; print $hellowsock "Hello, client world! The server is here!\n"; } } } close($sock); } else { print LOG localtime(time) . "\tDIED: Unable to create socket: $!\n" +; die "Hello World Server unable to create socket: $!\n"; } print "\nAnswered $canswered requests from Hello World clients this se +ssion.\n";

And here is the corresponding client...

#/usr/bin/perl -w # Hello World Client, by Jonadab the Unsightly One # Give it an IP address of a machine running the # hellows.pl server, and it will query that server # for a Hello World response, demonstrating sockets. require 5.002; use sigtrap; use Socket; $serverhost = 'localhost'; $serverport = 5; use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => $serverhost, PeerPort => $serverport, Proto => 'tcp', ); die "Hello World Client unable to create socket: $!\n" unless $sock; print $sock "Hello, are you there, server world?\n"; print <$sock>; close($sock);

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$/

In reply to Re: Perl socket server problem by jonadab
in thread Perl socket server problem by pego

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.