Sponsored Content
Top Forums Programming How is it work the event handlers!? Post 302980805 by flaviofachin on Friday 2nd of September 2016 08:11:07 AM
Old 09-02-2016
How is it work the event handlers!?

I just have started studying perl and I can not figure out what is the exact strategy used in the following script to handle events.

Precisely, what I do not understand is if the loop that is in charge to control the state of the socket, is managed at the system level (where the process will be locked inside the scheduler and so the following code must be read sequentially) or if this happens anywhere else, or maybe just in these "while loop" that apparently are based on the success of the setting of a variable.

Especially the "while" block at the server-side leaves me some concern.

Script to Create a Server
Code:
#!/usr/bin/perl -w
# Filename : server.pl

use strict;
use Socket;

# use port 7890 as default
my $port = shift || 7890;
my $proto = getprotobyname('tcp');
my $server = "localhost";  # Host IP running the server

# create a socket, make it reusable
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
   or die "Can't open socket $!\n";
setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1)
   or die "Can't set socket option to SO_REUSEADDR $!\n";

# bind to a port, then listen
bind( SOCKET, pack_sockaddr_in($port, inet_aton($server)))
   or die "Can't bind to port $port! \n";

listen(SOCKET, 5) or die "listen: $!";
print "SERVER started on port $port\n";

# accepting a connection
my $client_addr;
while ($client_addr = accept(NEW_SOCKET, SOCKET)) {
   # send them a message, close connection
   my $name = gethostbyaddr($client_addr, AF_INET );
   print NEW_SOCKET "Smile from the server";
   print "Connection recieved from $name\n";
   close NEW_SOCKET;
}
To run the server in background mode issue the following command on Unix prompt −

$perl sever.pl&
Script to Create a Client
Code:
#!/usr/bin/perl -w
# Filename : client.pl

use strict;
use Socket;

# initialize host and port
my $host = shift || 'localhost';
my $port = shift || 7890;
my $server = "localhost";  # Host IP running the server

# create the socket, connect to the port
socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2])
   or die "Can't create a socket $!\n";
connect( SOCKET, pack_sockaddr_in($port, inet_aton($server)))
   or die "Can't connect to port $port! \n";

my $line;
while ($line = <SOCKET>) {
        print "$line\n";
}
close SOCKET or die "close: $!";

Thanks for any answare!
 

2 More Discussions You Might Find Interesting

1. Programming

Signal Handlers using sigwait

After an extensive search, I haven't found a definitive answer to my question. "And what is your question you frackking noob", you may ask. Ok, here goes: When using sigwait to wait for SIGUSR1 or SIGUSR2, can you have it trigger a signal handler? The following code did NOT, and the example I got... (2 Replies)
Discussion started by: bcfd36
2 Replies

2. IP Networking

Discussion at work, would a router work pluging a cable in wan1 and lan1?

hi all. and sorry for the random question, but this sparkled a raging flame-war at work and i want more points of view situation a router, with linux of some sort, dhcp client requesting for ip in wan1 (as usual with wan ports) dhcp server listening in lan1, and assigning ip (as usual... (9 Replies)
Discussion started by: broli
9 Replies
NET-SERVER(1p)						User Contributed Perl Documentation					    NET-SERVER(1p)

NAME
net-server - Base Net::Server starting module SYNOPSIS
net-server [base type] [net server arguments] net-server PreFork ipv '*' net-server HTTP net-server HTTP app foo.cgi net-server HTTP app foo.cgi app /=bar.cgi net-server HTTP port 8080 port 8443/ssl ipv '*' server_type PreFork --SSL_key_file=my.key --SSL_cert_file=my.crt access_log_file STDERR DESCRIPTION
The net-server program gives a simple way to test out code and try port connection parameters. Though the running server can be robust enough for full tim use, it is anticipated that this binary will just be used for basic testing of net-server ports, acting as a simple echo server, or for running development scripts as CGI. OPTIONS
"base type" The very first argument may be a Net::Server flavor. This is given as shorthand for writing out server_type "ServerFlavor". Additionally, this allows types such as HTTP and PSGI, which are not true Net::Server base types, to subclass other server types via an additional server_type argument. net-server PreFork net-server HTTP # becomes a HTTP server in the Fork flavor net-server HTTP server_type PreFork # preforking HTTP server "port" Port to bind upon. Default is 80 if running a HTTP server as root, 8080 if running a HTTP server as non-root, or 20203 otherwise. Multiple value can be given for binding to multiple ports. All of the methods for specifying port attributes enumerated in Net::Server and Net::Server::Proto are available here. net-server port 20201 net-server port 20202 net-server port 20203/IPv6 "host" Host to bind to. Default is *. Will bind to an IPv4 socket if an IPv4 address is given. Will bind to an IPv6 socket if an IPv6 address is given (requires installation of IO::Socket::INET6). If a hostname is given and "ipv" is still set to 4, an IPv4 socket will be created. If a hostname is given and "ipv" is set to 6, an IPv6 socket will be created. If a hostname is given and "ipv" is set to * (default), a lookup will be performed and any available IPv4 or IPv6 addresses will be bound. The "ipv" parameter can be set directly, or passed along in the port, or additionally can be passed as part of the hostname. net-server host localhost net-server host localhost/IPv4 There are many more options available. Please see the Net::Server documentation. AUTHOR
Paul Seamons <paul at seamons.com> LICENSE
This package may be distributed under the terms of either the GNU General Public License or the Perl Artistic License perl v5.14.2 2012-06-12 NET-SERVER(1p)
All times are GMT -4. The time now is 02:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy