- Perl Basics
- Perl - Home
- Perl - Introduction
- Perl - Environment
- Perl - Syntax Overview
- Perl - Data Types
- Perl - Variables
- Perl - Scalars
- Perl - Arrays
- Perl - Hashes
- Perl - IF...ELSE
- Perl - Loops
- Perl - Operators
- Perl - Date & Time
- Perl - Subroutines
- Perl - References
- Perl - Formats
- Perl - File I/O
- Perl - Directories
- Perl - Error Handling
- Perl - Special Variables
- Perl - Coding Standard
- Perl - Regular Expressions
- Perl - Sending Email
- Perl Advanced
- Perl - Socket Programming
- Perl - Object Oriented
- Perl - Database Access
- Perl - CGI Programming
- Perl - Packages & Modules
- Perl - Process Management
- Perl - Embedded Documentation
- Perl - Functions References
- Perl Useful Resources
- Perl - Questions and Answers
- Perl - Quick Guide
- Perl - Useful Resources
- Perl - Discussion
Perl listen Function
Description
This function configures the network socket SOCKET for listening to incoming network connections. Sets the incoming connection queue length to EXPR. You might want to consider using the IO::Socket module, which provides a much easier way of creating and listening to network sockets.
Syntax
Following is the simple syntax for this function −
listen SOCKET, EXPR
Return Value
This function returns 0 on failure and 1 on success.
Example
Following is the example code showing its basic usage, this is a server example alongwith socket implementation Perl Socket −
Perl Socket#!/usr/bin/perl -w # server.pl #-------------------- use strict; use Socket; # use port 7890 as default my $port = shift || 7890; my $proto = getprotobyname('tcp'); # 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( 'Sn4x8', AF_INET, $port, "\0\0\0\0" )) 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(NET_SOCKET, SOCKET)) { # send them a message, close connection print NEW_SOCKET "Smile from the server"; close NEW_SOCKET; }
perl_function_references.htm
Advertisements