>>>>> "JB" == Jonas Bull <[email protected]> writes:
JB> use POSIX qw|setsid|;
JB> use Getopt::Long;
JB> use Socket;
that module is not needed.
JB> use IO::Socket;
JB> use threads;
JB> use threads::shared;
JB> use IO::Select;
JB> ###################### options from cli #####################
JB> my ($daemonize,$port);
JB> $result = GetOptions (
JB> "daemonize" => \$daemonize,
JB> "port=s"=> \$port
JB> );
JB> &daemonize unless ($daemonize);
don't call subs with & as it isn't needed and could cause bugs.
JB> my $host='thishost';
JB> $port=7070 unless ($port);
JB> $| = 1;
that will only set the autoflush on stdout (the currently selected
handle). the io::socket module has an autoflush method you should use.
JB> local *S;
don't use typeglobs for handles. use a lexical scalar. io::socket
returns a handle for you.
JB> socket (*S, PF_INET , SOCK_STREAM , getprotobyname('tcp')) or
JB> die "couldn't open socket: $!";
JB> setsockopt (*S, SOL_SOCKET, SO_REUSEADDR, 1);
JB> bind (*S, sockaddr_in($port, INADDR_ANY));
JB> listen (*S, 5)
gack!! why did you load IO::Socket and not even use it? that all could
be replaced by a simple single call.
JB> my $ss = IO::Select->new();
JB> $ss -> add (*S);
JB> while(1) {
JB> my @connections_pending = $ss->can_read();
JB> foreach (@connections_pending) {
use a named lexical variable instead of $_. easier reading and stops
some bugs from happening (nested uses of $_).
JB> my $fh;
JB> my $remote = accept($fh, $_);
JB> my($port,$iaddr) = sockaddr_in($remote);
JB> my $peeraddress = inet_ntoa($iaddr);
why get the peer address when you don't even use it?
JB> my $t = threads->create(\&new_connection, $fh);
JB> $t->detach();
JB> }
JB> }
JB> sub new_connection {
JB> my $fh = shift;
JB> binmode $fh;
JB> my %req;
JB> $req{HEADER}={};
JB> my $request_line = <$fh>;
JB> while (($request_line ne "\r\n")&&($request_line ne "\n")) {
JB> unless ($request_line) {
JB> close $fh;
JB> }
JB> chomp $request_line;
JB> ## Do something with $request_line
JB> $request_line = <$fh>;
JB> }
JB> close $fh;
JB> }
JB> sub daemonize {
JB> defined(my $pid = fork) or die "Can't fork! $!";
JB> exit if $pid;
JB> setsid or die "Can't start a new session! $!";
JB> sleep 1;
JB> warn "$$ Daemonized... \n" unless ($pid);
JB> }
JB>
############################################################################################
JB> 1-
JB> There is a lot going on here, but the first thing is, I want to change
JB> the socket() calles to use IO::Socket something like this:
JB> my $s = new IO::Socket::INET(
JB> LocalHost => "$host",
why are you quoting that? not needed.
JB> LocalPort => $port,
JB> Proto => 'tcp',
not needed as that is the default
JB> Listen => 2,
JB> Reuse => 1,
not needed as that is set for you these days.
JB> Blocking => 0);
JB> $s or die "no socket :$!";
JB> But that doesn't work. It starts, binds the port, and doesn't do much
else.
doesn't work is not a useful message. what did you try to do?
have you read perldoc perlipc? also get the book network programming
with perl.
uri
--
Uri Guttman ------ [email protected] -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/