How is it work the event handlers!?


 
Thread Tools Search this Thread
Top Forums Programming How is it work the event handlers!?
# 1  
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  
Old 09-02-2016
accept() will block at the kernel level, yes. It will be forced to wait until the client connects.

It's not based on the success of setting a variable. It does the same thing as while(accept(...)) without the assignment -- but the program needs the value accept is returning, so it's saved for later.
# 3  
Old 09-11-2016
Thanks very clear!
This User Gave Thanks to flaviofachin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

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

2. 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
Login or Register to Ask a Question